Skip to content

Commit 5224705

Browse files
sahrizviclaude
andcommitted
fix(check): address bot review — .ddl in default glob, dotfile edge, isFile check, handler test + close #1130
Round 2 on the release/v0.9.6 hotfix — addresses the 5 bot findings on PR #1131 and rolls in the follow-up filed as issue #1130. - coderabbit MAJOR + cubic P2 (default-discovery gap): `**/*.sql` glob at check.ts:462 missed `.ddl` files that the new SQL_EXTENSIONS filter accepts. Now scans both extensions and dedupes. - cubic P2 (bare-extension dotfile): `isSqlFile` accepted files named literally `.sql` and `.ddl` as if the leading dot were an extension separator. Node's `path.extname` treats those as dotfiles with NO extension. Fixed to match: bare-`.sql`/`.ddl` filenames are rejected. - cubic P2 (directory with .sql suffix): a directory named `foo.sql` passed the extension filter. Added a `statSync(f).isFile()` gate that rejects directories (and symlinks-to-directories, since statSync follows symlinks) with a clear warning. - cubic P2 (test coverage gap): the `isSqlFile` tests only exercised the pure helper; if the CLI handler stopped calling the filter the tests would still pass. Added a handler-level integration test with mixed SQL/non-SQL input that asserts the warning is printed and no content leaks. Also added handler tests for the directory + bare dotfile cases. Updated the pre-existing "handles directory with .sql extension" test to match the new isFile behavior (was expecting "Error reading" from the downstream readFile crash). - #1130 follow-up (drop env-var mutation from dispatcher test files): removed the `beforeAll`/`afterAll` that mutated `process.env.ALTIMATE_TELEMETRY_DISABLED` in `test/altimate/dispatcher.test.ts` and `test/skill/release-v0.9.6-adversarial.test.ts`. Dispatcher.call already wraps every Telemetry.track in try/catch that swallows errors — the env-var was defensive against nothing and wasn't parallel-safe. Closes #1130. Verification: - 101/101 tests in check-e2e + dispatcher + release-adversarial pass - rebuilt darwin-arm64 binary; sanity reproduction still shows no leak - typecheck clean; marker guard clean Closes #1130 Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent 14c8a4e commit 5224705

5 files changed

Lines changed: 108 additions & 34 deletions

File tree

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ export const VALID_CHECKS = new Set(["lint", "validate", "safety", "policy", "pi
5252
* parsed and echoed back through engine error messages (v0.9.6 fix). */
5353
export const SQL_EXTENSIONS = new Set([".sql", ".ddl"])
5454

55-
/** Case-insensitive SQL-extension test. Path.extname returns "" for
56-
* files without a dot, which correctly resolves to "not a SQL file". */
55+
/** Case-insensitive SQL-extension test. Matches node's ``path.extname``
56+
* semantics: a filename whose only dot is the FIRST character (e.g.
57+
* ``.sql`` or ``.ddl``) is a dotfile with NO extension, not a SQL file.
58+
* ``.query.sql`` is a dotfile with a real ``.sql`` extension (accepted).
59+
* ``foo.bar/baz`` is not SQL (the dot is in the directory, not the
60+
* filename). (cubic P2 round on release/v0.9.6 hotfix.) */
5761
export function isSqlFile(filePath: string): boolean {
58-
const dot = filePath.lastIndexOf(".")
5962
const slash = Math.max(filePath.lastIndexOf("/"), filePath.lastIndexOf("\\"))
60-
if (dot <= slash) return false
61-
return SQL_EXTENSIONS.has(filePath.slice(dot).toLowerCase())
63+
const basename = slash === -1 ? filePath : filePath.slice(slash + 1)
64+
const dot = basename.lastIndexOf(".")
65+
// dot === -1: no extension. dot === 0: dotfile with no extension (e.g. ``.sql``).
66+
if (dot <= 0) return false
67+
return SQL_EXTENSIONS.has(basename.slice(dot).toLowerCase())
6268
}
6369

6470
// ---------------------------------------------------------------------------

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

Lines changed: 19 additions & 3 deletions
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 } from "fs"
5+
import { readFileSync, existsSync, statSync } from "fs"
66
import { Glob } from "../../util/glob"
77
import path from "path"
88
import {
@@ -461,8 +461,10 @@ export const CheckCommand = cmd({
461461
// 3. Resolve files
462462
let files: string[] = args.files ?? []
463463
if (files.length === 0) {
464-
console.error("No files specified, searching for **/*.sql in current directory...")
465-
files = await Glob.scan("**/*.sql", { cwd: process.cwd(), absolute: true })
464+
console.error("No files specified, searching for **/*.{sql,ddl} in current directory...")
465+
const sqls = await Glob.scan("**/*.sql", { cwd: process.cwd(), absolute: true })
466+
const ddls = await Glob.scan("**/*.ddl", { cwd: process.cwd(), absolute: true })
467+
files = [...new Set([...sqls, ...ddls])]
466468
} else {
467469
// Expand globs in positional args
468470
const expanded: string[] = []
@@ -497,6 +499,20 @@ export const CheckCommand = cmd({
497499
console.error(`Warning: not a SQL file (extension "${ext}"), skipping: ${f}`)
498500
return false
499501
}
502+
// Reject directories (and symlinks-to-directories) whose name happens
503+
// to end in .sql / .ddl — statSync follows symlinks, so a symlink to
504+
// a dir is rejected. (cubic P2 round on release/v0.9.6 hotfix.)
505+
let st
506+
try {
507+
st = statSync(f)
508+
} catch (e) {
509+
console.error(`Warning: stat failed, skipping: ${f} (${(e as Error).message})`)
510+
return false
511+
}
512+
if (!st.isFile()) {
513+
console.error(`Warning: not a regular file (directory or other), skipping: ${f}`)
514+
return false
515+
}
500516
return true
501517
})
502518

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { describe, expect, test, beforeEach, beforeAll, afterAll, mock } from "bun:test"
1+
import { describe, expect, test, beforeEach, mock } from "bun:test"
22
import * as Dispatcher from "../../src/altimate/native/dispatcher"
33

4-
// Disable telemetry via env var instead of mock.module
5-
beforeAll(() => {
6-
process.env.ALTIMATE_TELEMETRY_DISABLED = "true"
7-
})
8-
afterAll(() => {
9-
delete process.env.ALTIMATE_TELEMETRY_DISABLED
10-
})
4+
// No telemetry disable needed — Dispatcher.call already wraps every
5+
// Telemetry.track call in try/catch that swallows all errors, so telemetry
6+
// firing during a test cannot fail the test. The previous env-var
7+
// mutation was defensive against nothing and wasn't parallel-safe (issue
8+
// #1130 — https://github.com/AltimateAI/altimate-code/issues/1130).
119

1210
describe("Dispatcher", () => {
1311
beforeEach(() => {

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,56 @@ describe("check command E2E", () => {
11121112
// ===========================================================================
11131113

11141114
describe("check command adversarial", () => {
1115+
test("handler filters non-SQL files from positional args — no content parsed or echoed", async () => {
1116+
// Handler-level integration test (cubic P2 catch on the pure-helper-only
1117+
// test coverage). This test would fail if CheckCommand.handler stopped
1118+
// calling isSqlFile — proving the filter is wired end-to-end, not just
1119+
// present as an unused helper. Reproduces the exact regression path:
1120+
// a non-SQL file containing "root:x:0" must NOT flow through the engine
1121+
// path where a rule message could echo its content to stdout.
1122+
const sqlFile = await writeSql(tmpDir.dir, "real.sql", "SELECT 1;")
1123+
const nonSql = path.join(tmpDir.dir, "fake-passwd-no-ext")
1124+
await fs.writeFile(nonSql, "root:x:0:0:root:/root:/bin/bash\n", "utf-8")
1125+
const r = await runHandler(baseArgs({ files: [sqlFile, nonSql], checks: "safety" }))
1126+
const j = parseJson(r.stdout)
1127+
// Only the .sql file made it through.
1128+
expect(j.files_checked).toBe(1)
1129+
// The non-SQL file was rejected with a clear warning on stderr.
1130+
expect(r.stderr).toMatch(/not a SQL file.*fake-passwd-no-ext/)
1131+
// Critically: no content leak. Neither the SQL output nor the warning
1132+
// contains any part of the non-SQL file's content.
1133+
expect(r.stdout).not.toMatch(/root:x:0/i)
1134+
expect(r.stderr).not.toMatch(/root:x:0/i)
1135+
})
1136+
1137+
test("handler rejects a directory whose name ends in .sql", async () => {
1138+
// cubic P2: extension check alone would accept ``foo.sql/`` as a file.
1139+
// The isFile() stat gate must reject it. When ALL positional files
1140+
// are rejected, check.ts's early "No SQL files found" bail-out fires
1141+
// and there is no JSON body — assert on stderr instead of parseJson.
1142+
const dirLikeFile = path.join(tmpDir.dir, "not-a-file.sql")
1143+
await fs.mkdir(dirLikeFile, { recursive: true })
1144+
const r = await runHandler(baseArgs({ files: [dirLikeFile], checks: "safety" }))
1145+
expect(r.stderr).toMatch(/not a regular file.*not-a-file\.sql/)
1146+
expect(r.stderr).toContain("No SQL files found to check")
1147+
// No content-leak surface at all when the file is rejected.
1148+
expect(r.stdout).not.toContain("not-a-file")
1149+
})
1150+
1151+
test("handler rejects a dotfile named exactly '.sql'", async () => {
1152+
// cubic P2: ``.sql`` as a filename is a dotfile with no extension,
1153+
// not a SQL file. Must be rejected before reaching the engine.
1154+
const dotSql = path.join(tmpDir.dir, ".sql")
1155+
await fs.writeFile(dotSql, "root:x:0:0:root:/root:/bin/bash\n", "utf-8")
1156+
const r = await runHandler(baseArgs({ files: [dotSql], checks: "safety" }))
1157+
// Same as above — all files rejected → "No SQL files found" bail-out.
1158+
expect(r.stderr).toMatch(/not a SQL file.*\.sql$/m)
1159+
expect(r.stderr).toContain("No SQL files found to check")
1160+
// Critical: the file's content NEVER reaches stdout or stderr.
1161+
expect(r.stdout).not.toMatch(/root:x:0/i)
1162+
expect(r.stderr).not.toMatch(/root:x:0/i)
1163+
})
1164+
11151165
test("handles SQL with embedded null bytes", async () => {
11161166
const file = await writeSql(tmpDir.dir, "null.sql", "SELECT 1;\0DROP TABLE users;")
11171167
const r = await runHandler(baseArgs({ files: [file], checks: "lint" }))
@@ -1294,14 +1344,19 @@ describe("check command adversarial", () => {
12941344
})
12951345

12961346
test("handles directory with .sql extension", async () => {
1347+
// Extension filter passes ``dir.sql`` (name ends in .sql) but the
1348+
// stat/isFile gate rejects it as "not a regular file" before it ever
1349+
// reaches the readFileSync call. The good.sql file still processes.
1350+
// (v0.9.6 hotfix: added statSync isFile() check.)
12971351
const good = await writeSql(tmpDir.dir, "good.sql", "SELECT 1;")
12981352
const dir = path.join(tmpDir.dir, "dir.sql")
12991353
await fs.mkdir(dir)
13001354

13011355
const r = await runHandler(baseArgs({ files: [good, dir], checks: "lint" }))
1302-
expect(r.stderr).toContain("Error reading")
1356+
expect(r.stderr).toContain("not a regular file")
1357+
expect(r.stderr).toContain("dir.sql")
13031358
const j = parseJson(r.stdout)
1304-
expect(j.files_checked).toBe(2)
1359+
expect(j.files_checked).toBe(1)
13051360
})
13061361

13071362
test("processes duplicate file args (each checked separately)", async () => {
@@ -1453,6 +1508,15 @@ describe("isSqlFile — extension filter (v0.9.6 sanity regression)", () => {
14531508
// Edge case: ``.query.sql`` is a hidden file with a real .sql extension.
14541509
expect(isSqlFile(".query.sql")).toBe(true)
14551510
})
1511+
test("basename that is JUST the extension (bare dotfile) is NOT SQL", () => {
1512+
// ``.sql`` and ``.ddl`` as filenames are dotfiles per node's
1513+
// ``path.extname`` — they have NO extension, they're just dot-prefixed
1514+
// names. Must not be accepted. (cubic P2 catch on release/v0.9.6 hotfix.)
1515+
expect(isSqlFile(".sql")).toBe(false)
1516+
expect(isSqlFile(".ddl")).toBe(false)
1517+
expect(isSqlFile("/some/dir/.sql")).toBe(false)
1518+
expect(isSqlFile("/some/dir/.ddl")).toBe(false)
1519+
})
14561520
test("directory paths (trailing slash) are not SQL files", () => {
14571521
expect(isSqlFile("/path/to/dir/")).toBe(false)
14581522
expect(isSqlFile("relative/dir/")).toBe(false)

‎packages/opencode/test/skill/release-v0.9.6-adversarial.test.ts‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,15 @@
2929
* hit ``await _registrationPromise`` by the time control returns to us —
3030
* we can immediately act on shared state without racing the call's setup.
3131
*/
32-
import { afterEach, beforeAll, afterAll, beforeEach, describe, expect, test } from "bun:test"
32+
import { afterEach, beforeEach, describe, expect, test } from "bun:test"
3333

3434
import * as Dispatcher from "../../src/altimate/native/dispatcher"
3535

36-
let _priorTelemetryDisabled: string | undefined
37-
beforeAll(() => {
38-
_priorTelemetryDisabled = process.env.ALTIMATE_TELEMETRY_DISABLED
39-
process.env.ALTIMATE_TELEMETRY_DISABLED = "true"
40-
})
41-
afterAll(() => {
42-
// Restore any pre-existing value rather than unconditionally deleting —
43-
// an outer suite may have set it and expects to see its own value after
44-
// this file runs. (cubic P2 round 3.)
45-
if (_priorTelemetryDisabled === undefined) {
46-
delete process.env.ALTIMATE_TELEMETRY_DISABLED
47-
} else {
48-
process.env.ALTIMATE_TELEMETRY_DISABLED = _priorTelemetryDisabled
49-
}
50-
})
36+
// No process.env mutation — Dispatcher.call already wraps every
37+
// Telemetry.track in try/catch that swallows errors, so telemetry firing
38+
// during a test cannot fail the test. Prior save/restore of
39+
// ALTIMATE_TELEMETRY_DISABLED wasn't parallel-safe under bun test
40+
// (coderabbit MAJOR on v0.9.6 hotfix, issue #1130). Removed cleanly here.
5141

5242
describe("v0.9.6 release: Dispatcher registration retry", () => {
5343
beforeEach(() => {

0 commit comments

Comments
 (0)