sqlite: reject connection access from authorizer callbacks - #65156
Draft
TrevorBurnham wants to merge 1 commit into
Draft
sqlite: reject connection access from authorizer callbacks#65156TrevorBurnham wants to merge 1 commit into
TrevorBurnham wants to merge 1 commit into
Conversation
Collaborator
|
Review requested:
|
meixg
approved these changes
Aug 9, 2026
TrevorBurnham
force-pushed
the
sqlite-authorizer-reentry
branch
2 times, most recently
from
August 9, 2026 19:14
3536092 to
47307a2
Compare
SQLite requires that an authorizer callback not modify the connection that invoked it, and counts sqlite3_prepare_v2() and sqlite3_step() as modifications. node:sqlite let an authorizer callback call prepare(), exec(), the statement execution methods, and other connection-mutating APIs on the same DatabaseSync. Track authorizer depth on DatabaseSync with an RAII guard around the callback, and throw ERR_INVALID_STATE from the affected entry points while the callback is on the stack. The depth is per-connection, so other connections stay usable from the callback. The guard covers every authorizer invocation, not just those from an explicit prepare(), since SQLite may re-prepare a statement during sqlite3_step() after a schema change. serialize() and the session changeset() and patchset() methods prepare statements internally, so they re-enter the authorizer too. Reentry through changeset() does not terminate: it recurses until the process is killed, with no way to catch it from JavaScript. Finalizing a statement is a separate hazard. It frees the virtual machine that sqlite3_step() is executing, which crashes rather than throwing, and any callback SQLite invokes during execution can reach it, not only an authorizer. Track the statements currently being stepped and reject finalizing one of those, so a user-defined function can still prepare and finalize its own helper statements. Disposal stays idempotent, since throwing for an already-finalized statement would demote a `using` scope's exception to a SuppressedError. Signed-off-by: Trevor Burnham <trevorburnham@gmail.com> Fixes: nodejs#63207 Assisted-by: claude:opus-5
TrevorBurnham
force-pushed
the
sqlite-authorizer-reentry
branch
from
August 10, 2026 14:12
47307a2 to
b1eea0e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Per the
sqlite3_set_authorizer()docs, the authorizer callback must not do anything that modifies the database connection that invoked it, andsqlite3_prepare_v2()andsqlite3_step()both count.node:sqliteallowed an authorizer callback to callprepare(),exec(), the statement execution methods, and other connection-mutating APIs on the sameDatabaseSync.Authorizer reentrancy
Track authorizer depth on
DatabaseSyncwith an RAII guard around the callback, and throwERR_INVALID_STATEfrom the affected entry points while the callback is on the stack. Depth is per-connection, so a differentDatabaseSyncstays usable from inside the callback.Guarded:
prepare,exec,serialize,deserialize,setAuthorizer,createSession,applyChangeset,createTagStore,function,aggregate,enableLoadExtension,enableDefensive,loadExtension, thelimitssetter;stmt.run/get/all/iterate;iter.next/return;sqlTagStore.run/get/all/iterate/clear;session.changeset/patchset.db.close()was already rejected by the existing callback-depth guard and keeps its current message.The guard covers every authorizer invocation, not just those from an explicit
prepare(), since SQLite may re-prepare a statement duringsqlite3_step()after a schema change.serialize()and the session changeset methods prepare statements internally, so they re-enter the authorizer too; reentry throughchangeset()never terminates, recursing until the process is killed with no way to catch it from JavaScript.Finalize-during-step
Covering the re-prepare path surfaced a segfault rather than a contract violation: finalizing a statement frees the virtual machine that
sqlite3_step()is executing. This isn't specific to authorizers. Any callback SQLite invokes during execution can reach it, including a user-defined function:Gating
close()on "any callback is running" would forbid a UDF from preparing and finalizing its own helper statement, which is safe. Instead this tracks the statements currently being stepped and rejects only those, withstatement cannot be finalized while it is being executed. A UDF can still dousing s = db.prepare(...). Tracking is a stack, so a UDF that steps an inner statement may finalize that inner statement but not the outer one still understep().statement[Symbol.dispose]()returns early when already finalized, keeping disposal idempotent; throwing there would demote ausingscope's real exception to aSuppressedError.Notes for reviewers
node:sqliteis Stability 1.2, so this changes behavior directly rather than through a deprecation cycle, and throws immediately rather than deferring, per the discussion in the issue.backup()andSession.close()are reachable from an authorizer and deliberately left unguarded.backup()schedulessqlite3_backup_stepon the threadpool, but that call takes the source connection's mutex and blocks until the in-progress step finishes; 120 stress iterations atrate: 1produced no corruption. Deleting a session object doesn't touch the VM under step. Guarding either would cost working behavior for symmetry alone, but I'll add them if you disagree.sqlTagStore.clear()only clears a JS-side cache. Guarded because the issue lists it; easy to drop.iterator.next()on a drained iterator throws inside an authorizer rather than returning{ done: true }. A drained iterator is still an object whose method the contract forbids calling, but I don't feel strongly.Verification
test-webstorage. 31 tests intest-sqlite-authz.js, 12 intest-sqlite-udf-close.js.make format-cppreports no changes;lint-cpp,lint-md, eslint,checkimports.py, andcpplint.pyclean.prepare()guard fails 3 tests. Both pass again on restore.Fixes: #63207