Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/shared/validation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ export const v1betaGeminiGenerateSchema = z
});

export const cliMitmStartSchema = z.object({
apiKey: z.string().trim().min(1, "Missing apiKey"),
apiKey: z.string().trim().min(1).nullable().optional(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

Since Zod's z.object() strips unrecognized keys by default during parsing, any keyId sent by the frontend will be stripped from the validated output. To allow the backend to receive and resolve the keyId as described in the PR description, keyId should be explicitly defined in the schema.

Suggested change
apiKey: z.string().trim().min(1).nullable().optional(),
apiKey: z.string().trim().min(1).nullable().optional(),
keyId: z.string().trim().min(1).optional(),

sudoPassword: z.string().optional(),
});

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/cli-mitm-schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import test from "node:test";
import assert from "node:assert/strict";
import { cliMitmStartSchema } from "../../src/shared/validation/schemas.ts";
import { validateBody } from "../../src/shared/validation/helpers.ts";

test("cliMitmStartSchema accepts a non-empty string apiKey", () => {
const result = validateBody(cliMitmStartSchema, {
apiKey: "sk-test-key-value",
sudoPassword: "password123",
});
assert.equal(result.success, true);
if (result.success) {
assert.equal(result.data.apiKey, "sk-test-key-value");
assert.equal(result.data.sudoPassword, "password123");
}
});

test("cliMitmStartSchema accepts a null apiKey", () => {
const result = validateBody(cliMitmStartSchema, {
apiKey: null,
sudoPassword: "",
});
assert.equal(result.success, true);
if (result.success) {
assert.equal(result.data.apiKey, null);
assert.equal(result.data.sudoPassword, "");
}
});

test("cliMitmStartSchema accepts an omitted apiKey", () => {
const result = validateBody(cliMitmStartSchema, {
sudoPassword: "",
});
assert.equal(result.success, true);
if (result.success) {
assert.equal(result.data.apiKey, undefined);
}
});
Comment on lines +31 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure the new keyId field is properly validated and not stripped, we should add a unit test verifying that keyId is accepted and correctly parsed by the schema.

test("cliMitmStartSchema accepts an omitted apiKey", () => {
  const result = validateBody(cliMitmStartSchema, {
    sudoPassword: "",
  });
  assert.equal(result.success, true);
  if (result.success) {
    assert.equal(result.data.apiKey, undefined);
  }
});

test("cliMitmStartSchema accepts a keyId instead of apiKey", () => {
  const result = validateBody(cliMitmStartSchema, {
    keyId: "key_12345",
    sudoPassword: "",
  });
  assert.equal(result.success, true);
  if (result.success) {
    assert.equal(result.data.keyId, "key_12345");
    assert.equal(result.data.apiKey, undefined);
  }
});