Skip to content

Commit 6fb1c43

Browse files
sahrizviclaude
andcommitted
fix: address bot review findings on #1274 — control-char injection, URL join, dead export
- `cli/cmd/link.ts`: sanitize workspace names (strip C0/C1 control bytes, including ESC) before they reach the OSC 8 hyperlink wrapper — an attacker/account-controlled workspace name containing its own `\x1b]8;;` could otherwise prematurely close our hyperlink and open a spoofed one with our trusted URL as inert visible prefix text. (CodeRabbit + cubic) - `cli/cmd/link.ts`: only emit the underline visual affordance when the terminal is one we're reasonably confident renders OSC 8 (conservative TERM_PROGRAM/VTE/Konsole/Windows Terminal allowlist) — SGR underline is far more universally rendered than OSC 8 itself, so emitting it unconditionally made names look clickable in terminals where they weren't. (cubic) - `cli/cmd/link.ts` + `workspace.tsx` + `workspace-sidebar.tsx`: build the manage URL via real `URL` pathname/search/hash manipulation instead of string-concatenating `toString()` — the dev-only `ALTIMATE_WORKSPACE_WEB_URL` override can carry its own path/query, and naive concatenation landed `/w/<id>` inside the query string instead of the path. Fixed in all three occurrences of the same pattern, not just the one CodeRabbit/cubic flagged in link.ts. (CodeRabbit + cubic) - `workspace.tsx`: drop the `export` on `isSafeHttpUrl` — no external importers; `link.ts` deliberately keeps its own private copy. (Kilo) Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JsVwcBQP6VUwqEej3vK2zG
1 parent bb9e666 commit 6fb1c43

3 files changed

Lines changed: 89 additions & 25 deletions

File tree

‎packages/opencode/src/cli/cmd/link.ts‎

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,76 @@ import { recordApprovedBinding } from "@/altimate/workspace/state"
4242
const CREATE_NEW_SENTINEL = "__create_new__"
4343
const SET_UP_IN_BROWSER_SENTINEL = "__browser_handoff__"
4444

45+
/** Strip C0/C1 control bytes (including ESC) from server-controlled text
46+
* before it reaches a raw-stdout escape-sequence wrapper. Workspace names
47+
* come from ``WorkspaceApi.listDatamates()`` with no charset validation — an
48+
* attacker-controlled name containing its own ``\x1b]8;;`` could otherwise
49+
* prematurely close our hyperlink and open a spoofed one pointing wherever
50+
* they choose, with our trusted URL as the visible (but inert) prefix.
51+
* (CodeRabbit + cubic, PR #1274.) */
52+
function stripControlChars(text: string): string {
53+
// eslint-disable-next-line no-control-regex
54+
return text.replace(/[\x00-\x1f\x7f]/g, "")
55+
}
56+
57+
/** Conservative allowlist of terminals known to render OSC 8 hyperlinks.
58+
* There's no capability query as reliable as opentui's device-attribute
59+
* detection (used by the TUI side) available to a plain CLI process, so this
60+
* errs toward false negatives — worst case a supporting terminal renders
61+
* plain text instead of a link, which is a strict improvement over the
62+
* inverse (underlining text that turns out not to be clickable). Mirrors the
63+
* checks the `supports-hyperlinks` package uses, inlined to avoid a new
64+
* dependency for one CLI affordance. */
65+
function terminalSupportsHyperlinks(): boolean {
66+
if (!process.stdout.isTTY) return false
67+
if (process.env.TERM === "dumb" || process.env.TERM === "linux") return false
68+
const termProgram = process.env.TERM_PROGRAM
69+
if (
70+
termProgram &&
71+
["iTerm.app", "WezTerm", "Hyper", "vscode", "ghostty", "Tabby", "rio", "Apple_Terminal"].includes(termProgram)
72+
)
73+
return true
74+
if (process.env.WT_SESSION) return true // Windows Terminal
75+
if (process.env.KONSOLE_VERSION) return true
76+
const vte = Number(process.env.VTE_VERSION)
77+
if (!Number.isNaN(vte) && vte >= 5000) return true // VTE >= 0.50.0 (GNOME Terminal and other VTE-based terms)
78+
return false
79+
}
80+
81+
/** Append ``/w/<id>`` to ``base``'s pathname using real URL semantics, rather
82+
* than string-concatenating ``toString()``. The dev-only
83+
* ``ALTIMATE_WORKSPACE_WEB_URL`` override (resolveWorkspaceWebUrl) can carry
84+
* its own path/query/fragment (e.g. a local dev server), and naive
85+
* concatenation would land ``/w/<id>`` inside the query string instead of the
86+
* path — clears search/hash for the same reason. (CodeRabbit + cubic, PR #1274.) */
87+
function buildManageUrl(base: URL, workspaceId: number): string {
88+
const u = new URL(base)
89+
u.pathname = `${u.pathname.replace(/\/+$/, "")}/w/${workspaceId}`
90+
u.search = ""
91+
u.hash = ""
92+
return u.toString()
93+
}
94+
4595
/** Wrap ``text`` in an OSC 8 terminal hyperlink pointing at ``url``, or return
4696
* ``text`` unchanged when ``url`` is null. Unlike the TUI's `<a href>` (which
4797
* crashes in the current @opentui/solid JSX layer — see workspace-sidebar.tsx),
48-
* plain stdout can emit OSC 8 directly: supporting terminals (iTerm2, Ghostty,
49-
* kitty, Windows Terminal, ...) render it as a real clickable link, and
50-
* terminals that don't recognize the sequence just skip the invisible control
51-
* bytes — the visible text is unaffected either way, so no capability check
52-
* is needed before emitting it. */
98+
* plain stdout can emit OSC 8 directly: supporting terminals render it as a
99+
* real clickable link, and terminals that don't recognize the sequence just
100+
* skip the invisible control bytes — the visible text is unaffected either
101+
* way, so the OSC 8 wrapping itself needs no capability check. The
102+
* *underline*, however, is a much older and more universally-rendered SGR
103+
* code — emitting it unconditionally would make the name look clickable in
104+
* terminals where it isn't, so it's gated on ``terminalSupportsHyperlinks``
105+
* (cubic, PR #1274). */
53106
function hyperlink(text: string, url: string | null): string {
54107
if (!url) return text
108+
const safeText = stripControlChars(text)
55109
const OSC8 = "\x1b]8;;"
56110
const ST = "\x1b\\"
57-
// Underline as a visual affordance that this text is clickable — OSC 8
58-
// alone carries no default styling. ``\x1b[24m`` (underline-off only, not
59-
// a full ``\x1b[0m`` reset) so it doesn't clobber a color clack already
60-
// applied around the whole line (e.g. the dim wrapper on a submitted value).
111+
if (!terminalSupportsHyperlinks()) return `${OSC8}${url}${ST}${safeText}${OSC8}${ST}`
61112
const UNDERLINE = "\x1b[4m"
62113
const UNDERLINE_OFF = "\x1b[24m"
63-
return `${OSC8}${url}${ST}${UNDERLINE}${text}${UNDERLINE_OFF}${OSC8}${ST}`
114+
return `${OSC8}${url}${ST}${UNDERLINE}${safeText}${UNDERLINE_OFF}${OSC8}${ST}`
64115
}
65116

66117
export const LinkCommand = cmd({
@@ -155,9 +206,7 @@ export const LinkCommand = cmd({
155206
// buildManageUrl (workspace.tsx) — null on BYOK/unresolvable, in which
156207
// case the name below prints as plain (non-clickable) text.
157208
const currentManageUrl =
158-
currentId !== undefined && workspaceWebBase
159-
? `${workspaceWebBase.toString().replace(/\/$/, "")}/w/${currentId}`
160-
: null
209+
currentId !== undefined && workspaceWebBase ? buildManageUrl(workspaceWebBase, currentId) : null
161210

162211
const options: Array<{ value: string; label: string; hint?: string }> = [
163212
// Only offer browser handoff for UNLINKED projects (CodeRabbit cycle 5).

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { readLocalBinding, type CachedBinding } from "@/altimate/workspace/state
1313
import { resolveWorkspaceWebUrl } from "@/altimate/workspace/browser-handoff"
1414
import { getResolvedWorkspaceId } from "@/altimate/workspace/session-context"
1515
import { AltimateApi } from "@/altimate/api/client"
16-
import { openManageUrl } from "./workspace"
16+
import { openManageUrl, joinManageUrlPath } from "./workspace"
1717

1818
const id = "altimate:sidebar-workspace"
1919

@@ -31,8 +31,8 @@ const POLL_MS = 30_000
3131
* base per (apiUrl, tenant) pair for the life of the process; if the file
3232
* changes mid-session, the binding cache invalidation (in state.ts) still
3333
* catches it via its own (tenant, apiUrl) top-level scoping. */
34-
let cachedManageBase: { apiUrl: string; tenant: string; base: string | null } | null = null
35-
async function resolveManageBase(): Promise<string | null> {
34+
let cachedManageBase: { apiUrl: string; tenant: string; base: URL | null } | null = null
35+
async function resolveManageBase(): Promise<URL | null> {
3636
try {
3737
const creds = await AltimateApi.getCredentials()
3838
if (
@@ -42,8 +42,7 @@ async function resolveManageBase(): Promise<string | null> {
4242
) {
4343
return cachedManageBase.base
4444
}
45-
const url = resolveWorkspaceWebUrl(creds.altimateUrl, creds.altimateInstanceName)
46-
const base = url ? url.toString().replace(/\/$/, "") : null
45+
const base = resolveWorkspaceWebUrl(creds.altimateUrl, creds.altimateInstanceName)
4746
cachedManageBase = { apiUrl: creds.altimateUrl, tenant: creds.altimateInstanceName, base }
4847
return base
4948
} catch {
@@ -69,7 +68,7 @@ function View(props: { api: TuiPluginApi }) {
6968
return
7069
}
7170
const base = await resolveManageBase()
72-
setManageUrl(base ? `${base}/w/${b.datamateId}` : null)
71+
setManageUrl(base ? joinManageUrlPath(base, b.datamateId) : null)
7372
} finally {
7473
refreshInFlight = false
7574
}

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ function OfferDialog(props: OfferProps) {
241241
)
242242
}
243243

244+
/** Append ``/w/<id>`` to ``base``'s pathname using real URL semantics, rather
245+
* than string-concatenating ``toString()``. The dev-only
246+
* ``ALTIMATE_WORKSPACE_WEB_URL`` override (resolveWorkspaceWebUrl) can carry
247+
* its own path/query/fragment (e.g. a local dev server), and naive
248+
* concatenation would land ``/w/<id>`` inside the query string instead of the
249+
* path — clears search/hash for the same reason. (CodeRabbit + cubic on
250+
* PR #1274's identical bug in cli/cmd/link.ts.) */
251+
export function joinManageUrlPath(base: URL, workspaceId: number): string {
252+
const u = new URL(base)
253+
u.pathname = `${u.pathname.replace(/\/+$/, "")}/w/${workspaceId}`
254+
u.search = ""
255+
u.hash = ""
256+
return u.toString()
257+
}
258+
244259
/** Build the SaaS manage-workspace URL for a bound workspace. Deterministic
245260
* from tenant + id, so any caller can construct it without an extra round-trip.
246261
* Returns null when the current deployment isn't the freemium web (BYOK or
@@ -250,7 +265,7 @@ async function buildManageUrl(workspaceId: number): Promise<string | null> {
250265
const creds = await AltimateApi.getCredentials()
251266
const base = resolveWorkspaceWebUrl(creds.altimateUrl, creds.altimateInstanceName)
252267
if (!base) return null
253-
return `${base.toString().replace(/\/$/, "")}/w/${workspaceId}`
268+
return joinManageUrlPath(base, workspaceId)
254269
} catch {
255270
return null
256271
}
@@ -563,11 +578,12 @@ async function createAndBindInline(
563578

564579
/** True when the URL parses and its protocol is exactly ``http:`` or ``https:``.
565580
* Used before handing a server-supplied URL to ``open()`` (which would otherwise
566-
* dispatch to whatever OS scheme handler matches the protocol). Kept exported
567-
* as a top-level helper — ``openManageUrl`` below is the sole in-module caller,
568-
* but ``cli/cmd/link.ts`` deliberately keeps its own copy (CLI/TUI split, see
569-
* that file's comment) rather than importing this one. */
570-
export function isSafeHttpUrl(url: string): boolean {
581+
* dispatch to whatever OS scheme handler matches the protocol). Not exported —
582+
* ``openManageUrl`` below is the sole caller; ``cli/cmd/link.ts`` deliberately
583+
* keeps its own private copy (CLI/TUI split, see that file's comment) rather
584+
* than importing this one. (Kilo, PR #1274 — the prior `export` had no
585+
* external importers.) */
586+
function isSafeHttpUrl(url: string): boolean {
571587
try {
572588
const u = new URL(url)
573589
return u.protocol === "http:" || u.protocol === "https:"

0 commit comments

Comments
 (0)