diff --git a/server/src/__tests__/secret-proposals-routes.test.ts b/server/src/__tests__/secret-proposals-routes.test.ts index 2ea6f49f634..a9028b4cb5f 100644 --- a/server/src/__tests__/secret-proposals-routes.test.ts +++ b/server/src/__tests__/secret-proposals-routes.test.ts @@ -228,6 +228,30 @@ describeEmbeddedPostgres("secret proposal routes", () => { return app; } + it("returns 422, not 500, when a proposal omits a required field", async () => { + const fixture = await seedRun(); + + const secretMissingJustification = await request(createAgentApp(fixture)) + .post("/api/agents/me/secret-proposals") + .send({ kind: "secret", name: "dev/missing/justification", value: "a-32-character-or-longer-secret", description: "x" }); + expect(secretMissingJustification.status).toBe(422); + + const secretMissingName = await request(createAgentApp(fixture)) + .post("/api/agents/me/secret-proposals") + .send({ kind: "secret", value: "a-32-character-or-longer-secret", justification: "Needed by the task" }); + expect(secretMissingName.status).toBe(422); + + const bindingMissingJustification = await request(createAgentApp(fixture)) + .post("/api/agents/me/secret-proposals") + .send({ kind: "binding", sourceConfigPath: "env.SOURCE_TOKEN", configPath: "env.TARGET_TOKEN" }); + expect(bindingMissingJustification.status).toBe(422); + + const complete = await request(createAgentApp(fixture)) + .post("/api/agents/me/secret-proposals") + .send({ kind: "secret", name: "dev/missing/ok", value: "a-32-character-or-longer-secret", justification: "Needed by the task" }); + expect(complete.status).toBe(201); + }); + describe("expiry scheduling", () => { let coordinator: ReturnType | undefined; let proposals: ReturnType; diff --git a/server/src/services/secret-proposals.ts b/server/src/services/secret-proposals.ts index b098946ce7f..562fc285177 100644 --- a/server/src/services/secret-proposals.ts +++ b/server/src/services/secret-proposals.ts @@ -324,6 +324,8 @@ export function createSecretProposalsService(db: Db) { value: string; justification: string; }) { + if (typeof input.name !== "string") throw unprocessable("Secret name is required"); + if (typeof input.justification !== "string") throw unprocessable("Justification is required"); const name = input.name.trim(); const justification = input.justification.trim(); if (!SECRET_NAME_RE.test(name)) throw unprocessable("Secret name must be a slash-separated path without empty segments"); @@ -381,7 +383,9 @@ export function createSecretProposalsService(db: Db) { if (input.sourceConfigPath && !CONFIG_PATH_RE.test(input.sourceConfigPath)) { throw unprocessable("sourceConfigPath must use env. or access."); } - if (!input.justification.trim()) throw unprocessable("Justification is required"); + if (typeof input.justification !== "string" || !input.justification.trim()) { + throw unprocessable("Justification is required"); + } if (input.justification.trim().length > 20_000) throw unprocessable("Justification must be at most 20000 characters"); const { run, originIssueId } = await loadRunContext(db, context); const targetAgentId = input.targetAgentId ?? run.agentId;