Skip to content

Commit 9ae7d77

Browse files
anandgupta42claude
andcommitted
fix(analyst): DuckDB read-only guard uses txid_current, dynamic query() is refused, estimate-lookup infrastructure errors propagate
- The DuckDB read-only guard probes the transaction id with `txid_current()`, which every supported DuckDB release exposes, instead of `current_transaction_id()` - DuckDB `query()` executes the SQL in its string argument, so a masked literal could smuggle an already-denied call into the probe; it is now part of the denied function class. `pg_ls_*dir` is no longer allowlisted (server directory listings are file access); redundant allowlist/denylist entries removed - The bare-table estimate lookup re-throws a lost helper session (`stateLost`) and any other infrastructure error instead of treating it as an unknown estimate; only recognised missing-statistics or catalog-access failures still mean unknown, so a probe never runs under silently reset session state - tests: query() smuggling and pg_ls_dir/pg_ls_waldir rejected; lost-session estimate error propagates; missing-catalog error still yields an unknown estimate Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 5a69583 commit 9ae7d77

3 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎packages/drivers/src/local-engine-child-source.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ async function withReadOnly(p, execute) {
238238
// The legacy binding has no autocommit getter. Implicit transactions receive
239239
// different IDs; an explicit transaction keeps the same ID across statements.
240240
// Refuse before BEGIN: a nested BEGIN would abort the caller's transaction.
241-
const first = await control("SELECT current_transaction_id() AS id");
242-
const second = await control("SELECT current_transaction_id() AS id");
241+
const first = await control("SELECT txid_current() AS id");
242+
const second = await control("SELECT txid_current() AS id");
243243
if (first[0].id === second[0].id) {
244244
throw new Error("DuckDB read-only execution cannot run inside an active transaction; finish it before running the probe.");
245245
}

‎packages/opencode/src/altimate/tools/sql-check.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const PURE_PG_FUNCTIONS =
1919
"snapshot_\\w*|visible_in_snapshot|notification_queue_usage|listening_channels|blocking_pids|" +
2020
"safe_snapshot_blocking_pids|isolation_test_session_is_blocked|stat_(?!reset)\\w*|statio_\\w*|mcv_list_\\w*|" +
2121
"index\\w*_has_property|options_to_table|settings_get_flags|size_bytes|lsn\\w*|partition_\\w*|" +
22-
"indexes_size|table_is_visible|has_\\w*_privilege|get_userbyid|database_collation_actual_version|" +
23-
"collation_actual_version|available_extension\\w*|control_\\w*|ls_\\w*dir|cursor\\w*|event_trigger_\\w*"
22+
"table_is_visible|has_\\w*_privilege|database_collation_actual_version|" +
23+
"collation_actual_version|available_extension\\w*|control_\\w*|cursor\\w*|event_trigger_\\w*"
2424
const SIDE_EFFECT_FUNCTION_CALL = new RegExp(
25-
"\\b(nextval|setval|set_config|load_extension|writefile|dblink\\w*|lo_\\w*|lowrite|loread|lo_creat|checkpoint|force_checkpoint|" +
25+
"\\b(nextval|setval|set_config|load_extension|writefile|dblink\\w*|lo_\\w*|lowrite|loread|checkpoint|force_checkpoint|query|" +
2626
`pg_(?!(?:${PURE_PG_FUNCTIONS})\\s*\\()\\w*)\\s*\\(`,
2727
"i",
2828
)
@@ -311,8 +311,15 @@ async function tableEstimate(table: string, dialect: Dialect, run: (sql: string)
311311
return estimates.length ? Math.max(...estimates) : undefined
312312
} catch (error) {
313313
if (isQueryTimeoutError(error)) throw error
314-
// Missing ANALYZE statistics or unavailable catalog access means unknown.
315-
return undefined
314+
// A lost helper session or any other infrastructure failure must reach the caller: the probe
315+
// would otherwise run under reset session state. Only recognised missing-statistics or
316+
// catalog-access failures mean "unknown estimate".
317+
if (typeof error === "object" && error !== null && "stateLost" in error) throw error
318+
const message = error instanceof Error ? error.message : String(error)
319+
if (/no such table|does not exist|sqlite_stat1|pragma_table_list|permission denied|insufficient privilege|catalog/i.test(message)) {
320+
return undefined
321+
}
322+
throw error
316323
}
317324
}
318325

‎packages/opencode/test/altimate/sql-check-postgres.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ describe("predicate_effect with mocked Postgres", () => {
152152
"checkpoint()",
153153
"force_checkpoint(current_database())",
154154
"pg_some_future_control_function(1)",
155+
"query('SELECT force_checkpoint(current_database())')",
156+
"pg_ls_dir('.')",
157+
"pg_ls_waldir()",
155158
],
156159
],
157160
[
@@ -402,6 +405,28 @@ describe("predicate_effect with mocked Postgres", () => {
402405
expect(execute.mock.calls[0][0]).not.toContain("COUNT(*)")
403406
})
404407

408+
test("a lost helper session during the estimate lookup reaches the caller instead of becoming an unknown estimate", async () => {
409+
const execute = postgres("r", 5, (sql) => {
410+
if (/pg_class/.test(sql)) throw Object.assign(new Error("session state was lost"), { code: "SESSION_STATE_LOST", stateLost: true })
411+
return sql
412+
})
413+
await expect(
414+
checkSql({ warehouse, check: "predicate_effect", base_sql: "orders", predicates: ["id > 2"] }),
415+
).rejects.toMatchObject({ stateLost: true })
416+
expect(execute).toHaveBeenCalledTimes(1)
417+
})
418+
419+
test("a missing-catalog error during the estimate lookup still means an unknown estimate", async () => {
420+
const execute = postgres("r", 5, (sql) => {
421+
if (/pg_class/.test(sql)) throw new Error('relation "pg_catalog.pg_class" does not exist')
422+
return sql
423+
})
424+
const result = await checkSql({ warehouse, check: "predicate_effect", base_sql: "orders", predicates: ["id > 2"] })
425+
expect(result.metadata).toMatchObject({ total_rows: 5, combined_kept_rows: 3 })
426+
expect(result.output).toContain(unknownEstimate)
427+
expect(execute).toHaveBeenCalledTimes(2)
428+
})
429+
405430
test("allows a known estimate equal to the cap and does not label it unknown", async () => {
406431
const execute = postgres()
407432
const result = await checkSql({

0 commit comments

Comments
 (0)