-
Notifications
You must be signed in to change notification settings - Fork 413
fix: mark personal-preference reads private and uncacheable #1731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
| import { controlPlaneUserFetch } from "@/lib/control-plane"; | ||
| import { GET, PUT } from "./route"; | ||
|
|
||
| vi.mock("@/lib/control-plane", () => ({ controlPlaneUserFetch: vi.fn() })); | ||
|
|
||
| const context = { params: Promise.resolve(undefined) }; | ||
|
|
||
| describe("/api/model-preferences", () => { | ||
| beforeEach(() => vi.resetAllMocks()); | ||
|
|
||
| it("relays the preferences read as a private, uncacheable response", async () => { | ||
| vi.mocked(controlPlaneUserFetch).mockResolvedValue( | ||
| Response.json({ defaultModel: "anthropic/claude-sonnet-5" }) | ||
| ); | ||
|
|
||
| const response = await GET(new NextRequest("http://localhost/api/model-preferences"), context); | ||
|
|
||
| expect(controlPlaneUserFetch).toHaveBeenCalledWith("/model-preferences", undefined); | ||
| expect(response.status).toBe(200); | ||
| expect(response.headers.get("Cache-Control")).toBe("private, no-store"); | ||
| await expect(response.json()).resolves.toEqual({ defaultModel: "anthropic/claude-sonnet-5" }); | ||
| }); | ||
|
|
||
| it("forwards a preferences update with the browser session", async () => { | ||
| vi.mocked(controlPlaneUserFetch).mockResolvedValue(Response.json({ ok: true })); | ||
| const request = new NextRequest("http://localhost/api/model-preferences", { | ||
| method: "PUT", | ||
| headers: { Cookie: "__Secure-openinspect.session_token=session.signature" }, | ||
| body: JSON.stringify({ defaultModel: "anthropic/claude-opus-5" }), | ||
| }); | ||
|
|
||
| const response = await PUT(request, context); | ||
|
|
||
| expect(controlPlaneUserFetch).toHaveBeenCalledWith("/model-preferences", { | ||
| method: "PUT", | ||
| body: JSON.stringify({ defaultModel: "anthropic/claude-opus-5" }), | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| expect(response.status).toBe(200); | ||
| expect(response.headers.get("Cache-Control")).toBe("private, no-store"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,3 @@ | ||
| import type { NextRequest } from "next/server"; | ||
| import { NextResponse } from "next/server"; | ||
| import { getServerAuthSession } from "@/lib/server-auth-session"; | ||
| import { controlPlaneUserFetch } from "@/lib/control-plane"; | ||
| import { settingsProxy } from "@/lib/settings-proxy"; | ||
|
|
||
| export async function GET() { | ||
| const session = await getServerAuthSession(); | ||
| if (!session?.user) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| try { | ||
| const response = await controlPlaneUserFetch("/model-preferences"); | ||
| const data = await response.json(); | ||
| return NextResponse.json(data, { status: response.status }); | ||
| } catch (error) { | ||
| console.error("Failed to fetch model preferences:", error); | ||
| return NextResponse.json({ error: "Failed to fetch model preferences" }, { status: 500 }); | ||
| } | ||
| } | ||
|
|
||
| export async function PUT(request: NextRequest) { | ||
| const session = await getServerAuthSession(); | ||
| if (!session?.user) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| try { | ||
| const body = await request.json(); | ||
|
|
||
| const response = await controlPlaneUserFetch("/model-preferences", { | ||
| method: "PUT", | ||
| body: JSON.stringify(body), | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
| return NextResponse.json(data, { status: response.status }); | ||
| } catch (error) { | ||
| console.error("Failed to update model preferences:", error); | ||
| return NextResponse.json({ error: "Failed to update model preferences" }, { status: 500 }); | ||
| } | ||
| } | ||
| export const { GET, PUT } = settingsProxy(() => "/model-preferences", "model preferences"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-preferencesresponse.packages/web/src/app/api/model-preferences/route.tsparses the control-plane response and constructs a freshNextResponsewithout forwardingCache-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 withsettingsProxy(() => "/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.There was a problem hiding this comment.
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-preferencesis nowsettingsProxy(() => "/model-preferences", "model preferences"), androute.test.tsat that boundary asserts the private, no-store header on the read and the forwarded update.