Skip to content

Commit 776d8d7

Browse files
sahrizviclaude
andcommitted
fix: address round-3 cubic findings on #1274 — non-TTY OSC 8 leak, second URL-join duplicate
- `hyperlink()`: skip OSC 8 wrapping entirely (return bare sanitized text) when stdout isn't a TTY. Stdin can be a TTY (satisfying the handler's interactive-input check) while stdout is redirected to a file or piped — in that case no terminal is reading the bytes, so the previous "harmless when unrecognized" reasoning didn't hold: raw OSC 8 would land as literal junk in the captured output. `terminalSupportsHyperlinks()` already checked `isTTY` for the underline decision; this applies the same check before emitting any escape bytes at all. - `manageUrlFor`: delegate to `buildManageUrl` instead of its own copy of the pre-fix string-concatenation bug. Two near-identical URL builders in the same file had drifted — the first round's fix only touched `currentManageUrl`'s construction and missed this second one, used by the browser-handoff and create-flow "Manage it at:" lines. - Declined (with reasoning in `buildManageUrl`'s comment): moving the URL-join helper to a module shared with the TUI side. Contradicts this codebase's existing, documented CLI/TUI self-containment convention (same rationale as `isSafeHttpUrl`'s deliberate duplication) — the fix is consolidating within this file, not spanning the boundary. - Updated `link.test.ts` for `hyperlink()`'s new non-TTY early return, and added a test isolating "TTY but unrecognized terminal" (OSC 8, no underline) from "not a TTY at all" (no escape bytes whatsoever) — the previous tests conflated the two via a shared `isTTY: false` setup. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01JsVwcBQP6VUwqEej3vK2zG
1 parent 789ff74 commit 776d8d7

2 files changed

Lines changed: 48 additions & 13 deletions

File tree

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,18 @@ export function terminalSupportsHyperlinks(): boolean {
9393
* ``ALTIMATE_WORKSPACE_WEB_URL`` override (resolveWorkspaceWebUrl) can carry
9494
* its own path/query/fragment (e.g. a local dev server), and naive
9595
* concatenation would land ``/w/<id>`` inside the query string instead of the
96-
* path — clears search/hash for the same reason. (CodeRabbit + cubic, PR #1274.) */
96+
* path — clears search/hash for the same reason. (CodeRabbit + cubic, PR #1274.)
97+
*
98+
* Both in-file callers (``currentManageUrl`` in the handler, and
99+
* ``manageUrlFor`` below) now go through this one implementation. The
100+
* identical ``workspace.tsx``/``workspace-sidebar.tsx`` copy
101+
* (``joinManageUrlPath``) is deliberately NOT unified with this one across a
102+
* shared module, though: same
103+
* CLI/TUI self-containment reasoning as ``isSafeHttpUrl``'s split (see
104+
* ``cli/cmd/link.ts``'s other comment on that). cubic suggested a shared
105+
* module (PR #1274 round 3); declined for that reason — but *within* this
106+
* file, keep it to one implementation (see ``manageUrlFor``'s comment on
107+
* why two near-identical copies in the same file already drifted once). */
97108
export function buildManageUrl(base: URL, workspaceId: number): string {
98109
const u = new URL(base)
99110
u.pathname = `${u.pathname.replace(/\/+$/, "")}/w/${workspaceId}`
@@ -105,17 +116,23 @@ export function buildManageUrl(base: URL, workspaceId: number): string {
105116
/** Wrap ``text`` in an OSC 8 terminal hyperlink pointing at ``url``, or return
106117
* ``text`` unchanged when ``url`` is null. Unlike the TUI's `<a href>` (which
107118
* crashes in the current @opentui/solid JSX layer — see workspace-sidebar.tsx),
108-
* plain stdout can emit OSC 8 directly: supporting terminals render it as a
109-
* real clickable link, and terminals that don't recognize the sequence just
110-
* skip the invisible control bytes — the visible text is unaffected either
111-
* way, so the OSC 8 wrapping itself needs no capability check. The
112-
* *underline*, however, is a much older and more universally-rendered SGR
113-
* code — emitting it unconditionally would make the name look clickable in
114-
* terminals where it isn't, so it's gated on ``terminalSupportsHyperlinks``
115-
* (cubic, PR #1274). */
119+
* plain stdout can emit OSC 8 directly: a terminal directly interpreting the
120+
* bytes either renders a real clickable link or silently skips the sequence
121+
* it doesn't recognize — the visible text is unaffected either way. That
122+
* "harmless when unrecognized" argument only holds when a terminal emulator
123+
* is actually the one reading the bytes, though: with stdout redirected to a
124+
* file or piped into another program (stdin can still be a TTY — the
125+
* interactive-stdin check in the handler doesn't imply stdout is a terminal
126+
* too), there's no interpreter to skip them, so the raw escape sequence
127+
* would land as literal junk in the captured output. Skip the OSC 8 wrapping
128+
* entirely in that case. The *underline* is additionally gated on
129+
* ``terminalSupportsHyperlinks`` — a much older and more universally-rendered
130+
* SGR code than OSC 8, so emitting it unconditionally would make the name
131+
* look clickable in terminals where it isn't. (cubic, PR #1274, rounds 2 + 3.) */
116132
export function hyperlink(text: string, url: string | null): string {
117133
if (!url || !text) return text
118134
const safeText = stripControlChars(text)
135+
if (!process.stdout.isTTY) return safeText
119136
const OSC8 = "\x1b]8;;"
120137
const ST = "\x1b\\"
121138
if (!terminalSupportsHyperlinks()) return `${OSC8}${url}${ST}${safeText}${OSC8}${ST}`
@@ -382,13 +399,17 @@ async function runBrowserHandoff(
382399
}
383400

384401
/** Best-effort manage-workspace URL for the current credentials. Returns null
385-
* on BYOK / unresolvable deployments — callers omit the "Manage it at" line. */
402+
* on BYOK / unresolvable deployments — callers omit the "Manage it at" line.
403+
* Delegates the actual join to ``buildManageUrl`` rather than re-deriving it —
404+
* this function had its own copy of the pre-fix string-concatenation bug
405+
* (cubic, PR #1274 round 3): two near-identical builders in the same file
406+
* drifted, and only one got fixed the first time around. */
386407
async function manageUrlFor(workspaceId: number): Promise<string | null> {
387408
try {
388409
const creds = await AltimateApi.getCredentials()
389410
const base = resolveWorkspaceWebUrl(creds.altimateUrl, creds.altimateInstanceName)
390411
if (!base) return null
391-
return `${base.toString().replace(/\/$/, "")}/w/${workspaceId}`
412+
return buildManageUrl(base, workspaceId)
392413
} catch {
393414
return null
394415
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,21 @@ describe("hyperlink", () => {
137137
expect(hyperlink("anas-skill-test", null)).toBe("anas-skill-test")
138138
})
139139

140-
test("wraps text in OSC 8 with no underline when the terminal isn't recognized", () => {
140+
test("returns bare sanitized text with no escape bytes at all when stdout isn't a TTY", () => {
141+
// Even with a recognized TERM_PROGRAM — stdin can be a TTY (satisfying
142+
// the handler's interactive-input check) while stdout is redirected to
143+
// a file or piped, in which case no terminal is reading these bytes and
144+
// raw OSC 8 would land as literal junk in the captured output.
141145
Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true })
146+
process.env.TERM_PROGRAM = "iTerm.app"
147+
const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242")
148+
expect(out).toBe("anas-skill-test")
149+
expect(out).not.toContain("\x1b")
150+
})
151+
152+
test("wraps text in OSC 8 with no underline on a TTY whose terminal isn't recognized", () => {
153+
Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true })
154+
delete process.env.TERM_PROGRAM
142155
const out = hyperlink("anas-skill-test", "https://tenant.ws.myaltimate.com/w/4242")
143156
expect(out).toBe("\x1b]8;;https://tenant.ws.myaltimate.com/w/4242\x1b\\anas-skill-test\x1b]8;;\x1b\\")
144157
expect(out).not.toContain("\x1b[4m")
@@ -154,7 +167,8 @@ describe("hyperlink", () => {
154167
})
155168

156169
test("sanitizes an adversarial name so it cannot open a second, spoofed link", () => {
157-
Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true })
170+
Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true })
171+
delete process.env.TERM_PROGRAM
158172
const malicious = "name\x1b]8;;http://evil.example\x1b\\CLICK ME\x1b]8;;\x1b\\"
159173
const out = hyperlink(malicious, "https://tenant.ws.myaltimate.com/w/4242")
160174
// Exactly one real OSC 8 open + one real OSC 8 close — the malicious

0 commit comments

Comments
 (0)