Skip to content

Commit 46c2895

Browse files
saravmajesticclaude
andcommitted
fix(workspace): reserve 409 for the pilot gate; classify session lookup failures
- A session from another directory is a bad request (400), so 409 means only "workspace mode is off" and a caller can act on the status alone. - `Session.get` validates synchronously: defer it into the promise chain so a malformed id is answered instead of escaping the route, and report a lookup failure that is not a validation error as a logged 500. - `workspaceRouteRefusal` takes the password as a parameter (defaulting to the flag) so the password-set origin policy is covered by tests. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8af7760 commit 46c2895

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

‎packages/opencode/src/server/server.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,24 @@ export namespace Server {
7777
const log = Log.create({ service: "server" })
7878
// altimate_change start — shared gate for the `/altimate/workspace/*` routes
7979
/** Why a `/workspace` action must not run, or undefined when it may.
80+
*
81+
* 409 is reserved for the pilot gate, so a caller can tell "this server is not in workspace mode"
82+
* apart from a bad request (400) without parsing the message.
8083
*
8184
* Outside the workspace pilot a skill sync purges the snapshot, so the flag is checked first.
8285
* A browser origin on an unsecured server is refused for the same reason as Altimate Base
8386
* registration: a CORS-allowed page is not a local process. Native clients (the extension host,
8487
* curl) send no Origin. With a server password set, a same-origin page may call; others may not. */
85-
function workspaceRouteRefusal(
88+
export function workspaceRouteRefusal(
8689
origin: string | undefined,
8790
host: string | undefined,
91+
password: string | undefined = Flag.OPENCODE_SERVER_PASSWORD,
8892
): { status: 403 | 409; body: { ok: false; error: string } } | undefined {
8993
if (!CoreFlag.ALTIMATE_WORKSPACE) {
9094
return { status: 409, body: { ok: false, error: "Workspace mode is not enabled for this server." } }
9195
}
9296
if (!origin) return undefined
93-
if (!Flag.OPENCODE_SERVER_PASSWORD) {
97+
if (!password) {
9498
log.warn("refused browser-originated workspace action on an unsecured server", { origin })
9599
return {
96100
status: 403,
@@ -1031,15 +1035,23 @@ export namespace Server {
10311035
// The memory reload loads THIS directory's workspace memory into the named session, so the
10321036
// session must be one of this directory's; another project's would receive it.
10331037
if (sessionID) {
1034-
const session = await Session.get(sessionID as never).catch((err) => err as Error)
1038+
// `Session.get` validates the id synchronously, so the call is deferred into the promise
1039+
// chain for a malformed id to land in the handler below rather than escape the route.
1040+
const session = await Promise.resolve()
1041+
.then(() => Session.get(sessionID as never))
1042+
.catch((err) => err as Error)
10351043
if (session instanceof NotFoundError) {
10361044
return c.json({ ok: false, error: `Session not found: ${sessionID}` }, 404)
10371045
}
1038-
if (session instanceof Error) {
1046+
if (session instanceof z.ZodError) {
10391047
return c.json({ ok: false, error: `Invalid sessionID: ${sessionID}` }, 400)
10401048
}
1049+
if (session instanceof Error) {
1050+
log.error("workspace refresh: session lookup failed", { error: session.message })
1051+
return c.json({ ok: false, error: session.message }, 500)
1052+
}
10411053
if (nodePath.resolve(session.directory) !== nodePath.resolve(Instance.directory)) {
1042-
return c.json({ ok: false, error: "That session belongs to a different project directory." }, 409)
1054+
return c.json({ ok: false, error: "That session belongs to a different project directory." }, 400)
10431055
}
10441056
}
10451057
try {

‎packages/opencode/test/server/altimate-workspace-routes.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,28 @@ describe("POST /altimate/workspace/refresh", () => {
6161
spies.push(refresh)
6262

6363
const response = await post("/altimate/workspace/refresh", { sessionID: "ses_123" })
64-
expect(response.status).toBe(409)
64+
expect(response.status).toBe(400)
65+
expect(refresh).not.toHaveBeenCalled()
66+
})
67+
68+
test("reports a failed session lookup as a 500, not as a bad sessionID", async () => {
69+
spies.push(spyOn(Session, "get").mockRejectedValue(new Error("database is locked")))
70+
const refresh = spyOn(Manage, "refresh")
71+
spies.push(refresh)
72+
73+
const response = await post("/altimate/workspace/refresh", { sessionID: "ses_123" })
74+
expect(response.status).toBe(500)
75+
expect(await response.json()).toEqual({ ok: false, error: "database is locked" })
76+
expect(refresh).not.toHaveBeenCalled()
77+
})
78+
79+
test("an arbitrary session id is looked up for real and answered, never escaping the route", async () => {
80+
const refresh = spyOn(Manage, "refresh")
81+
spies.push(refresh)
82+
83+
const response = await post("/altimate/workspace/refresh", { sessionID: "not-a-session" })
84+
expect(response.status).toBe(404)
85+
expect(((await response.json()) as Record<string, unknown>).ok).toBe(false)
6586
expect(refresh).not.toHaveBeenCalled()
6687
})
6788

@@ -200,6 +221,23 @@ describe("POST /altimate/workspace/sync", () => {
200221
})
201222
})
202223

224+
describe("origin policy with a server password set", () => {
225+
// The password flag is read once at module load, so the policy is exercised directly with one.
226+
test("lets a native client (no Origin) and this server's own page through", () => {
227+
expect(Server.workspaceRouteRefusal(undefined, "127.0.0.1:4096", "pw")).toBeUndefined()
228+
expect(Server.workspaceRouteRefusal("http://127.0.0.1:4096", "127.0.0.1:4096", "pw")).toBeUndefined()
229+
})
230+
231+
test("refuses another origin even though Basic credentials would be replayed", () => {
232+
expect(Server.workspaceRouteRefusal("https://evil.test", "127.0.0.1:4096", "pw")?.status).toBe(403)
233+
})
234+
235+
test("refuses every origin when no password is set", () => {
236+
expect(Server.workspaceRouteRefusal("http://127.0.0.1:4096", "127.0.0.1:4096", undefined)?.status).toBe(403)
237+
expect(Server.workspaceRouteRefusal(undefined, "127.0.0.1:4096", undefined)).toBeUndefined()
238+
})
239+
})
240+
203241
describe("same-origin check used when a server password is set", () => {
204242
test("accepts this server's own pages only", () => {
205243
expect(Server.sameOrigin("http://127.0.0.1:4096", "127.0.0.1:4096")).toBe(true)

0 commit comments

Comments
 (0)