Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.7k
sqlite: validate connection after reading options#65591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+166
−0
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| 'use strict'; | ||
| const { skipIfSQLiteMissing } = require('../common'); | ||
| skipIfSQLiteMissing(); | ||
| const tmpdir = require('../common/tmpdir'); | ||
| const assert = require('node:assert'); | ||
| const { join } = require('node:path'); | ||
| const { test } = require('node:test'); | ||
| const { backup, DatabaseSync } = require('node:sqlite'); | ||
| tmpdir.refresh(); | ||
| const closedError = { | ||
| code: 'ERR_INVALID_STATE', | ||
| message: 'database is not open', | ||
| }; | ||
| // Reading the options bag runs a user getter, so the connection validated on | ||
| // entry can already be closed by the time the method reaches SQLite. | ||
| test('function() with a getter that closes the database', () => { | ||
| const db = new DatabaseSync(':memory:'); | ||
| const options = { | ||
| get useBigIntArguments() { | ||
| db.close(); | ||
| return false; | ||
| }, | ||
| }; | ||
| assert.throws(() => { | ||
| db.function('custom', options, () => 1); | ||
| }, closedError); | ||
| }); | ||
| test('aggregate() with a getter that closes the database', () => { | ||
| const db = new DatabaseSync(':memory:'); | ||
| const options = { | ||
| start: 0, | ||
| step: (acc, value) => acc + value, | ||
| get useBigIntArguments() { | ||
| db.close(); | ||
| return false; | ||
| }, | ||
| }; | ||
| assert.throws(() => { | ||
| db.aggregate('custom', options); | ||
| }, closedError); | ||
| }); | ||
| test('deserialize() with a getter that closes the database', () => { | ||
| const source = new DatabaseSync(':memory:'); | ||
| source.exec('CREATE TABLE data (value INTEGER)'); | ||
| const serialized = source.serialize(); | ||
| source.close(); | ||
| const db = new DatabaseSync(':memory:'); | ||
| const options = { | ||
| get dbName() { | ||
| db.close(); | ||
| return 'main'; | ||
| }, | ||
| }; | ||
| assert.throws(() => { | ||
| db.deserialize(serialized, options); | ||
| }, closedError); | ||
| }); | ||
| test('prepare() with a getter that closes the database', () => { | ||
| const db = new DatabaseSync(':memory:'); | ||
| const options = { | ||
| get persistent() { | ||
| db.close(); | ||
| return false; | ||
| }, | ||
| }; | ||
| assert.throws(() => { | ||
| db.prepare('SELECT 1', options); | ||
| }, closedError); | ||
| }); | ||
| test('applyChangeset() with a getter that closes the database', () => { | ||
| const source = new DatabaseSync(':memory:'); | ||
| source.exec('CREATE TABLE data (value INTEGER PRIMARY KEY)'); | ||
| const session = source.createSession(); | ||
| source.exec('INSERT INTO data VALUES (1)'); | ||
| const changeset = session.changeset(); | ||
| source.close(); | ||
| const db = new DatabaseSync(':memory:'); | ||
| db.exec('CREATE TABLE data (value INTEGER PRIMARY KEY)'); | ||
| const options = { | ||
| get onConflict() { | ||
| db.close(); | ||
| return undefined; | ||
| }, | ||
| }; | ||
| assert.throws(() => { | ||
| db.applyChangeset(changeset, options); | ||
| }, closedError); | ||
| }); | ||
| test('tag store with a getter that closes the database', () => { | ||
| const db = new DatabaseSync(':memory:'); | ||
| const sql = db.createTagStore(10); | ||
| const strings = ['SELECT ', '']; | ||
| Object.defineProperty(strings, 0, { | ||
| get() { | ||
| db.close(); | ||
| return 'SELECT '; | ||
| }, | ||
| }); | ||
| assert.throws(() => { | ||
| sql.get(strings, 1); | ||
| }, closedError); | ||
| }); | ||
| test('backup() with a getter that closes the database', () => { | ||
| const db = new DatabaseSync(':memory:'); | ||
| db.exec('CREATE TABLE data (value INTEGER)'); | ||
| const options = { | ||
| get rate() { | ||
| db.close(); | ||
| return 1; | ||
| }, | ||
| }; | ||
| assert.throws(() => { | ||
| backup(db, join(tmpdir.path, 'backup.db'), options); | ||
| }, closedError); | ||
| }); |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can just check only once after arguments validation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. I kept the early check on purpose, and the fix that landed in #65595 made the same call.
Checking only after argument validation changes which error wins when the database is already closed. Two existing tests depend on the current order:
test/parallel/test-sqlite-database-sync.js:379callsdb.prepare()with no arguments on a closed database and expectsERR_INVALID_STATE. Without the early check the missingsqlargument throwsERR_INVALID_ARG_TYPEfirst.test/parallel/test-sqlite-serialize.js:182callsdb.deserialize(new Uint8Array(0))on a closed database and expectsERR_INVALID_STATE. Without the early check it becomesERR_INVALID_ARG_VALUE. I applied that change and rebuilt to confirm it.The early check also rejects the call while an authorizer callback is running, so the options getters never run re-entrantly there.
Since #65595 covers the same six functions, I am closing this PR.