Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/server/negative-results/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ type NegativeResultRow = {
};

export class DatabaseNegativeResultRepository implements NegativeResultRepository {
constructor(private readonly database?: Queryable) {}
constructor(
private readonly database?: Queryable,
private readonly manageTransactions = true,
) {}

private run<T>(operation: (db: Queryable) => Promise<T>): Promise<T> {
return this.database ? operation(this.database) : withDatabase(operation);
Expand Down Expand Up @@ -261,7 +264,7 @@ export class DatabaseNegativeResultRepository implements NegativeResultRepositor
input: UpdateNegativeResultInput,
): Promise<NegativeResultRecord | undefined> {
return this.run((db) =>
withTransaction(db, async (tx) => {
this.inTransaction(db, async (tx) => {
const current = await readCurrentRow(tx, id);
if (!current) return undefined;
const record = mapRow(current);
Expand Down Expand Up @@ -291,7 +294,7 @@ export class DatabaseNegativeResultRepository implements NegativeResultRepositor

async upsertBySubject(input: CreateNegativeResultInput): Promise<NegativeResultRecord> {
return this.run((db) =>
withTransaction(db, async (tx) => {
this.inTransaction(db, async (tx) => {
const normalized = normalizeCreateInput(input);
await validateDatabaseInput(tx, normalized);
const key = coverageKey(normalized);
Expand Down Expand Up @@ -333,10 +336,20 @@ export class DatabaseNegativeResultRepository implements NegativeResultRepositor
return result.rows.map(mapRow);
});
}

private inTransaction<T>(
db: Queryable,
operation: (tx: Queryable) => Promise<T>,
): Promise<T> {
return this.manageTransactions ? withTransaction(db, operation) : operation(db);
}
}

export function createDatabaseNegativeResultRepository(db?: Queryable) {
return new DatabaseNegativeResultRepository(db);
export function createDatabaseNegativeResultRepository(
db?: Queryable,
options: { manageTransactions?: boolean } = {},
) {
return new DatabaseNegativeResultRepository(db, options.manageTransactions ?? true);
}

export function createInMemoryNegativeResultRepository(
Expand Down
8 changes: 8 additions & 0 deletions src/server/recon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export {
type PersistPassiveAuthSurfaceResult,
persistPassiveAuthSurface,
} from "./passive-auth-surface";
export {
type CompletePassiveAuthMappingInput,
createPassiveAuthTaskAlignmentService,
lockPassiveAuthMappingScope,
type PassiveAuthMappingScope,
type PassiveAuthTaskAlignmentResult,
type PassiveAuthTaskAlignmentService,
} from "./passive-auth-task-alignment";
export {
type CreateStoredPassiveAuthSurfaceInput,
type CreateStoredPassiveAuthSurfaceResult,
Expand Down
3 changes: 3 additions & 0 deletions src/server/recon/passive-auth-blockers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { PassiveAuthSurfaceSummary } from "./passive-auth-surface";
export type PersistPassiveAuthBlockersInput = {
projectId: string;
threadId?: string;
sourceThreadId?: string;
targetId: string;
taskId?: string;
summaryArtifactId: string;
Expand Down Expand Up @@ -43,6 +44,8 @@ export async function persistPassiveAuthBlockers(
dedupeKey: passiveBlockerDedupeKey(input, signal.reason),
metadata: {
workflow: "passive-auth-surface-v1",
scope: "project-target",
sourceThreadId: input.sourceThreadId ?? input.threadId ?? null,
summaryArtifactId: input.summaryArtifactId,
sourceArtifactIds: [...signal.evidenceArtifactIds].sort(),
passiveOnly: true,
Expand Down
47 changes: 27 additions & 20 deletions src/server/recon/passive-auth-surface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
type BlockerRecord,
type BlockerService,
getDefaultBlockerService,
} from "../blockers";
import type { BlockerRecord } from "../blockers";
import type {
ArtifactServiceInstance,
CreateArtifactResult,
Expand All @@ -14,7 +10,11 @@ import {
type NormalizedDiscovery,
normalizeDiscoveryArtifacts,
} from "./discovery-artifact-normalizer";
import { persistPassiveAuthBlockers } from "./passive-auth-blockers";
import {
createPassiveAuthTaskAlignmentService,
type PassiveAuthTaskAlignmentResult,
type PassiveAuthTaskAlignmentService,
} from "./passive-auth-task-alignment";

export type PassiveAuthSurfaceSummary = {
targetId: string;
Expand Down Expand Up @@ -55,15 +55,17 @@ export type PersistPassiveAuthSurfaceResult = {
summary: PassiveAuthSurfaceSummary;
artifact: CreateArtifactResult;
blockers: BlockerRecord[];
taskAlignment: PassiveAuthTaskAlignmentResult;
};

type ArtifactWriter = Pick<ArtifactServiceInstance, "createArtifact">;

export async function persistPassiveAuthSurface(
input: PersistPassiveAuthSurfaceInput,
artifactWriter: ArtifactWriter,
blockerService: BlockerService = getDefaultBlockerService(),
taskAlignment: PassiveAuthTaskAlignmentService = createPassiveAuthTaskAlignmentService(),
): Promise<PersistPassiveAuthSurfaceResult> {
const mappingTask = await taskAlignment.start(input);
const normalized = normalizeDiscoveryArtifacts(input.artifacts);
const summary = buildPassiveAuthSurfaceSummary(
input.targetId,
Expand All @@ -75,7 +77,7 @@ export async function persistPassiveAuthSurface(
projectId: input.projectId,
...(input.threadId ? { threadId: input.threadId } : {}),
targetId: input.targetId,
...(input.taskId ? { taskId: input.taskId } : {}),
taskId: mappingTask.id,
name: `passive-auth-surface-${input.targetId}.json`,
kind: "report",
contentType: "application/json",
Expand All @@ -87,25 +89,30 @@ export async function persistPassiveAuthSurface(
attributionPolicy: "project-thread-task-target",
metadata: {
workflow: "passive-auth-surface-v1",
sourceTaskId: input.taskId ?? null,
rawArtifactIds: summary.rawArtifactIds,
rawArtifacts: summary.rawArtifacts,
blockerReasons: summary.blockers.map((blocker) => blocker.reason),
authCategories: summary.authRoutes.map((route) => route.category),
},
});
const blockers = await persistPassiveAuthBlockers(
{
projectId: input.projectId,
...(input.threadId ? { threadId: input.threadId } : {}),
targetId: input.targetId,
...(input.taskId ? { taskId: input.taskId } : {}),
summaryArtifactId: artifact.id,
summary,
},
blockerService,
);
const aligned = await taskAlignment.complete({
projectId: input.projectId,
...(input.threadId ? { threadId: input.threadId } : {}),
targetId: input.targetId,
taskId: mappingTask.id,
...(input.taskId ? { sourceTaskId: input.taskId } : {}),
reportArtifactId: artifact.id,
summary,
});

return { normalized, summary, artifact, blockers };
return {
normalized,
summary,
artifact,
blockers: aligned.blockers,
taskAlignment: aligned,
};
}

export function buildPassiveAuthSurfaceSummary(
Expand Down
Loading