|
| 1 | +/** |
| 2 | + * Adversarial coverage for the v0.11.2 patch payload (v0.11.1..HEAD, 2 squash-merged PRs). |
| 3 | + * |
| 4 | + * 1. #1302 — Altimate Base is offered to every user on an implicit free Zen default (closes #1301). |
| 5 | + * The TUI side is covered by packages/tui tests (local, cycle-stability, ready-pending, |
| 6 | + * history-startup-race, dialog-altimate-base). The server side lives in |
| 7 | + * `Provider.readDefaultModelState()` / `Provider.defaultModel()` and the ACP default scan, covered |
| 8 | + * by `test/provider/provider.test.ts` and `test/acp/default-model.test.ts` for the happy shapes |
| 9 | + * (public tier vs keyed Zen vs registered Base, recents precedence, decline honoured). |
| 10 | + * 2. #1274 — clickable workspace names: `stripControlChars` / `hyperlink` / |
| 11 | + * `terminalSupportsHyperlinks` in `src/cli/cmd/link.ts`, covered by `test/cli/cmd/link.test.ts`. |
| 12 | + * |
| 13 | + * This file adds the boundary classes those suites do not reach: |
| 14 | + * |
| 15 | + * - `readDefaultModelState()` against a HOSTILE model.json: not-an-object JSON, `recent` that is an |
| 16 | + * object / string / null, recent entries missing fields or with non-string fields, prototype-key |
| 17 | + * provider/model ids, a `declinedManagedBaseDefault` that is truthy-but-not-`true` ("yes", 1, {}), |
| 18 | + * and a file that is not JSON at all. The contract is: never throw, never return a malformed |
| 19 | + * entry, and treat anything but literal `true` as "not declined" (a decline is an explicit act; |
| 20 | + * garbage must not manufacture one, and must not silently erase one either — see the last test). |
| 21 | + * - `stripControlChars` idempotence and the full C0/DEL/C1/bidi ranges by exhaustive scan (not the |
| 22 | + * handful of sampled bytes the main suite uses), plus the guarantee that ordinary Unicode |
| 23 | + * (CJK, combining marks, emoji, RTL letters themselves) survives. |
| 24 | + * - `hyperlink` with a `url` that parses but carries control bytes at every position (start, middle, |
| 25 | + * end, inside the query), with non-http(s) schemes that `new URL()` accepts, with an `http:` |
| 26 | + * scheme (allowed), and with a `text` made ENTIRELY of control bytes (the sanitized text is empty, |
| 27 | + * and the function must still return a string, never throw). |
| 28 | + * - `terminalSupportsHyperlinks` under adversarial env: `VTE_VERSION` that is empty, negative, |
| 29 | + * `Infinity`, or exactly the 5000 boundary; `TERM_PROGRAM` with a lookalike casing; and the |
| 30 | + * precedence of `TERM=dumb` over every positive signal. |
| 31 | + * |
| 32 | + * Rules: no `mock.module()`; env is saved/restored per test; the real state dir is never touched |
| 33 | + * (`withTestStateHome`). |
| 34 | + */ |
| 35 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 36 | +import fs from "node:fs/promises" |
| 37 | +import path from "node:path" |
| 38 | +import { Global } from "@/global" |
| 39 | +import { Provider } from "@/provider/provider" |
| 40 | +import { hyperlink, stripControlChars, terminalSupportsHyperlinks } from "@/cli/cmd/link" |
| 41 | +import { withTestStateHome } from "../fixture/fixture" |
| 42 | + |
| 43 | +async function writeModelJson(raw: string) { |
| 44 | + await fs.mkdir(Global.Path.state, { recursive: true }) |
| 45 | + await fs.writeFile(path.join(Global.Path.state, "model.json"), raw) |
| 46 | +} |
| 47 | + |
| 48 | +describe("v0.11.2 adversarial: Provider.readDefaultModelState against a hostile model.json", () => { |
| 49 | + test("non-object JSON roots yield an empty, not-declined state without throwing", async () => { |
| 50 | + for (const raw of ["null", "42", '"string"', "true", "[]", "[1,2,3]"]) { |
| 51 | + await withTestStateHome(async () => { |
| 52 | + await writeModelJson(raw) |
| 53 | + const state = await Provider.readDefaultModelState() |
| 54 | + expect(state).toEqual({ recent: [], declinedManagedBaseDefault: false }) |
| 55 | + }) |
| 56 | + } |
| 57 | + }) |
| 58 | + |
| 59 | + test("a file that is not JSON at all is treated as absent", async () => { |
| 60 | + await withTestStateHome(async () => { |
| 61 | + await writeModelJson("{ this is not json") |
| 62 | + expect(await Provider.readDefaultModelState()).toEqual({ recent: [], declinedManagedBaseDefault: false }) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + test("`recent` that is not an array is ignored, whatever its type", async () => { |
| 67 | + for (const recent of ['"opencode/big-pickle"', "null", "{}", '{"providerID":"opencode","modelID":"x"}', "7"]) { |
| 68 | + await withTestStateHome(async () => { |
| 69 | + await writeModelJson(`{"recent": ${recent}}`) |
| 70 | + expect((await Provider.readDefaultModelState()).recent).toEqual([]) |
| 71 | + }) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + test("malformed recent entries are dropped individually; well-formed neighbours survive in order", async () => { |
| 76 | + await withTestStateHome(async () => { |
| 77 | + await writeModelJson( |
| 78 | + JSON.stringify({ |
| 79 | + recent: [ |
| 80 | + null, |
| 81 | + "opencode/big-pickle", |
| 82 | + { providerID: "opencode" }, |
| 83 | + { modelID: "big-pickle" }, |
| 84 | + { providerID: 1, modelID: "big-pickle" }, |
| 85 | + { providerID: "opencode", modelID: ["big-pickle"] }, |
| 86 | + { providerID: "anthropic", modelID: "claude-sonnet-5" }, |
| 87 | + { providerID: "", modelID: "" }, |
| 88 | + { providerID: "opencode", modelID: "big-pickle" }, |
| 89 | + ], |
| 90 | + }), |
| 91 | + ) |
| 92 | + const { recent } = await Provider.readDefaultModelState() |
| 93 | + // Every surviving entry has string ids; the two well-formed ones keep their relative order. |
| 94 | + for (const entry of recent) { |
| 95 | + expect(typeof entry.providerID).toBe("string") |
| 96 | + expect(typeof entry.modelID).toBe("string") |
| 97 | + } |
| 98 | + const survivors = recent.map((entry) => `${entry.providerID}/${entry.modelID}`) |
| 99 | + expect(survivors.indexOf("anthropic/claude-sonnet-5")).toBeGreaterThanOrEqual(0) |
| 100 | + expect(survivors.indexOf("opencode/big-pickle")).toBeGreaterThan(survivors.indexOf("anthropic/claude-sonnet-5")) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + test("prototype-key ids in recent are plain strings, not property lookups", async () => { |
| 105 | + await withTestStateHome(async () => { |
| 106 | + await writeModelJson( |
| 107 | + JSON.stringify({ recent: [{ providerID: "__proto__", modelID: "constructor" }, { providerID: "toString", modelID: "valueOf" }] }), |
| 108 | + ) |
| 109 | + const { recent } = await Provider.readDefaultModelState() |
| 110 | + // Reading the state must not resolve these against Object.prototype; it either keeps them as |
| 111 | + // inert strings or drops them. Either way nothing throws and nothing is a function. |
| 112 | + for (const entry of recent) { |
| 113 | + expect(typeof entry.providerID).toBe("string") |
| 114 | + expect(typeof entry.modelID).toBe("string") |
| 115 | + } |
| 116 | + expect(Object.getPrototypeOf(recent)).toBe(Array.prototype) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + test("only a literal `true` counts as a decline", async () => { |
| 121 | + for (const value of ['"true"', '"yes"', "1", "{}", "[]", '"declined"', "null", "0", "false"]) { |
| 122 | + await withTestStateHome(async () => { |
| 123 | + await writeModelJson(`{"recent": [], "declinedManagedBaseDefault": ${value}}`) |
| 124 | + expect((await Provider.readDefaultModelState()).declinedManagedBaseDefault).toBe(false) |
| 125 | + }) |
| 126 | + } |
| 127 | + await withTestStateHome(async () => { |
| 128 | + await writeModelJson(`{"recent": [], "declinedManagedBaseDefault": true}`) |
| 129 | + expect((await Provider.readDefaultModelState()).declinedManagedBaseDefault).toBe(true) |
| 130 | + }) |
| 131 | + }) |
| 132 | + |
| 133 | + test("a decline survives a hostile `recent` in the same file", async () => { |
| 134 | + // A corrupted recents array must not take the decline flag down with it: the two fields are |
| 135 | + // independent, and losing the decline would silently re-enrol a refusing user. |
| 136 | + await withTestStateHome(async () => { |
| 137 | + await writeModelJson(`{"recent": "garbage", "declinedManagedBaseDefault": true}`) |
| 138 | + expect(await Provider.readDefaultModelState()).toEqual({ recent: [], declinedManagedBaseDefault: true }) |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + test("readDefaultModelState reads the isolated test state dir, never the real one", async () => { |
| 143 | + const real = process.env.OPENCODE_TEST_STATE_HOME |
| 144 | + await withTestStateHome(async () => { |
| 145 | + expect(Global.Path.state).toBe(process.env.OPENCODE_TEST_STATE_HOME!) |
| 146 | + expect(Global.Path.state).not.toBe(real ?? "") |
| 147 | + }) |
| 148 | + }) |
| 149 | +}) |
| 150 | + |
| 151 | +describe("v0.11.2 adversarial: stripControlChars", () => { |
| 152 | + test("strips every C0, DEL, C1 and bidi control code point, by exhaustive scan", () => { |
| 153 | + const ranges: [number, number][] = [ |
| 154 | + [0x00, 0x1f], |
| 155 | + [0x7f, 0x9f], |
| 156 | + [0x200e, 0x200f], |
| 157 | + [0x202a, 0x202e], |
| 158 | + [0x2066, 0x2069], |
| 159 | + ] |
| 160 | + for (const [lo, hi] of ranges) { |
| 161 | + for (let cp = lo; cp <= hi; cp++) { |
| 162 | + const ch = String.fromCodePoint(cp) |
| 163 | + expect(stripControlChars(`a${ch}b`)).toBe("ab") |
| 164 | + } |
| 165 | + } |
| 166 | + }) |
| 167 | + |
| 168 | + test("is idempotent and preserves ordinary Unicode, including RTL letters and emoji", () => { |
| 169 | + const samples = ["données-équipe", "日本語ワークスペース", "עברית", "العربية", "🚀 launch", "é", "a\tb"] |
| 170 | + for (const s of samples) { |
| 171 | + const once = stripControlChars(s) |
| 172 | + expect(stripControlChars(once)).toBe(once) |
| 173 | + if (s !== "a\tb") expect(once).toBe(s) |
| 174 | + } |
| 175 | + expect(stripControlChars("a\tb")).toBe("ab") |
| 176 | + }) |
| 177 | + |
| 178 | + test("input made entirely of control bytes becomes the empty string", () => { |
| 179 | + expect(stripControlChars("\x1b\x9b\x7f\u202e\u2066\u200f")).toBe("") |
| 180 | + expect(stripControlChars("")).toBe("") |
| 181 | + }) |
| 182 | +}) |
| 183 | + |
| 184 | +describe("v0.11.2 adversarial: hyperlink", () => { |
| 185 | + const saved: Record<string, string | undefined> = {} |
| 186 | + const savedIsTTY = process.stdout.isTTY |
| 187 | + beforeEach(() => { |
| 188 | + for (const k of ["TERM", "TERM_PROGRAM", "WT_SESSION", "KONSOLE_VERSION", "VTE_VERSION"]) { |
| 189 | + saved[k] = process.env[k] |
| 190 | + delete process.env[k] |
| 191 | + } |
| 192 | + Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true }) |
| 193 | + process.env.TERM_PROGRAM = "iTerm.app" |
| 194 | + }) |
| 195 | + afterEach(() => { |
| 196 | + for (const [k, v] of Object.entries(saved)) { |
| 197 | + if (v === undefined) delete process.env[k] |
| 198 | + else process.env[k] = v |
| 199 | + } |
| 200 | + Object.defineProperty(process.stdout, "isTTY", { value: savedIsTTY, configurable: true }) |
| 201 | + }) |
| 202 | + |
| 203 | + test("a URL carrying a control byte anywhere falls back to plain sanitized text", () => { |
| 204 | + const bad = [ |
| 205 | + "\x1bhttps://example.com/w/1", |
| 206 | + "https://exam\x1bple.com/w/1", |
| 207 | + "https://example.com/w/1\x1b]8;;https://evil.example\x1b\\", |
| 208 | + "https://example.com/w/1?x=\x9b", |
| 209 | + "https://example.com/w/1#\x7f", |
| 210 | + ] |
| 211 | + for (const url of bad) { |
| 212 | + const out = hyperlink("name", url) |
| 213 | + expect(out).toBe("name") |
| 214 | + expect(out).not.toContain("\x1b") |
| 215 | + } |
| 216 | + }) |
| 217 | + |
| 218 | + test("non-http(s) schemes that `new URL()` accepts never become links", () => { |
| 219 | + for (const url of ["javascript:alert(1)", "file:///etc/passwd", "ftp://example.com/x", "data:text/html,hi", "mailto:[email protected]"]) { |
| 220 | + expect(hyperlink("name", url)).toBe("name") |
| 221 | + } |
| 222 | + }) |
| 223 | + |
| 224 | + test("plain http: is allowed and the link wraps exactly the sanitized text", () => { |
| 225 | + const out = hyperlink("na\x1bme", "http://example.com/w/1") |
| 226 | + expect(out).toContain("\x1b]8;;http://example.com/w/1\x1b\\") |
| 227 | + expect(out).toContain("name") |
| 228 | + expect(out).not.toContain("na\x1bme") |
| 229 | + }) |
| 230 | + |
| 231 | + test("an OSC 8 breakout in the text is defused: the only link targets in the output are ours", () => { |
| 232 | + const out = hyperlink("\x1b]8;;https://evil.example\x1b\\", "https://example.com/w/1") |
| 233 | + expect(typeof out).toBe("string") |
| 234 | + // The text's ESC bytes are gone, so "]8;;https://evil.example\\" is inert visible text, never an |
| 235 | + // OSC 8 sequence: every ESC-introduced link opener in the output points at our URL or is the |
| 236 | + // empty closer. |
| 237 | + const openers = out.split("\x1b]8;;").slice(1) |
| 238 | + expect(openers.length).toBe(2) |
| 239 | + expect(openers[0].startsWith("https://example.com/w/1\x1b\\")).toBe(true) |
| 240 | + expect(openers[1].startsWith("\x1b\\")).toBe(true) |
| 241 | + expect(out).not.toContain("\x1b]8;;https://evil") |
| 242 | + }) |
| 243 | + |
| 244 | + test("text made entirely of control bytes yields a string and never throws", () => { |
| 245 | + const out = hyperlink("\x1b\x9b\u202e", "https://example.com/w/1") |
| 246 | + expect(typeof out).toBe("string") |
| 247 | + // Remove our own wrapper sequences; nothing of the hostile text may remain. |
| 248 | + const stripped = out.replace(/\x1b\]8;;[^\x1b]*\x1b\\/g, "").replace(/\x1b\[2?4m/g, "") |
| 249 | + expect(stripped).toBe("") |
| 250 | + }) |
| 251 | + |
| 252 | + test("empty text is returned untouched even with a valid URL", () => { |
| 253 | + expect(hyperlink("", "https://example.com/w/1")).toBe("") |
| 254 | + }) |
| 255 | +}) |
| 256 | + |
| 257 | +describe("v0.11.2 adversarial: terminalSupportsHyperlinks env edge cases", () => { |
| 258 | + const saved: Record<string, string | undefined> = {} |
| 259 | + const savedIsTTY = process.stdout.isTTY |
| 260 | + beforeEach(() => { |
| 261 | + for (const k of ["TERM", "TERM_PROGRAM", "WT_SESSION", "KONSOLE_VERSION", "VTE_VERSION"]) { |
| 262 | + saved[k] = process.env[k] |
| 263 | + delete process.env[k] |
| 264 | + } |
| 265 | + Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true }) |
| 266 | + }) |
| 267 | + afterEach(() => { |
| 268 | + for (const [k, v] of Object.entries(saved)) { |
| 269 | + if (v === undefined) delete process.env[k] |
| 270 | + else process.env[k] = v |
| 271 | + } |
| 272 | + Object.defineProperty(process.stdout, "isTTY", { value: savedIsTTY, configurable: true }) |
| 273 | + }) |
| 274 | + |
| 275 | + test("VTE_VERSION boundary: 4999 no, 5000 yes; garbage, empty, negative and Infinity are not treated as support", () => { |
| 276 | + process.env.VTE_VERSION = "4999" |
| 277 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 278 | + process.env.VTE_VERSION = "5000" |
| 279 | + expect(terminalSupportsHyperlinks()).toBe(true) |
| 280 | + for (const v of ["", "abc", "-1", "NaN"]) { |
| 281 | + process.env.VTE_VERSION = v |
| 282 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 283 | + } |
| 284 | + // `Number("Infinity") >= 5000` is true; document the behaviour rather than let it drift silently. |
| 285 | + process.env.VTE_VERSION = "Infinity" |
| 286 | + expect(terminalSupportsHyperlinks()).toBe(true) |
| 287 | + }) |
| 288 | + |
| 289 | + test("TERM_PROGRAM matching is exact: lookalike casing is not on the allowlist", () => { |
| 290 | + for (const p of ["iterm.app", "ITERM.APP", "WEZTERM", "VSCode", "Apple_Terminal", ""]) { |
| 291 | + process.env.TERM_PROGRAM = p |
| 292 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 293 | + } |
| 294 | + }) |
| 295 | + |
| 296 | + test("TERM=dumb or TERM=linux wins over every positive signal", () => { |
| 297 | + process.env.TERM_PROGRAM = "iTerm.app" |
| 298 | + process.env.WT_SESSION = "1" |
| 299 | + process.env.KONSOLE_VERSION = "230000" |
| 300 | + process.env.VTE_VERSION = "9999" |
| 301 | + for (const term of ["dumb", "linux"]) { |
| 302 | + process.env.TERM = term |
| 303 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 304 | + } |
| 305 | + }) |
| 306 | + |
| 307 | + test("a non-TTY stdout is never treated as supporting hyperlinks", () => { |
| 308 | + Object.defineProperty(process.stdout, "isTTY", { value: false, configurable: true }) |
| 309 | + process.env.TERM_PROGRAM = "iTerm.app" |
| 310 | + expect(terminalSupportsHyperlinks()).toBe(false) |
| 311 | + expect(hyperlink("name", "https://example.com/w/1")).toBe("name") |
| 312 | + }) |
| 313 | +}) |
0 commit comments