import { BadRequestException, Body, CanActivate, ConflictException, Controller, DynamicModule, ForbiddenException, Get, Inject, Injectable, Module, NotFoundException, Param, Post, Req, UnauthorizedException, UseGuards, UsePipes, ValidationPipe, } from "@nestjs/common"; import { IsString, IsUUID, Matches, MaxLength } from "class-validator"; import { createHash, timingSafeEqual } from "crypto"; import { mkdir, open, FileHandle } from "fs/promises"; import { constants } from "fs"; import { isAbsolute } from "path"; import { PrismaModule } from "../prisma/prisma.module"; import { PrismaService } from "../prisma/prisma.service"; import { PREPARE_WORKFLOW_CONFIG, PrepareWorkflowService, WorkflowIdentity, } from "../orchestration/prepare-workflow.service"; // This is reviewed static configuration, not a URL fetched by the transport. function validArtifactBase(value: string): boolean { try { const u = new URL(value); // DNS name only: reject all IP literals, localhost/private single-label names, // noncanonical paths/escapes, credentials, query, fragment and alternate ports. return u.protocol === "https:" && !u.username && !u.password && !u.port && /^[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$/.test(u.hostname) && u.hostname.includes(".") && !/^[0-9.]+$/.test(u.hostname) && !/(^|\.)(localhost|local|internal|lan|home)$/.test(u.hostname) && u.pathname === "/internal/prepare-worker/artifacts" && value === u.origin + u.pathname; } catch { return false; } } const CONFIG = Symbol("WORKER_TRANSPORT_CONFIG"); export interface WorkerTransportConfig { enabled: boolean; grants: { id: string; role: "WORKFLOW_WORKER" | "STAGE_CI"; credentialSha256: string; }[]; workerIds: string[]; stageCiIds: string[]; artifactDirectory: string; artifactBaseUrl: string; } class ClaimDto { @IsUUID() taskId!: string; } class LeaseDto extends ClaimDto { @IsUUID() runId!: string; @IsUUID() leaseToken!: string; } class CompleteDto extends LeaseDto { @IsString() @MaxLength(2048) report!: string; } class StageEvidenceDto extends ClaimDto { @IsUUID() runId!: string; @Matches(/^[a-f0-9]{40}$/) commitSha!: string; @Matches(/^[a-f0-9]{64}$/) sha256!: string; @Matches(/^[a-f0-9]{64}$/) evidenceSha256!: string; @Matches(/^https:\/\/[^\s]+$/) @MaxLength(2000) uri!: string; } export function tracerReport(lease: { taskId: string; runId: string; commitSha: string; }) { return JSON.stringify({ kind: "DETERMINISTIC_PREPARE_TRACER", schemaVersion: 1, taskId: lease.taskId, runId: lease.runId, commitSha: lease.commitSha, environment: "staging", llmPilot: false, browserExecuted: false, shellExecuted: false, deploymentExecuted: false, checks: ["lease-bound identity", "deterministic JSON serialization"], actualCents: 0, }); } @Injectable() class WorkerIdentityGuard implements CanActivate { constructor(@Inject(CONFIG) private readonly config: WorkerTransportConfig) {} canActivate(context: any) { if (!this.config.enabled) throw new ForbiddenException("Worker transport disabled"); const req = context.switchToHttp().getRequest(); const auth = req.headers.authorization; if (typeof auth !== "string" || !/^Bearer [\x21-\x7e]{32,512}$/.test(auth)) throw new UnauthorizedException(); const digest = createHash("sha256").update(auth.slice(7)).digest(); const grant = this.config.grants.find((g) => timingSafeEqual(Buffer.from(g.credentialSha256, "hex"), digest), ); if (!grant) throw new UnauthorizedException(); req.workerIdentity = { id: grant.id, role: grant.role }; return true; } } @Injectable() class WorkerTransportService { constructor( private readonly workflow: PrepareWorkflowService, private readonly db: PrismaService, @Inject(CONFIG) private readonly config: WorkerTransportConfig, ) {} // Linux descriptor-relative traversal pins each directory across renames. Never // realpath-check then reopen by name: that reintroduces a symlink race. private async artifactDir(): Promise { let dir = await open("/", constants.O_RDONLY | constants.O_DIRECTORY); try { for (const part of this.config.artifactDirectory.split("/").filter(Boolean)) { const path = `/proc/self/fd/${dir.fd}/${part}`; try { await mkdir(path, { mode: 0o700 }); } catch (e: any) { if (e.code !== "EEXIST") throw e; } const next = await open(path, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW); await dir.close(); dir = next; } const stat = await dir.stat(); if (stat.uid !== process.getuid!() || (stat.mode & 0o077) !== 0) throw new Error("Private owned artifact directory required"); return dir; } catch (e) { await dir.close(); throw e; } } private worker(a: WorkflowIdentity) { if (a.role !== "WORKFLOW_WORKER" || !this.config.workerIds.includes(a.id)) throw new ForbiddenException("Worker allowlist required"); } async claim(a: WorkflowIdentity, dto: ClaimDto) { this.worker(a); const t = await this.db.orchestrationTask.findUnique({ where: { id: dto.taskId }, }); // Explicit per-task pilot scope; never opportunistically consume arbitrary queued work. if (!t || !["READY", "RUNNING"].includes(t.state)) return { lease: null }; if (t.budgetLimitCents !== 20) throw new ForbiddenException("Pilot hard task cap is 20 cents"); return { lease: await this.workflow.claim(a, dto.taskId) }; } heartbeat(a: WorkflowIdentity, dto: LeaseDto) { this.worker(a); return this.workflow.heartbeat(a, dto); } async complete(a: WorkflowIdentity, dto: CompleteDto) { this.worker(a); // Recheck stop/interrupt and fencing before artifact creation; core rechecks atomically at commit. await this.workflow.heartbeat(a, dto); const run = await this.db.orchestrationRun.findUniqueOrThrow({ where: { id: dto.runId }, }); const expected = tracerReport({ ...dto, commitSha: run.commitSha }); if (dto.report !== expected) throw new BadRequestException( "Only deterministic harmless tracer report accepted", ); const sha256 = createHash("sha256").update(expected).digest("hex"); let dir: FileHandle | undefined, file: FileHandle | undefined; try { dir = await this.artifactDir(); file = await open(`/proc/self/fd/${dir.fd}/${sha256}.json`, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); await file.writeFile(expected, "utf8"); await file.sync(); } catch { throw new ConflictException("Artifact creation refused"); } finally { await file?.close(); await dir?.close(); } const result = await this.workflow.complete(a, { ...dto, sha256, uri: this.config.artifactBaseUrl + "/" + sha256, actualCents: 0, }); return { state: result.state, taskId: result.id, runId: dto.runId, sha256, actualCents: 0, }; } async artifact(a: WorkflowIdentity, hash: string) { if (!/^[a-f0-9]{64}$/.test(hash)) throw new BadRequestException("Invalid artifact hash"); const registered = await this.db.orchestrationArtifact.findFirst({ where: { sha256: hash, kind: "QA", uri: this.config.artifactBaseUrl + "/" + hash, run: { environment: "staging", status: "SUCCEEDED", task: { operationMode: "PREPARE", budgetLimitCents: 20, } }, }, }); if (!registered) throw new NotFoundException("Artifact not registered"); const run = await this.db.orchestrationRun.findUniqueOrThrow({ where: { id: registered.runId } }); const task = await this.db.orchestrationTask.findUniqueOrThrow({ where: { id: run.taskId } }); const outbox = await this.db.prepareWorkflowOutbox.findUnique({ where: { taskId: run.taskId } }); if (!outbox || outbox.status !== "DONE" || outbox.runId !== run.id || (a.role === "WORKFLOW_WORKER" && outbox.leaseOwner !== a.id)) throw new NotFoundException("Artifact not registered"); const metadata = task.metadata as any; if (metadata?.workflowScope !== "STAGE_PREPARE" || metadata?.commitSha !== run.commitSha) throw new NotFoundException("Artifact not registered"); let bytes: string; let dir: FileHandle | undefined, file: FileHandle | undefined; try { dir = await this.artifactDir(); file = await open(`/proc/self/fd/${dir.fd}/${hash}.json`, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); const stat = await file.stat(); if (!stat.isFile() || stat.nlink !== 1 || stat.uid !== process.getuid!() || (stat.mode & 0o077) !== 0 || stat.size > 2048) throw new Error("Unsafe artifact file"); // Fixed-size descriptor read: concurrent growth cannot allocate unbounded memory. const buffer = Buffer.alloc(2049); const { bytesRead } = await file.read(buffer, 0, buffer.length, 0); if (bytesRead > 2048) throw new Error("Oversized artifact"); bytes = buffer.subarray(0, bytesRead).toString("utf8"); } catch { throw new NotFoundException("Artifact unavailable"); } finally { await file?.close(); await dir?.close(); } if (createHash("sha256").update(bytes).digest("hex") !== hash) throw new ConflictException("Artifact integrity mismatch"); if (bytes !== tracerReport({ taskId: run.taskId, runId: run.id, commitSha: run.commitSha })) throw new ConflictException("Artifact binding mismatch"); return JSON.parse(bytes); } stage(a: WorkflowIdentity, dto: StageEvidenceDto) { return this.workflow.stageEvidence(a, dto); } } @Controller("internal/prepare-worker") @UseGuards(WorkerIdentityGuard) @UsePipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, }), ) class WorkerTransportController { constructor(private readonly service: WorkerTransportService) {} @Post("claim") claim(@Req() req: any, @Body() dto: ClaimDto) { return this.service.claim(req.workerIdentity, dto); } @Post("heartbeat") heartbeat(@Req() req: any, @Body() dto: LeaseDto) { return this.service.heartbeat(req.workerIdentity, dto); } @Post("complete") complete(@Req() req: any, @Body() dto: CompleteDto) { return this.service.complete(req.workerIdentity, dto); } @Post("stage-evidence") stage( @Req() req: any, @Body() dto: StageEvidenceDto, ) { return this.service.stage(req.workerIdentity, dto); } @Get("artifacts/:hash") artifact(@Req() req: any, @Param("hash") hash: string) { return this.service.artifact(req.workerIdentity, hash); } } @Module({}) export class WorkerTransportModule { static register(config: WorkerTransportConfig): DynamicModule { if (typeof config.enabled !== "boolean" || !Array.isArray(config.grants) || !Array.isArray(config.workerIds) || !Array.isArray(config.stageCiIds)) throw new Error("Explicit boolean enable and credential allowlists required"); const allIds = [...config.workerIds, ...config.stageCiIds]; if (allIds.some(id => typeof id !== "string" || !/^[a-zA-Z0-9_-]{1,100}$/.test(id)) || new Set(allIds).size !== allIds.length) throw new Error("Invalid or overlapping worker/CI allowlists"); const ids = new Set(), digests = new Set(); if ( !isAbsolute(config.artifactDirectory) || config.artifactDirectory === "/" || config.artifactDirectory.split("/").some(p => p === "." || p === "..") || !validArtifactBase(config.artifactBaseUrl) ) throw new Error( "Absolute artifact directory and fixed HTTPS artifact base required", ); for (const g of config.grants) { const allowed = g.role === "WORKFLOW_WORKER" ? config.workerIds : g.role === "STAGE_CI" ? config.stageCiIds : []; if ( !/^[a-zA-Z0-9_-]{1,100}$/.test(g.id) || !allowed.includes(g.id) || !/^[a-f0-9]{64}$/.test(g.credentialSha256) || ids.has(g.id) || digests.has(g.credentialSha256) || (config.workerIds.includes(g.id) && config.stageCiIds.includes(g.id)) ) throw new Error("Invalid or overlapping worker/CI credential grants"); ids.add(g.id); digests.add(g.credentialSha256); } const snapshot = JSON.parse(JSON.stringify(config)) as WorkerTransportConfig; snapshot.grants.forEach(Object.freeze); Object.freeze(snapshot.grants); Object.freeze(snapshot.workerIds); Object.freeze(snapshot.stageCiIds); Object.freeze(snapshot); return { module: WorkerTransportModule, imports: [PrismaModule], controllers: [WorkerTransportController], providers: [ PrepareWorkflowService, WorkerIdentityGuard, WorkerTransportService, { provide: CONFIG, useValue: snapshot }, { provide: PREPARE_WORKFLOW_CONFIG, useValue: Object.freeze({ workerIds: snapshot.workerIds, stageCiIds: snapshot.stageCiIds, }), }, ], exports: [PrepareWorkflowService], }; } }