|
| 1 | +/** |
| 2 | + * Adversarial coverage for the v0.12.4 release payload (v0.12.3..HEAD): the workspace pilot's |
| 3 | + * `serve` HTTP routes (#1366, #1371), skipped-skill reporting (#1374, #1376), relink / consent / |
| 4 | + * pinned skills (#1373), account-scoped binding cache and skill snapshot (#1377), and the TUI |
| 5 | + * model-store copy (#1365, covered by its own aliasing test), PLUS this release's review fixes: |
| 6 | + * |
| 7 | + * - user-facing link / re-link messages no longer say "binding" or "pre-check" (End User persona) |
| 8 | + * |
| 9 | + * Each PR carries its own behavioural suites (skill-sync, state-account-scope, the route tests, |
| 10 | + * skill.test.ts attribution cases). This file targets the pure helpers those PRs introduced with |
| 11 | + * hostile inputs: path traversal and lookalike segments against snapshot attribution, type |
| 12 | + * confusion against the precedence rule, raw-error leakage into user-facing skip reasons, control |
| 13 | + * characters in server-supplied skill ids, and header combinations against the route gate. |
| 14 | + */ |
| 15 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test" |
| 16 | +import path from "path" |
| 17 | +import { readFileSync } from "fs" |
| 18 | +import { |
| 19 | + isWithin, |
| 20 | + snapshotCopyYields, |
| 21 | + snapshotProjectOf, |
| 22 | +} from "../../src/altimate/workspace/snapshot-path" |
| 23 | +import { |
| 24 | + describeSyncProblems, |
| 25 | + displayId, |
| 26 | + forgetAnnouncement, |
| 27 | + shouldAnnounce, |
| 28 | + skipReason, |
| 29 | +} from "../../src/altimate/workspace/skill-sync" |
| 30 | +import { ConflictError, isHiddenBindingConflict } from "../../src/altimate/workspace/api-client" |
| 31 | +import { Server } from "../../src/server/server" |
| 32 | +import { WORKSPACE_PILOT_OFF_MESSAGE } from "../../src/cli/cmd/workspace-pilot" |
| 33 | + |
| 34 | +const SNAP = path.join(".altimate-code", "skill", "_workspace") |
| 35 | + |
| 36 | +describe("v0.12.4 adversarial: snapshot attribution by path segments", () => { |
| 37 | + test("a traversal out of the snapshot is not attributed to it", () => { |
| 38 | + // Resolves to /p/.altimate-code/evil — no `_workspace` segment survives. |
| 39 | + expect(snapshotProjectOf(`/p/${SNAP}/../../evil/SKILL.md`)).toBeNull() |
| 40 | + }) |
| 41 | + |
| 42 | + test("a traversal INTO the snapshot is attributed after resolution", () => { |
| 43 | + expect(snapshotProjectOf(`/p/x/../${SNAP}/a/SKILL.md`)).toBe("/p") |
| 44 | + }) |
| 45 | + |
| 46 | + test("lookalike segments are not a snapshot", () => { |
| 47 | + expect(snapshotProjectOf("/p/.altimate-code/skill/_workspace2/a/SKILL.md")).toBeNull() |
| 48 | + expect(snapshotProjectOf("/p/.altimate-code-x/skill/_workspace/a/SKILL.md")).toBeNull() |
| 49 | + expect(snapshotProjectOf("/p/.altimate-code/skills/_workspace/a/SKILL.md")).toBeNull() |
| 50 | + expect(snapshotProjectOf("/p/_workspace/skill/.altimate-code/a/SKILL.md")).toBeNull() |
| 51 | + }) |
| 52 | + |
| 53 | + test("a project opened at the filesystem root is `/`, not 'no project'", () => { |
| 54 | + expect(snapshotProjectOf(`/${SNAP}/a/SKILL.md`)).toBe("/") |
| 55 | + }) |
| 56 | + |
| 57 | + test("nested snapshots attribute to the outermost project", () => { |
| 58 | + expect(snapshotProjectOf(`/p/${SNAP}/q/${SNAP}/a/SKILL.md`)).toBe("/p") |
| 59 | + }) |
| 60 | + |
| 61 | + test("isWithin refuses a prefix sibling and a parent", () => { |
| 62 | + expect(isWithin("/proj", "/proj2/SKILL.md")).toBe(false) |
| 63 | + expect(isWithin("/proj", "/")).toBe(false) |
| 64 | + expect(isWithin("/proj", "/proj")).toBe(true) |
| 65 | + expect(isWithin("/proj", "/proj/a/../b")).toBe(true) |
| 66 | + expect(isWithin("/proj", "/proj/../proj2")).toBe(false) |
| 67 | + }) |
| 68 | +}) |
| 69 | + |
| 70 | +describe("v0.12.4 adversarial: snapshotCopyYields type confusion", () => { |
| 71 | + const match = `/proj/${SNAP}/pub-1/SKILL.md` |
| 72 | + |
| 73 | + test("non-string or relative existing locations never make the workspace copy yield", () => { |
| 74 | + for (const existing of [undefined, null, 0, 1, {}, [], true, "builtin:x", "<built-in>", "relative/SKILL.md", ""]) { |
| 75 | + expect(snapshotCopyYields(match, existing as unknown, "/proj")).toBe(false) |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + test("no project root means no protection", () => { |
| 80 | + expect(snapshotCopyYields(match, "/proj/.claude/skills/x/SKILL.md", undefined)).toBe(false) |
| 81 | + expect(snapshotCopyYields(match, "/proj/.claude/skills/x/SKILL.md", "")).toBe(false) |
| 82 | + }) |
| 83 | + |
| 84 | + test("only a project skill inside the root wins over the workspace copy", () => { |
| 85 | + expect(snapshotCopyYields(match, "/proj/.claude/skills/x/SKILL.md", "/proj")).toBe(true) |
| 86 | + expect(snapshotCopyYields(match, "/proj2/.claude/skills/x/SKILL.md", "/proj")).toBe(false) |
| 87 | + expect(snapshotCopyYields(match, "/home/me/.claude/skills/x/SKILL.md", "/proj")).toBe(false) |
| 88 | + // Another snapshot entry is not the user's own skill. |
| 89 | + expect(snapshotCopyYields(match, `/proj/${SNAP}/pub-2/SKILL.md`, "/proj")).toBe(false) |
| 90 | + }) |
| 91 | + |
| 92 | + test("a non-snapshot match never yields, whatever it collides with", () => { |
| 93 | + expect(snapshotCopyYields("/proj/.claude/skills/y/SKILL.md", "/proj/.claude/skills/x/SKILL.md", "/proj")).toBe(false) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +describe("v0.12.4 adversarial: skip reasons never carry the raw error", () => { |
| 98 | + test("non-Error inputs fall back to the generic reason", () => { |
| 99 | + for (const err of [undefined, null, "size mismatch", 42, {}, [], Symbol("x")]) { |
| 100 | + expect(skipReason(err)).toBe("it could not be downloaded") |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + test("paths and URLs in the message are not echoed", () => { |
| 105 | + const reason = skipReason(new Error("size mismatch for /Users/alice/.secrets/token at https://api.example.com/x")) |
| 106 | + expect(reason).toBe("its file size could not be verified") |
| 107 | + expect(reason).not.toContain("/Users") |
| 108 | + expect(reason).not.toContain("https://") |
| 109 | + }) |
| 110 | + |
| 111 | + test("local write failures are attributed to the device, network codes are not", () => { |
| 112 | + for (const code of ["ENOSPC", "EDQUOT", "EACCES", "EPERM", "EROFS"]) { |
| 113 | + expect(skipReason(Object.assign(new Error("x"), { code }))).toBe("it could not be saved on this device") |
| 114 | + } |
| 115 | + for (const code of ["ECONNRESET", "ETIMEDOUT", "__proto__", "constructor", "toString"]) { |
| 116 | + expect(skipReason(Object.assign(new Error("x"), { code }))).toBe("it could not be downloaded") |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + test("a message that merely CONTAINS a known prefix is not mapped", () => { |
| 121 | + expect(skipReason(new Error("server said: size mismatch"))).toBe("it could not be downloaded") |
| 122 | + }) |
| 123 | +}) |
| 124 | + |
| 125 | +describe("v0.12.4 adversarial: server-supplied skill ids are sanitised for display", () => { |
| 126 | + test("control characters and line separators are stripped", () => { |
| 127 | + expect(displayId("pub\u0000-1\n\r\t\u001b[31m")).toBe("pub-1[31m") |
| 128 | + expect(displayId("a
b
c\u0085d")).toBe("abcd") |
| 129 | + }) |
| 130 | + |
| 131 | + test("an id that is empty after cleaning gets a placeholder", () => { |
| 132 | + expect(displayId("")).toBe("(unnamed skill)") |
| 133 | + expect(displayId("\u0000\n
")).toBe("(unnamed skill)") |
| 134 | + }) |
| 135 | + |
| 136 | + test("the 64-character boundary", () => { |
| 137 | + expect(displayId("a".repeat(64))).toBe("a".repeat(64)) |
| 138 | + const long = displayId("a".repeat(65)) |
| 139 | + expect(long.length).toBe(64) |
| 140 | + expect(long.endsWith("…")).toBe(true) |
| 141 | + }) |
| 142 | +}) |
| 143 | + |
| 144 | +describe("v0.12.4 adversarial: describeSyncProblems boundaries", () => { |
| 145 | + const skipped = (n: number) => Array.from({ length: n }, (_, i) => ({ skill: `s${i}`, reason: "r" })) |
| 146 | + |
| 147 | + test("nothing skipped and no error is not a problem", () => { |
| 148 | + expect(describeSyncProblems({ changed: false, skipped: [] })).toBeNull() |
| 149 | + }) |
| 150 | + |
| 151 | + test("an error alone is reported as the run failing", () => { |
| 152 | + expect(describeSyncProblems({ changed: false, skipped: [], error: "offline" })).toEqual({ |
| 153 | + title: "Workspace skills not synced", |
| 154 | + message: "offline", |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + test("singular, exactly three, and more than three", () => { |
| 159 | + expect(describeSyncProblems({ changed: false, skipped: skipped(1) })!.title).toBe("1 workspace skill skipped") |
| 160 | + const three = describeSyncProblems({ changed: false, skipped: skipped(3) })! |
| 161 | + expect(three.message.split("\n")).toHaveLength(3) |
| 162 | + expect(three.message).not.toContain("more") |
| 163 | + const five = describeSyncProblems({ changed: false, skipped: skipped(5) })! |
| 164 | + expect(five.title).toBe("5 workspace skills skipped") |
| 165 | + expect(five.message.split("\n")).toEqual(["s0: r", "s1: r", "s2: r", "…and 2 more"]) |
| 166 | + }) |
| 167 | + |
| 168 | + test("a run error is appended after the skipped list, not instead of it", () => { |
| 169 | + const p = describeSyncProblems({ changed: false, skipped: skipped(1), error: "publish failed" })! |
| 170 | + expect(p.message.split("\n")).toEqual(["s0: r", "publish failed"]) |
| 171 | + }) |
| 172 | +}) |
| 173 | + |
| 174 | +describe("v0.12.4 adversarial: the announce latch", () => { |
| 175 | + const problem = { title: "t", message: "m" } |
| 176 | + |
| 177 | + test("the same problem is announced once per directory, keyed on the resolved path", () => { |
| 178 | + const dir = `/tmp/v0124-latch-${process.pid}-a` |
| 179 | + expect(shouldAnnounce(dir, problem)).toBe(true) |
| 180 | + expect(shouldAnnounce(`${dir}/./`, problem)).toBe(false) |
| 181 | + expect(shouldAnnounce(dir, { title: "t", message: "m2" })).toBe(true) |
| 182 | + }) |
| 183 | + |
| 184 | + test("a clean run clears the latch so a recurrence is announced again", () => { |
| 185 | + const dir = `/tmp/v0124-latch-${process.pid}-b` |
| 186 | + expect(shouldAnnounce(dir, problem)).toBe(true) |
| 187 | + expect(shouldAnnounce(dir, null)).toBe(false) |
| 188 | + expect(shouldAnnounce(dir, problem)).toBe(true) |
| 189 | + }) |
| 190 | + |
| 191 | + test("forgetting a stale problem does not drop a newer one", () => { |
| 192 | + const dir = `/tmp/v0124-latch-${process.pid}-c` |
| 193 | + const newer = { title: "t", message: "newer" } |
| 194 | + expect(shouldAnnounce(dir, problem)).toBe(true) |
| 195 | + expect(shouldAnnounce(dir, newer)).toBe(true) |
| 196 | + forgetAnnouncement(dir, problem) |
| 197 | + expect(shouldAnnounce(dir, newer)).toBe(false) |
| 198 | + forgetAnnouncement(dir, newer) |
| 199 | + expect(shouldAnnounce(dir, newer)).toBe(true) |
| 200 | + }) |
| 201 | +}) |
| 202 | + |
| 203 | +describe("v0.12.4 adversarial: workspace route gate", () => { |
| 204 | + const original = process.env.ALTIMATE_WORKSPACE |
| 205 | + beforeEach(() => { |
| 206 | + process.env.ALTIMATE_WORKSPACE = "1" |
| 207 | + }) |
| 208 | + afterEach(() => { |
| 209 | + if (original === undefined) delete process.env.ALTIMATE_WORKSPACE |
| 210 | + else process.env.ALTIMATE_WORKSPACE = original |
| 211 | + }) |
| 212 | + |
| 213 | + test("outside the pilot every request is a 409, whatever its headers", () => { |
| 214 | + delete process.env.ALTIMATE_WORKSPACE |
| 215 | + for (const [origin, fetchSite] of [ |
| 216 | + [undefined, undefined], |
| 217 | + ["http://127.0.0.1:4096", "same-origin"], |
| 218 | + ["https://evil.example", "cross-site"], |
| 219 | + ] as const) { |
| 220 | + expect(Server.workspaceRouteRefusal(origin, "127.0.0.1:4096", "pw", fetchSite)?.status).toBe(409) |
| 221 | + } |
| 222 | + }) |
| 223 | + |
| 224 | + test("a browser-labelled cross-site request is refused even with a password and matching origin", () => { |
| 225 | + for (const fetchSite of ["cross-site", "same-site", "Same-Origin", "NONE", " same-origin"]) { |
| 226 | + expect(Server.workspaceRouteRefusal("http://127.0.0.1:4096", "127.0.0.1:4096", "pw", fetchSite)?.status).toBe(403) |
| 227 | + } |
| 228 | + }) |
| 229 | + |
| 230 | + test("an origin on an unsecured server is refused; a native client is not", () => { |
| 231 | + expect(Server.workspaceRouteRefusal("http://127.0.0.1:4096", "127.0.0.1:4096", undefined)?.status).toBe(403) |
| 232 | + expect(Server.workspaceRouteRefusal("http://127.0.0.1:4096", "127.0.0.1:4096", "")?.status).toBe(403) |
| 233 | + expect(Server.workspaceRouteRefusal(undefined, "127.0.0.1:4096", undefined)).toBeUndefined() |
| 234 | + }) |
| 235 | + |
| 236 | + test("with a password, malformed and lookalike origins are refused", () => { |
| 237 | + for (const origin of ["null", "not a url", "http://127.0.0.1:4096.evil.example", "http://127.0.0.1:40960", "http://localhost:4096"]) { |
| 238 | + expect(Server.workspaceRouteRefusal(origin, "127.0.0.1:4096", "pw")?.status).toBe(403) |
| 239 | + } |
| 240 | + expect(Server.workspaceRouteRefusal("http://127.0.0.1:4096", undefined, "pw")?.status).toBe(403) |
| 241 | + }) |
| 242 | +}) |
| 243 | + |
| 244 | +describe("v0.12.4 adversarial: hidden-workspace conflicts", () => { |
| 245 | + test("only a ConflictError naming an id and withholding the name is a hidden workspace", () => { |
| 246 | + const conflict = (detail: Record<string, unknown>) => new ConflictError(detail as never) |
| 247 | + expect(isHiddenBindingConflict(conflict({ message: "x", existing_datamate_id: 7 }))).toBe(true) |
| 248 | + expect(isHiddenBindingConflict(conflict({ message: "x", existing_datamate_id: 7, existing_datamate_name: "Growth" }))).toBe(false) |
| 249 | + expect(isHiddenBindingConflict(conflict({ message: "x", existing_datamate_id: "7" }))).toBe(false) |
| 250 | + expect(isHiddenBindingConflict(conflict({ message: "x" }))).toBe(false) |
| 251 | + for (const err of [undefined, null, new Error("409"), { detail: { existing_datamate_id: 7 } }, "conflict"]) { |
| 252 | + expect(isHiddenBindingConflict(err)).toBe(false) |
| 253 | + } |
| 254 | + }) |
| 255 | +}) |
| 256 | + |
| 257 | +describe("v0.12.4 adversarial: user-facing wording (release review fixes)", () => { |
| 258 | + const src = (p: string) => readFileSync(path.resolve(import.meta.dir, "..", "..", "src", p), "utf-8") |
| 259 | + |
| 260 | + test("link and re-link messages speak of links, not bindings or pre-checks", () => { |
| 261 | + const link = src("cli/cmd/link.ts") |
| 262 | + const tui = src("plugin/tui/altimate/workspace.tsx") |
| 263 | + for (const stale of [ |
| 264 | + "Pre-check missed an existing binding", |
| 265 | + "No existing binding to re-link", |
| 266 | + "No existing binding for this remote to re-link", |
| 267 | + "pre-check skipped", |
| 268 | + ]) { |
| 269 | + expect(link).not.toContain(stale) |
| 270 | + expect(tui).not.toContain(stale) |
| 271 | + } |
| 272 | + expect(link).toContain("This project is already linked to a workspace — re-linking it instead.") |
| 273 | + expect(tui).toContain("Could not reach the Altimate workspace service to check for an existing link.") |
| 274 | + }) |
| 275 | + |
| 276 | + test("the pilot-off message tells the user exactly how to opt in", () => { |
| 277 | + expect(WORKSPACE_PILOT_OFF_MESSAGE).toContain("ALTIMATE_WORKSPACE=1") |
| 278 | + }) |
| 279 | +}) |
0 commit comments