Skip to content

Commit 0c47f84

Browse files
sahrizviclaude
andcommitted
fix(check): reject symlinks whose target isn't SQL + test hygiene
Round 3 on the release/v0.9.6 hotfix — addresses 3 new bot findings on PR #1131: - coderabbit MAJOR (real leak surface): the extension filter and statSync isFile check both see the SYMLINK target, not the link. So `ln -s /etc/passwd passwd.sql` sailed through — link's .sql extension passed, statSync followed the link, /etc/passwd IS a regular file, readFileSync then read its content, and the safety renderer echoed the first line. Exact leak class this PR exists to close. Fix: `lstatSync` to detect symlinks; when one is present, `realpathSync` to resolve the target and require ITS extension to also be SQL. Preserves `link.sql -> real.sql` (both SQL); rejects `passwd.sql -> /etc/passwd`. Two new handler-level regression tests: • "handler rejects symlink whose target is NOT a SQL file" • "handler ACCEPTS symlink whose target IS a SQL file" Reproduced the attack locally with the built binary: pre-fix leaked the file's first line; post-fix skips cleanly. - coderabbit MAJOR (spy hygiene in check-e2e.test.ts): the spyOn calls on process.stdout.write / process.stderr.write / console.error in beforeEach were never restored. Bun doesn't auto-restore spyOn across test files. Added `mock.restore()` in afterEach. - coderabbit MAJOR (dispatcher singleton isolation): same class of finding as the env-var one — the tests mutate module-wide `nativeHandlers`/`_ensureRegistered`. Not fixable without an instance-per-test refactor of the whole Dispatcher module. Documented the concurrency contract in-file: safe under bun's default sequential test-file execution; #1130 already tracks the broader parallel-safety cleanup this class of finding calls for. Same disposition as the release-adversarial file. Verification: - 103/103 tests pass (2 new symlink cases) - rebuilt darwin-arm64 binary; symlink attack repro shows the warning and no content leak - typecheck clean; marker guard clean Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent 5224705 commit 0c47f84

3 files changed

Lines changed: 84 additions & 2 deletions

File tree

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { Argv } from "yargs"
33
import { cmd } from "./cmd"
44
import { Dispatcher } from "../../altimate/native"
5-
import { readFileSync, existsSync, statSync } from "fs"
5+
import { readFileSync, existsSync, statSync, lstatSync, realpathSync } from "fs"
66
import { Glob } from "../../util/glob"
77
import path from "path"
88
import {
@@ -513,6 +513,38 @@ export const CheckCommand = cmd({
513513
console.error(`Warning: not a regular file (directory or other), skipping: ${f}`)
514514
return false
515515
}
516+
// Reject symlinks whose target does NOT itself have a SQL extension.
517+
// The extension filter above only inspects the LINK name, and
518+
// statSync follows the link — so ``ln -s /etc/passwd passwd.sql``
519+
// sails through with statIsFile=true and readFileSync then reads
520+
// /etc/passwd's content, which the safety renderer echoes back
521+
// (the same class of leak this whole PR exists to close). Detect
522+
// symlinks with lstat and re-validate the resolved target's
523+
// extension. Preserves ``link.sql -> real.sql`` (both are SQL) but
524+
// rejects ``passwd.sql -> /etc/passwd``. (coderabbit MAJOR round
525+
// on release/v0.9.6 hotfix.)
526+
let lst
527+
try {
528+
lst = lstatSync(f)
529+
} catch (e) {
530+
console.error(`Warning: lstat failed, skipping: ${f} (${(e as Error).message})`)
531+
return false
532+
}
533+
if (lst.isSymbolicLink()) {
534+
let resolved: string
535+
try {
536+
resolved = realpathSync(f)
537+
} catch (e) {
538+
console.error(`Warning: symlink resolve failed, skipping: ${f} (${(e as Error).message})`)
539+
return false
540+
}
541+
if (!isSqlFile(resolved)) {
542+
console.error(
543+
`Warning: symlink target is not a SQL file, skipping: ${f} -> ${resolved}`,
544+
)
545+
return false
546+
}
547+
}
516548
return true
517549
})
518550

‎packages/opencode/test/altimate/dispatcher.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import * as Dispatcher from "../../src/altimate/native/dispatcher"
66
// firing during a test cannot fail the test. The previous env-var
77
// mutation was defensive against nothing and wasn't parallel-safe (issue
88
// #1130 — https://github.com/AltimateAI/altimate-code/issues/1130).
9+
//
10+
// Concurrency contract: the ``Dispatcher`` module is a process-wide
11+
// singleton (``nativeHandlers`` Map, ``_ensureRegistered`` hook). These
12+
// tests intentionally mutate that singleton via ``Dispatcher.reset()``
13+
// and ``Dispatcher.setRegistrationHook()``. This is safe under bun's
14+
// DEFAULT behaviour of running test files sequentially in one process,
15+
// but NOT parallel-safe if the runner is later flipped to ``--concurrency
16+
// N``. Isolating the dispatcher would require an instance-per-test
17+
// refactor across the whole module; deferred as a broader tech-debt
18+
// task (issue #1130 tracks the parallel-safety cleanup for this + the
19+
// sibling release-adversarial file). Same class of finding as the
20+
// env-var mutation coderabbit flagged.
921

1022
describe("Dispatcher", () => {
1123
beforeEach(() => {

‎packages/opencode/test/cli/check-e2e.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// IMPORTANT: This file uses Dispatcher.register() / Dispatcher.reset() instead
44
// of mock.module("@/altimate/native") to avoid Bun's mock.module leaking across
55
// test files and breaking Glob/Dispatcher for all subsequent tests in CI.
6-
import { describe, test, expect, beforeEach, afterEach, spyOn } from "bun:test"
6+
import { describe, test, expect, beforeEach, afterEach, mock, spyOn } from "bun:test"
77
import * as fs from "fs/promises"
88
import path from "path"
99
import os from "os"
@@ -153,6 +153,11 @@ beforeEach(async () => {
153153
afterEach(async () => {
154154
process.exit = origExit
155155
process.exitCode = 0
156+
// Restore all spies (process.stdout.write, process.stderr.write,
157+
// console.error) — Bun doesn't auto-restore spyOn mocks across test
158+
// files, so without this the mocks leak into subsequent files' output.
159+
// (coderabbit MAJOR on v0.9.6 hotfix.)
160+
mock.restore()
156161
// Restore Dispatcher: clear our mocks and re-enable lazy registration
157162
Dispatcher.reset()
158163
// Re-install the registration hook so subsequent tests get real handlers
@@ -1148,6 +1153,39 @@ describe("check command adversarial", () => {
11481153
expect(r.stdout).not.toContain("not-a-file")
11491154
})
11501155

1156+
test("handler rejects symlink whose target is NOT a SQL file", async () => {
1157+
// coderabbit MAJOR: statSync follows symlinks; without a lstat check the
1158+
// extension filter would accept `passwd.sql -> /etc/passwd` because the
1159+
// LINK's name ends in .sql, and readFileSync would then dutifully read
1160+
// /etc/passwd's content — the exact leak class this PR closes. Fix uses
1161+
// lstatSync + realpathSync to require the resolved target's extension
1162+
// to also be SQL.
1163+
const target = path.join(tmpDir.dir, "fake-passwd.txt")
1164+
const link = path.join(tmpDir.dir, "passwd.sql")
1165+
await fs.writeFile(target, "root:x:0:0:root:/root:/bin/bash\n", "utf-8")
1166+
await fs.symlink(target, link)
1167+
const r = await runHandler(baseArgs({ files: [link], checks: "safety" }))
1168+
expect(r.stderr).toMatch(/symlink target is not a SQL file/)
1169+
expect(r.stderr).toContain("passwd.sql")
1170+
expect(r.stderr).toContain("fake-passwd.txt")
1171+
expect(r.stderr).toContain("No SQL files found to check")
1172+
// Critical: no content leak — the file was never read.
1173+
expect(r.stdout).not.toMatch(/root:x:0/i)
1174+
expect(r.stderr).not.toMatch(/root:x:0/i)
1175+
})
1176+
1177+
test("handler ACCEPTS symlink whose target IS a SQL file", async () => {
1178+
// Sibling of the above — symlink-to-SQL is a legitimate use case
1179+
// (e.g. `link.sql -> real.sql`) and must still flow through.
1180+
const target = await writeSql(tmpDir.dir, "real.sql", "SELECT 1;")
1181+
const link = path.join(tmpDir.dir, "link.sql")
1182+
await fs.symlink(target, link)
1183+
const r = await runHandler(baseArgs({ files: [link], checks: "safety" }))
1184+
const j = parseJson(r.stdout)
1185+
expect(j.files_checked).toBe(1)
1186+
expect(r.stderr).not.toMatch(/symlink target is not/)
1187+
})
1188+
11511189
test("handler rejects a dotfile named exactly '.sql'", async () => {
11521190
// cubic P2: ``.sql`` as a filename is a dotfile with no extension,
11531191
// not a SQL file. Must be rejected before reaching the engine.

0 commit comments

Comments
 (0)