Skip to content

Commit 77256d0

Browse files
sahrizviclaude
andcommitted
fix(workspace): three review findings on skill publish
**A symlinked skill directory defeated the managed-snapshot check.** `path.resolve` is lexical: it normalises `..` and absolutises, but it does not follow links. So a skill directory that IS a link into `.altimate-code/skill/ _workspace` resolved to its own path, passed `isManagedSkill`, and the bundle walk then followed the link — publishing the workspace's own skills back to it under the user's name. Compared through `realpathSync` now, falling back to the lexical form for a path that does not exist, which cannot be a link into the snapshot anyway. **The bundle size guard could not stop the thing it exists to stop.** `collectBundle` read each file with `readFile` and only then checked the running total, so a single oversized file was pulled entirely into memory before being rejected. Size is checked before the read now; the cumulative check stays for many small files and as a backstop if the file grows in between. **A conflicting rename on the update path surfaced a raw API envelope.** The POST path maps 409 to `SkillNameConflictError`; the PATCH path only handled `NotFoundError`, so renaming a skill onto a name this creator already uses reached the caller as the server's own error shape — the exact outcome the typed errors in this module exist to prevent, and invisible from the create path. Tests: 14 in this file, 3 new, whole altimate suite green. Worth recording how the tests were arrived at, because the first versions were worthless: all three mutations SURVIVED. Asserting that an oversized bundle is rejected does not test this fix — the post-read check rejects it too — so the test now patches `readFile` and asserts the oversized file is never read at all. The other two had no coverage whatsoever. All three mutations fail now. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012Q51zFUmPg1WwtS5CrGJE6
1 parent 79f77e1 commit 77256d0

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

‎packages/opencode/src/altimate/workspace/skill-publish.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
// interpret.
3434
import fs from "fs/promises"
3535
import path from "path"
36+
import { realpathSync } from "fs"
3637
import { Log } from "@/altimate/util/log"
3738
import { Global } from "@/global"
3839
import { Filesystem } from "@/util/filesystem"
@@ -125,6 +126,15 @@ export async function collectBundle(dir: string): Promise<BundleFile[]> {
125126
}
126127
if (!entry.isFile()) continue
127128
const relative = path.relative(root, full).split(path.sep).join("/")
129+
// Size BEFORE read. `readFile` pulls the whole file into memory, so
130+
// checking the running total afterwards let a single oversized file
131+
// through the very guard meant to stop it — the limit was enforced only
132+
// once the damage was done. The cumulative check below stays as the
133+
// answer for many small files, and as a backstop if the file grew
134+
// between this stat and the read.
135+
const stat = await fs.stat(full)
136+
if (bytes + stat.size > MAX_BUNDLE_BYTES)
137+
throw new BundleTooLargeError(`This skill is larger than ${MAX_BUNDLE_BYTES / (1024 * 1024)}MB.`)
128138
const raw = await fs.readFile(full)
129139
let content: string
130140
try {
@@ -148,8 +158,23 @@ export async function collectBundle(dir: string): Promise<BundleFile[]> {
148158

149159
/** True when this path lives inside the workspace-owned snapshot. */
150160
export function isManagedSkill(projectDirectory: string, skillDirectory: string): boolean {
151-
const managed = path.resolve(projectDirectory, MANAGED_DIR)
152-
const candidate = path.resolve(skillDirectory)
161+
// `path.resolve` is lexical: it normalises `..` and makes the path absolute,
162+
// but it does not follow links. A skill directory that IS a symlink into the
163+
// workspace-owned snapshot therefore resolved to its own link path, missed
164+
// this check, and `collectBundle` then walked through the link and published
165+
// the workspace's own skills back to it. Compare real paths where they exist.
166+
const real = (p: string): string => {
167+
try {
168+
return realpathSync(p)
169+
} catch {
170+
// Absent or unreadable: fall back to the lexical form. A path that does
171+
// not exist cannot be a link into the snapshot, and `collectBundle` will
172+
// fail on it in a moment anyway.
173+
return path.resolve(p)
174+
}
175+
}
176+
const managed = real(path.resolve(projectDirectory, MANAGED_DIR))
177+
const candidate = real(skillDirectory)
153178
return candidate === managed || candidate.startsWith(managed + path.sep)
154179
}
155180

@@ -257,6 +282,11 @@ export async function publishSkill(input: {
257282
// The skill was deleted in the workspace since we published it. Falling
258283
// through to create is the useful answer; failing would strand the user
259284
// with a local id they cannot see or clear.
285+
// A PATCH that renames onto a name this creator already uses answers 409.
286+
// Without this the raw server envelope reaches the caller — the exact
287+
// thing the typed errors in this module exist to prevent — and only on
288+
// the update path, so the create path looked correct in isolation.
289+
if (err instanceof ConflictError) throw new SkillNameConflictError(input.name)
260290
if (!(err instanceof NotFoundError)) throw err
261291
log.info("published skill no longer exists in the workspace; creating it again", {
262292
publicId: existing,

‎packages/opencode/test/altimate/workspace/skill-publish.test.ts‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66
// real sandbox, network stubbed at `globalThis.fetch` so assertions are about the
77
// requests actually issued — method, path, body — rather than a mock's call log.
88
import { afterAll, afterEach, beforeEach, describe, expect, test } from "bun:test"
9-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
9+
import {
10+
closeSync,
11+
ftruncateSync,
12+
mkdirSync,
13+
mkdtempSync,
14+
openSync,
15+
rmSync,
16+
symlinkSync,
17+
writeFileSync,
18+
} from "node:fs"
1019
import path from "node:path"
20+
import fsp from "node:fs/promises"
1121
import os from "node:os"
1222

1323
const ORIGINAL_XDG_STATE_HOME = process.env.XDG_STATE_HOME
@@ -210,3 +220,64 @@ describe("publishSkill", () => {
210220
expect(requests.filter((r) => r.method === "POST")).toHaveLength(1)
211221
})
212222
})
223+
224+
describe("the bundle size guard", () => {
225+
test("refuses an oversized file WITHOUT reading it into memory", async () => {
226+
// The guard checked the running total after `readFile`, so a single huge
227+
// file was fully loaded before being rejected — the limit enforced only
228+
// once the memory had already been spent. Asserting on the rejection alone
229+
// does not test that: the post-read check rejects too, and the mutation
230+
// survived. The property is that `readFile` is never called for the file.
231+
const dir = path.join(SANDBOX, `oversize-${Math.random().toString(36).slice(2)}`)
232+
mkdirSync(dir, { recursive: true })
233+
writeFileSync(path.join(dir, "SKILL.md"), "---\nname: big\n---\n")
234+
const huge = path.join(dir, "huge.txt")
235+
const fd = openSync(huge, "w")
236+
try {
237+
ftruncateSync(fd, 64 * 1024 * 1024) // sparse: past the limit, cheap on disk
238+
} finally {
239+
closeSync(fd)
240+
}
241+
242+
const read: string[] = []
243+
const originalReadFile = fsp.readFile
244+
;(fsp as unknown as { readFile: unknown }).readFile = ((...args: unknown[]) => {
245+
read.push(String(args[0]))
246+
return (originalReadFile as (...a: unknown[]) => unknown)(...args)
247+
}) as unknown as typeof fsp.readFile
248+
249+
try {
250+
await expect(collectBundle(dir)).rejects.toThrow(/larger than/i)
251+
} finally {
252+
;(fsp as unknown as { readFile: unknown }).readFile = originalReadFile
253+
}
254+
expect(read.some((r) => r.endsWith("huge.txt"))).toBe(false)
255+
})
256+
257+
test("a symlinked skill directory into the managed snapshot is still managed", async () => {
258+
// `path.resolve` is lexical, so a skill directory that IS a link into the
259+
// workspace-owned snapshot resolved to its own path and passed the check —
260+
// and the bundle walk then followed the link and would have published the
261+
// workspace's own skills back to it.
262+
const proj = mkdtempSync(path.join(SANDBOX, "symproj-"))
263+
const managed = path.join(proj, ".altimate-code", "skill", "_workspace", "pub-a")
264+
mkdirSync(managed, { recursive: true })
265+
const link = path.join(proj, "looks-local")
266+
symlinkSync(managed, link)
267+
268+
expect(isManagedSkill(proj, link)).toBe(true)
269+
})
270+
271+
test("a rename that collides on the update path is a typed conflict", async () => {
272+
// The create path mapped 409 to SkillNameConflictError; the update path did
273+
// not, so a PATCH that renames onto an existing name surfaced the raw
274+
// server envelope — the exact thing this module's typed errors exist to
275+
// prevent.
276+
await publish() // records the id, so the next call takes the PATCH branch
277+
statuses.PATCH = 409
278+
279+
const err = await publish().catch((e) => e)
280+
281+
expect(err).toBeInstanceOf(SkillNameConflictError)
282+
})
283+
})

0 commit comments

Comments
 (0)