sqlite: re-validate database state after reading options - #65595
Open
TrevorBurnham wants to merge 1 commit into
Open
sqlite: re-validate database state after reading options#65595TrevorBurnham wants to merge 1 commit into
TrevorBurnham wants to merge 1 commit into
Conversation
Collaborator
|
Review requested:
|
prepare(), function(), aggregate(), deserialize(), applyChangeset() and backup() validated the connection, then read their options bag with Object::Get(). A property getter runs arbitrary JavaScript at that point, so a getter calling close() invalidates what was just checked. Five of the six then passed a null sqlite3* to SQLite and crashed; prepare() reported a spurious "out of memory". Re-check IsOpen() after option parsing, immediately before the SQLite call, keeping the early check so invalid calls still fail before any user code runs. IsOpen() is the only condition a getter can change: authorizer and callback depths are RAII-managed. createSession() already parsed options first, so it only gains the early check. deserialize() also latched the buffer length before reading options.dbName. A getter that shrank the backing store left the length too large; CopyContents() then handed the uninitialized remainder to SQLite, from where serialize() returned it to JavaScript. Check the CopyContents() result instead of discarding it. function() and aggregate() cast the callback's length property with As<Int32>() and no IsInt32() guard. length is configurable, so any type reached the cast and produced a silently wrong arity. Fixes: nodejs#65586 Signed-off-by: Trevor Burnham <trevorburnham@gmail.com>
TrevorBurnham
force-pushed
the
sqlite-option-getter-toctou
branch
from
August 27, 2026 15:58
39de389 to
d3e10a8
Compare
TrevorBurnham
marked this pull request as ready for review
August 27, 2026 16:56
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.
Fixes #65586
Several
DatabaseSyncmethods checkIsOpen()and then read their options bag withObject::Get(). A user-supplied property getter runs arbitrary JavaScript at that point, so a getter that callsclose()invalidates the check before the resultingsqlite3*is used.function(),aggregate(),deserialize(),applyChangeset(),backup(). All butprepare()all segfault in this scenario.prepare()reports a spuriousERR_SQLITE_ERROR: out of memoryinstead, becausesqlite3_prepare_v3()null-checks its argument.The fix here is to re-check
IsOpen()after option parsing, immediately before the SQLite call. The existing early check is kept: moving it would make attacker-controlled getters run on calls that are already doomed to throw, which is the wrong direction for a hardening change, and would reorder existing error precedence.createSession()already parsed its options before validating. It gains the matching early check here for consistency.Two related fixes in the same code:
deserialize()latchedinput->ByteLength()before readingoptions.dbName. A getter that shrinks the backingArrayBufferleaves the latched length too large,CopyContents()copies only what remains, its return value is discarded, and the uninitialized remainder is handed to SQLite, whereserialize()returns it to JavaScript. TheCopyContents()result is now checked.function()andaggregate()cast the callback'slengthwithAs<Int32>()and noIsInt32()guard.lengthis configurable, so'abc',{},NaNand1.5all reached the cast and registered a silently wrong arity. Now validated, matching the five sites in this file that already do.Verification
New
test/parallel/test-sqlite-options-getter-reentry.jscovers all seven sites, buffer shrink and detach, and thelengthtype checks.Compatibility note
Rejecting a non-integer
fn.lengthwithERR_INVALID_ARG_TYPEis a behavior change, though it only affects functions whoselengthhas been redefined.