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
70 changes: 70 additions & 0 deletions packages/control-plane/src/routes/keyboard-shortcuts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_KEYBOARD_SHORTCUTS } from "@open-inspect/shared/types/keyboard-shortcuts";
import type * as AuthenticateModule from "../auth/authenticate";
import {
createTestRequestHandler,
ownerAuthorizationDatabase,
TEST_BACKGROUND_TASK_CONTEXT,
TEST_SERVICE_SECRETS,
} from "../router.test-support";
import type { Env } from "../types";
import { keyboardShortcutRoutes } from "./keyboard-shortcuts";

const mocks = vi.hoisted(() => ({ authenticate: vi.fn() }));

vi.mock("../auth/authenticate", async (importOriginal) => ({
...(await importOriginal<typeof AuthenticateModule>()),
authenticate: mocks.authenticate,
}));

const mockStore = { get: vi.fn(), set: vi.fn() };
vi.mock("../db/keyboard-shortcut-preferences", () => ({
KeyboardShortcutPreferencesStore: vi.fn().mockImplementation(function () {
return mockStore;
}),
}));

const handleRequest = createTestRequestHandler([keyboardShortcutRoutes]);
const env = { ...TEST_SERVICE_SECRETS, DB: ownerAuthorizationDatabase() } as unknown as Env;

describe("keyboard shortcut routes", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.authenticate.mockImplementation(async (request: Request) => ({
principal: { kind: "user", userId: "user-1" },
request,
}));
});

it("answers a personal read privately and uncacheably", async () => {

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.

[deep review] These three new route suites add roughly 180 lines of repeated authentication/store/env scaffolding to assert a three-line policy change, but they do not add a distinct contract boundary: the catalog snapshot records each exact declaration, the admission-matrix integration test exercises each declared policy on production routes, and request-lifecycle.test.ts verifies that the policy is stamped onto responses. The incidental payload/store assertions cover unchanged behavior, and this testing shape still missed the model-preferences BFF dropping the header. Please keep the policy coverage in those canonical tests and spend the targeted request-level coverage at the web boundary where behavior can actually be lost. The PUT assertion below should also go: absence of Cache-Control is not a behavioral invariant and would make a future no-store hardening fail for no reason.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Agreed. The three route-local suites are removed in a74563a; the conformance snapshot and the lifecycle test carry the policy coverage, and the new test sits at the web boundary where the header was actually lost. The PUT no-header assertion went with them.

mockStore.get.mockResolvedValue({ "session.new": "mod+k" });

const response = await handleRequest(
new Request("https://test.local/keyboard-shortcuts"),
env,
TEST_BACKGROUND_TASK_CONTEXT
);

expect(response.status).toBe(200);
expect(response.headers.get("Cache-Control")).toBe("private, no-store");
await expect(response.json()).resolves.toEqual({ shortcuts: { "session.new": "mod+k" } });
expect(mockStore.get).toHaveBeenCalledWith("user-1");
});

it("declares no cache policy on the write", async () => {
mockStore.set.mockResolvedValue(DEFAULT_KEYBOARD_SHORTCUTS);

const response = await handleRequest(
new Request("https://test.local/keyboard-shortcuts", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ shortcuts: DEFAULT_KEYBOARD_SHORTCUTS }),
}),
env,
TEST_BACKGROUND_TASK_CONTEXT
);

expect(response.status).toBe(200);
expect(response.headers.get("Cache-Control")).toBeNull();
});
});
6 changes: 5 additions & 1 deletion packages/control-plane/src/routes/keyboard-shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export const keyboardShortcutRoutes = new Hono<ControlPlaneHonoEnv>();

keyboardShortcutRoutes.get(
"/keyboard-shortcuts",
admit({ ...SCM_AGNOSTIC_HUMAN_USER_ROUTE, authorization: ACTIVE_SELF }),
admit({
...SCM_AGNOSTIC_HUMAN_USER_ROUTE,
authorization: ACTIVE_SELF,
cacheControl: "private, no-store",
}),
async (c) => {
const { ctx } = c.var.admitted;
const shortcuts = await new KeyboardShortcutPreferencesStore(ctx.db).get(ctx.principal.userId);
Expand Down
57 changes: 57 additions & 0 deletions packages/control-plane/src/routes/model-preferences.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_ENABLED_MODELS } from "@open-inspect/shared/models";
import type * as AuthenticateModule from "../auth/authenticate";
import {
createTestRequestHandler,
ownerAuthorizationDatabase,
TEST_BACKGROUND_TASK_CONTEXT,
TEST_SERVICE_SECRETS,
} from "../router.test-support";
import type { Env } from "../types";
import { modelPreferencesRoutes } from "./model-preferences";

const mocks = vi.hoisted(() => ({ authenticate: vi.fn() }));

vi.mock("../auth/authenticate", async (importOriginal) => ({
...(await importOriginal<typeof AuthenticateModule>()),
authenticate: mocks.authenticate,
}));

const mockStore = { getEnabledModels: vi.fn(), setEnabledModels: vi.fn() };
vi.mock("../db/model-preferences", async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
ModelPreferencesStore: vi.fn().mockImplementation(function () {
return mockStore;
}),
}));

const handleRequest = createTestRequestHandler([modelPreferencesRoutes]);
const env = {
...TEST_SERVICE_SECRETS,
SCM_PROVIDER: "github",
DB: ownerAuthorizationDatabase(),
} as unknown as Env;

describe("model preference routes", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.authenticate.mockImplementation(async (request: Request) => ({
principal: { kind: "user", userId: "user-1" },
request,
}));
});

it("answers the preference read privately and uncacheably", async () => {
mockStore.getEnabledModels.mockResolvedValue(null);

const response = await handleRequest(
new Request("https://test.local/model-preferences"),
env,
TEST_BACKGROUND_TASK_CONTEXT
);

expect(response.status).toBe(200);
expect(response.headers.get("Cache-Control")).toBe("private, no-store");
await expect(response.json()).resolves.toEqual({ enabledModels: DEFAULT_ENABLED_MODELS });
});
});
1 change: 1 addition & 0 deletions packages/control-plane/src/routes/model-preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ modelPreferencesRoutes.get(
admit({
...GITHUB_USER_OR_SERVICE_ROUTE,
authorization: activeGlobal({ actorlessGrants: [{ service: "slack-bot" }] }),
cacheControl: "private, no-store",

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.

[deep review] This policy does not reach the browser-facing /api/model-preferences response. packages/web/src/app/api/model-preferences/route.ts parses the control-plane response and constructs a fresh NextResponse without forwarding Cache-Control, so the primary web caller still has no explicit private/no-store policy. The neighboring keyboard-shortcuts and skill-profile routes already expose the code-judo move here: replace the bespoke 42-line web handler with settingsProxy(() => "/model-preferences", "model preferences"). That makes the cache behavior consistent and deletes duplicated auth, body parsing, response translation, and error handling. Please cover the web route boundary as part of that change; the new control-plane-only test passes while this regression remains.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Confirmed and fixed in a74563a: /api/model-preferences is now settingsProxy(() => "/model-preferences", "model preferences"), and route.test.ts at that boundary asserts the private, no-store header on the read and the forwarded update.

}),
(c) => getModelPreferences(c.var.admitted.ctx)
);
Expand Down
53 changes: 53 additions & 0 deletions packages/control-plane/src/routes/skills.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type * as AuthenticateModule from "../auth/authenticate";
import {
createTestRequestHandler,
ownerAuthorizationDatabase,
TEST_BACKGROUND_TASK_CONTEXT,
TEST_SERVICE_SECRETS,
} from "../router.test-support";
import type { Env } from "../types";
import { skillRoutes } from "./skills";

const mocks = vi.hoisted(() => ({ authenticate: vi.fn() }));

vi.mock("../auth/authenticate", async (importOriginal) => ({
...(await importOriginal<typeof AuthenticateModule>()),
authenticate: mocks.authenticate,
}));

const mockProfileStore = { list: vi.fn() };
vi.mock("../db/skill-profiles", async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
SkillProfileStore: vi.fn().mockImplementation(function () {
return mockProfileStore;
}),
}));

const handleRequest = createTestRequestHandler([skillRoutes]);
const env = { ...TEST_SERVICE_SECRETS, DB: ownerAuthorizationDatabase() } as unknown as Env;

describe("skill profile routes", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.authenticate.mockImplementation(async (request: Request) => ({
principal: { kind: "user", userId: "user-1" },
request,
}));
});

it("answers the caller's own profiles privately and uncacheably", async () => {
mockProfileStore.list.mockResolvedValue([]);

const response = await handleRequest(
new Request("https://test.local/skill-profiles"),
env,
TEST_BACKGROUND_TASK_CONTEXT
);

expect(response.status).toBe(200);
expect(response.headers.get("Cache-Control")).toBe("private, no-store");
await expect(response.json()).resolves.toEqual({ profiles: [] });
expect(mockProfileStore.list).toHaveBeenCalledWith("user-1");
});
});
7 changes: 6 additions & 1 deletion packages/control-plane/src/routes/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,11 @@ const PROFILES_MANAGE_OWN = admit({
...SCM_AGNOSTIC_HUMAN_USER_ROUTE,
authorization: requirePermission("skill_profiles.manage_own"),
});
const PROFILES_READ_OWN = admit({
...SCM_AGNOSTIC_HUMAN_USER_ROUTE,
authorization: requirePermission("skill_profiles.manage_own"),
cacheControl: "private, no-store",
});

export const skillRoutes = new Hono<ControlPlaneHonoEnv>();

Expand All @@ -649,7 +654,7 @@ skillRoutes.put("/skills/:id", SKILLS_MANAGE, (c) =>
dispatch(c, handleReplaceSkillContentAndAssignments)
);
skillRoutes.delete("/skills/:id", SKILLS_MANAGE, (c) => dispatch(c, handleDeleteSkill));
skillRoutes.get("/skill-profiles", PROFILES_MANAGE_OWN, (c) => dispatch(c, handleListProfiles));
skillRoutes.get("/skill-profiles", PROFILES_READ_OWN, (c) => dispatch(c, handleListProfiles));
skillRoutes.post("/skill-profiles", PROFILES_MANAGE_OWN, (c) => dispatch(c, handleCreateProfile));
skillRoutes.patch("/skill-profiles/:id", PROFILES_MANAGE_OWN, (c) =>
dispatch(c, handleUpdateProfile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ exports[`Hono route catalog conformance > dispatches every frozen method/path/po
"{"identity":"GET /image-builds/status","pathname":"/image-builds/status","groups":{},"authentication":"user-or-service","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"image_builds.read"}],"auditAllowed":false,"service":{"kind":"actor"}},"supportedScmProviders":["github"],"cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /image-builds/enabled","pathname":"/image-builds/enabled","groups":{},"authentication":"user-or-service","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"image_builds.read"}],"auditAllowed":false,"service":{"kind":"actor"}},"supportedScmProviders":["github"],"cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /image-builds/enabled-repos","pathname":"/image-builds/enabled-repos","groups":{},"authentication":"user-or-service","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"image_builds.read"}],"auditAllowed":false,"service":{"kind":"actor"}},"supportedScmProviders":["github"],"cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /model-preferences","pathname":"/model-preferences","groups":{},"authentication":"user-or-service","authorization":{"kind":"active-global","service":{"kind":"actor","actorlessGrants":[{"service":"slack-bot"}]},"auditAllowed":false},"supportedScmProviders":["github"],"cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /model-preferences","pathname":"/model-preferences","groups":{},"authentication":"user-or-service","authorization":{"kind":"active-global","service":{"kind":"actor","actorlessGrants":[{"service":"slack-bot"}]},"auditAllowed":false},"supportedScmProviders":["github"],"cacheControl":"private, no-store","hasServiceActorClaims":false}",
"{"identity":"PUT /model-preferences","pathname":"/model-preferences","groups":{},"authentication":"user-or-service","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"models.preferences.manage"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":["github"],"cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /model-provider-accounts/legacy-credentials","pathname":"/model-provider-accounts/legacy-credentials","groups":{},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"provider_accounts.read"}],"auditAllowed":false,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":"private, no-store","hasServiceActorClaims":false}",
"{"identity":"GET /model-provider-accounts","pathname":"/model-provider-accounts","groups":{},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"provider_accounts.read"}],"auditAllowed":false,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":"private, no-store","hasServiceActorClaims":false}",
Expand Down Expand Up @@ -157,11 +157,11 @@ exports[`Hono route catalog conformance > dispatches every frozen method/path/po
"{"identity":"PATCH /skills/:id","pathname":"/skills/fixture-152-id%2Fraw","groups":{"id":"fixture-152-id%2Fraw"},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skills.manage"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"PUT /skills/:id","pathname":"/skills/fixture-153-id%2Fraw","groups":{"id":"fixture-153-id%2Fraw"},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skills.manage"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"DELETE /skills/:id","pathname":"/skills/fixture-154-id%2Fraw","groups":{"id":"fixture-154-id%2Fraw"},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skills.manage"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /skill-profiles","pathname":"/skill-profiles","groups":{},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skill_profiles.manage_own"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /skill-profiles","pathname":"/skill-profiles","groups":{},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skill_profiles.manage_own"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":"private, no-store","hasServiceActorClaims":false}",
"{"identity":"POST /skill-profiles","pathname":"/skill-profiles","groups":{},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skill_profiles.manage_own"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"PATCH /skill-profiles/:id","pathname":"/skill-profiles/fixture-157-id%2Fraw","groups":{"id":"fixture-157-id%2Fraw"},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skill_profiles.manage_own"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"DELETE /skill-profiles/:id","pathname":"/skill-profiles/fixture-158-id%2Fraw","groups":{"id":"fixture-158-id%2Fraw"},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"skill_profiles.manage_own"}],"auditAllowed":true,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /keyboard-shortcuts","pathname":"/keyboard-shortcuts","groups":{},"authentication":"user","authorization":{"kind":"active-self","auditAllowed":false},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /keyboard-shortcuts","pathname":"/keyboard-shortcuts","groups":{},"authentication":"user","authorization":{"kind":"active-self","auditAllowed":false},"supportedScmProviders":"all","cacheControl":"private, no-store","hasServiceActorClaims":false}",
"{"identity":"PUT /keyboard-shortcuts","pathname":"/keyboard-shortcuts","groups":{},"authentication":"user","authorization":{"kind":"active-self","auditAllowed":true},"supportedScmProviders":"all","cacheControl":null,"hasServiceActorClaims":false}",
"{"identity":"GET /me/authorization","pathname":"/me/authorization","groups":{},"authentication":"user","authorization":{"kind":"authenticated","auditAllowed":false},"supportedScmProviders":"all","cacheControl":"private, no-store","hasServiceActorClaims":false}",
"{"identity":"GET /roles","pathname":"/roles","groups":{},"authentication":"user","authorization":{"kind":"active-user","allOf":[{"kind":"permission","permission":"workspace.roles.read"}],"auditAllowed":false,"service":{"kind":"actor"}},"supportedScmProviders":"all","cacheControl":"private, no-store","hasServiceActorClaims":false}",
Expand Down
Loading