Skip to content

Commit a6f592b

Browse files
committed
fix(workspace): address harness-bot round 8 — Host header, close sockets, pin marker semantics
Three findings on the browser-handoff + sidebar surfaces: 1. DNS-rebinding guard on the callback listener. Binding to 127.0.0.1 is necessary but not sufficient: a malicious page whose hostname resolves to 127.0.0.1 can drive the browser to attacker.com:7317/workspace-bound and the socket lands on our listener with Host: attacker.com. State validation catches it eventually, but rejecting the request on Host mismatch kills the attack before touching state. (altimate-harness-bot #1100 comment 3837907679.) 2. server.close() leaves keep-alive sockets open. Follow every close() with server.closeAllConnections?.() (Node 18.2+, safe with the optional-call guard). (altimate-harness-bot #1100 comment 3837907954.) 3. Sidebar `(pinned via --workspace)` label — clarified semantics with a code comment (option b of the review). Known imprecision is accepted; getResolvedWorkspaceId() already encodes "was passed AND resolved" at the env-var level, so the pin never falsely appears for a session that wasn't launched with --workspace. (altimate-harness-bot #1100 comment 3837908331.)
1 parent fe55cf7 commit a6f592b

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎packages/opencode/src/altimate/workspace/browser-handoff.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,19 @@ function markReason<E extends Error>(err: E, reason: HandoffFailureReason): E &
201201
async function startListener(pending: HandoffPending): Promise<{ server: Server; port: number }> {
202202
const server = createServer((req, res) => {
203203
const port = (server.address() as { port?: number } | null)?.port ?? CALLBACK_PORT_MIN
204+
// DNS-rebinding guard — bind to 127.0.0.1 is not enough on its own. A
205+
// malicious page can point its own hostname at 127.0.0.1, drive the
206+
// browser to ``http://attacker.com:7317/workspace-bound?state=...&
207+
// workspace_id=...``, and the connection lands here. The Host header
208+
// it sends is ``attacker.com``, so refusing anything except our own
209+
// loopback authorities kills the attack before state validation runs.
210+
// (altimate-harness-bot round 8.)
211+
const reqHost = (req.headers.host ?? "").toLowerCase()
212+
if (reqHost !== `127.0.0.1:${port}` && reqHost !== `localhost:${port}`) {
213+
res.writeHead(400)
214+
res.end("Bad request")
215+
return
216+
}
204217
const url = new URL(req.url || "/", `http://127.0.0.1:${port}`)
205218
if (url.pathname !== "/workspace-bound") {
206219
res.writeHead(404)
@@ -333,6 +346,12 @@ async function startListener(pending: HandoffPending): Promise<{ server: Server;
333346
}
334347

335348
server.close()
349+
// server.close() only stops accepting new connections — any keep-alive
350+
// socket still open in the port-exhaustion / squatter case keeps the
351+
// port bound until the peer drops. Force-terminate here so the next
352+
// ``altimate-code link`` doesn't skip this port needlessly. Guard with
353+
// ``?.()`` for Node <18.2. (altimate-harness-bot round 8.)
354+
server.closeAllConnections?.()
336355
const code = lastErr?.code
337356
throw markReason(
338357
new Error(
@@ -409,6 +428,11 @@ export async function runHandoffWithOpener(
409428
if (listenerHandle) {
410429
try {
411430
listenerHandle.server.close()
431+
// Force-terminate lingering keep-alive sockets — otherwise the
432+
// port can stay bound for minutes after abort/timeout and the
433+
// next ``altimate-code link`` walks past it needlessly. Guard
434+
// with ``?.()`` for Node <18.2. (altimate-harness-bot round 8.)
435+
listenerHandle.server.closeAllConnections?.()
412436
} catch {
413437
/* best effort */
414438
}

‎packages/opencode/src/plugin/tui/altimate/workspace-sidebar.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ function View(props: { api: TuiPluginApi }) {
9999
<>
100100
<text fg={theme().textMuted}>
101101
{b().datamateName}
102+
{/* ``pinned via --workspace`` means "this SESSION was launched
103+
* with --workspace and it resolved to this id". It does NOT
104+
* mean "the current binding was set by --workspace" — if the
105+
* user relinks mid-session to a different workspace, the pin
106+
* disappears (id mismatch); if they relink to the same id,
107+
* the pin correctly stays because the launch fact is
108+
* unchanged. Known imprecision: relink-to-same-id looks
109+
* indistinguishable from "never relinked". Accepted per
110+
* altimate-harness-bot round 8 (option b of the review).
111+
* ``getResolvedWorkspaceId`` returns null when the launch
112+
* had no --workspace flag or the flag failed to resolve,
113+
* so the pin never falsely appears for a session that
114+
* wasn't launched with the flag. */}
102115
<Show when={getResolvedWorkspaceId() === b().datamateId}>
103116
{" (pinned via --workspace)"}
104117
</Show>

0 commit comments

Comments
 (0)