Skip to content
Draft
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
5 changes: 4 additions & 1 deletion src/codex/prompt-layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,10 @@ export function writeBaseVariant(
? input.id
: input.id ?? newBaseVariantId(existing);
if (!BASE_VARIANT_ID.test(targetId)) return { ok: false, error: "unknown_layer", detail: targetId };
if (deleting && !existing.some(v => v.id === targetId)) {
// A caller-supplied id is an edit (or delete), never an alternate create path.
// Requiring it to exist keeps the generated-id path as the sole place where a
// new variant can enter, and therefore makes the cap impossible to bypass.
if (input.id !== null && !existing.some(v => v.id === targetId)) {
return { ok: false, error: "unknown_layer", detail: targetId };
}
if (!deleting && input.id === null && existing.length >= MAX_BASE_VARIANTS) {
Expand Down
6 changes: 5 additions & 1 deletion src/server/management/codex-prompt-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,11 @@ export async function handleCodexPromptRoutes(ctx: ManagementContext): Promise<R
}
if (typeof body.title !== "string") return fail(ctx, "invalid_body", 400, "title must be a string");
if (typeof body.body !== "string") return fail(ctx, "invalid_body", 400, "body must be a string");
return settle(ctx, writeBaseVariant({ id, title: body.title, body: body.body }, revision, paths(ctx)));
const normalized = normalizeBody(body.body);
if (utf8Bytes(normalized) > MAX_BODY_BYTES) {
return fail(ctx, "body_too_large", 400, `base variant exceeds ${MAX_BODY_BYTES} bytes`);
Comment on lines +400 to +402

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Enforce the limit on the complete stored variant

When a dashboard-session client supplies a large title, this check measures only body, while writeBaseVariant prepends the unrestricted title to the persisted Markdown file and the subsequent snapshot reads it back. A title approaching the 4 MiB management-request limit therefore bypasses the new 64 KiB storage restriction and can still produce and echo a multi-megabyte base prompt. Validate the title and/or measure the complete serialized variant before writing it.

AGENTS.md reference: src/AGENTS.md:L20-L20

Useful? React with 👍 / 👎.

}
return settle(ctx, writeBaseVariant({ id, title: body.title, body: normalized }, revision, paths(ctx)));
}

if (url.pathname === "/api/codex-prompt/adopt" && req.method === "POST") {
Expand Down
9 changes: 9 additions & 0 deletions tests/codex-prompt-base-variants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ describe("base variant selection", () => {
expect(writeBaseVariant({ id, title: "edited", body: "b2" }, rev(paths), paths).ok).toBe(true);
});

test("a caller-supplied id cannot create a variant", () => {
const paths = fixture("model = \"x\"\n");
expect(writeBaseVariant({ id: "aaaaaa", title: "Injected", body: "b" }, rev(paths), paths)).toMatchObject({
ok: false,
error: "unknown_layer",
});
expect(readBaseVariants(paths)).toEqual([]);
});

test("a stale revision is refused", () => {
const paths = fixture("model = \"x\"\n");
const stale = rev(paths);
Expand Down
14 changes: 14 additions & 0 deletions tests/codex-prompt-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,20 @@ describe("020 coverage completions", () => {
expect(editDefault.status).toBe(400);
expect(editDefault.body.code).toBe("unknown_layer");

// A string id is edit-only. It cannot bypass the creation cap by naming a
// syntactically valid file that the server did not generate.
const editMissing = await call("PUT", "/api/codex-prompt/base", fx, {
id: "aaaaaa", title: "Missing", body: "b", revision: rev0,
});
expect(editMissing.status).toBe(400);
expect(editMissing.body.code).toBe("unknown_layer");

const oversized = await call("PUT", "/api/codex-prompt/base", fx, {
id: null, title: "Too large", body: "x".repeat(64 * 1024 + 1), revision: rev0,
});
expect(oversized.status).toBe(400);
expect(oversized.body.code).toBe("body_too_large");

// An unknown variant would leave the key naming a file Codex cannot read.
const unknown = await call("PUT", "/api/codex-prompt/base/select", fx, {
kind: "variant", id: "zzzzzz", revision: rev0,
Expand Down
Loading