From b49495d9fc63d932301e868f4f6cc154c21bee6b Mon Sep 17 00:00:00 2001 From: Ultranmus Date: Wed, 2 Dec 2026 00:26:28 +0530 Subject: [PATCH] sqlite: roll back applyChangeset() when filter throws When the filter callback of applyChangeset() throws, xFilter can only tell SQLite to skip the remaining tables, so sqlite3changeset_apply() returns SQLITE_OK and the tables accepted before the exception stay applied. An exception from onConflict, by contrast, aborts and rolls back the whole apply. Wrap the apply in a savepoint when a filter is given and roll back to it when the filter threw. A savepoint is used instead of a transaction so that a transaction the caller already has open, and the writes made in it before the call, are kept. Fixes: https://github.com/nodejs/node/issues/67841 Signed-off-by: Ultranmus --- doc/api/sqlite.md | 4 ++ src/node_sqlite.cc | 42 +++++++++++++++++++ test/parallel/test-sqlite-session.js | 62 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 1ed1dd74bf5a..877eeeec79cd 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -1069,6 +1069,10 @@ added: attempt is made to apply any changes to the table. Otherwise, if the return value is truthy or no `filter` callback is provided, all changes related to the table are attempted. + When an error is thrown in the `filter` callback, applying the changeset is + aborted, any changes already applied by this call are rolled back, and the + error is rethrown. Changes made before the call, including those in a + transaction that is still open, are kept. * `onConflict` {Function} A function that determines how to handle conflicts. The function receives one argument, which can be one of the following values: diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 001718d853e0..200ca88b3a5f 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -3211,7 +3211,22 @@ void Database::ApplyChangeset(const FunctionCallbackInfo& args) { std::memcpy(changeset->Data(), buf.data(), buf.length()); } + // A filter callback that throws can only make SQLite skip the remaining + // tables (xFilter cannot abort the apply), so tables accepted before the + // exception would stay applied. Wrap the apply in a savepoint so it can be + // undone in that case. A savepoint, rather than a transaction, leaves any + // transaction the caller already has open, and its earlier writes, intact. int r; + const bool use_savepoint = static_cast(context.filterCallback); + if (use_savepoint) { + r = sqlite3_exec(db->connection_.get(), + "SAVEPOINT node_sqlite_apply_changeset", + nullptr, + nullptr, + nullptr); + CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void()); + } + { CallbackDepthGuard guard(db); r = sqlite3changeset_apply( @@ -3223,6 +3238,33 @@ void Database::ApplyChangeset(const FunctionCallbackInfo& args) { xConflict, static_cast(&context)); } + if (use_savepoint) { + // Mirrors how sqlite3changeset_apply() handles its own savepoint. + int release_r = SQLITE_ERROR; + if (!filterCallbackFailed) { + release_r = sqlite3_exec(db->connection_.get(), + "RELEASE node_sqlite_apply_changeset", + nullptr, + nullptr, + nullptr); + } + if (release_r != SQLITE_OK) { + sqlite3_exec(db->connection_.get(), + "ROLLBACK TO node_sqlite_apply_changeset", + nullptr, + nullptr, + nullptr); + sqlite3_exec(db->connection_.get(), + "RELEASE node_sqlite_apply_changeset", + nullptr, + nullptr, + nullptr); + } + // The exception thrown by the filter callback is still pending and is + // propagated to JavaScript. + if (filterCallbackFailed) return; + if (r == SQLITE_OK) r = release_r; + } if (r == SQLITE_OK) { args.GetReturnValue().Set(true); return; diff --git a/test/parallel/test-sqlite-session.js b/test/parallel/test-sqlite-session.js index 06d4bedd68b3..703820949a86 100644 --- a/test/parallel/test-sqlite-session.js +++ b/test/parallel/test-sqlite-session.js @@ -433,6 +433,68 @@ test('filter handler throws', (t) => { }); }); +suite('filter handler throws after accepting a table', () => { + const createTableSql = ` + CREATE TABLE a(id INTEGER PRIMARY KEY, v); + CREATE TABLE b(id INTEGER PRIMARY KEY, v); + CREATE TABLE marker(id INTEGER); + `; + + function prepare() { + const source = new Database(':memory:'); + const target = new Database(':memory:'); + source.exec(createTableSql); + target.exec(createTableSql); + const session = source.createSession(); + source.exec("INSERT INTO a VALUES (1, 'x'); INSERT INTO b VALUES (1, 'y')"); + return { target, changeset: session.changeset() }; + } + + function count(db, table) { + return db.prepare(`SELECT count(*) AS n FROM ${table}`).get().n; + } + + const filterError = new Error('filter failed for b'); + const filter = (table) => { + if (table === 'b') throw filterError; + return true; + }; + + test('rolls back the tables applied before the exception', (t) => { + const { target, changeset } = prepare(); + t.assert.throws(() => target.applyChangeset(changeset, { filter }), + (err) => err === filterError); + t.assert.strictEqual(count(target, 'a'), 0); + t.assert.strictEqual(count(target, 'b'), 0); + t.assert.strictEqual(target.isTransaction, false); + }); + + test('keeps an open outer transaction and its earlier writes', (t) => { + const { target, changeset } = prepare(); + target.exec('BEGIN; INSERT INTO marker VALUES (7)'); + t.assert.throws(() => target.applyChangeset(changeset, { filter }), + (err) => err === filterError); + t.assert.strictEqual(target.isTransaction, true); + t.assert.strictEqual(count(target, 'a'), 0); + t.assert.strictEqual(count(target, 'b'), 0); + t.assert.strictEqual(count(target, 'marker'), 1); + target.exec('INSERT INTO marker VALUES (8); COMMIT'); + t.assert.strictEqual(count(target, 'marker'), 2); + }); + + test('a filter that does not throw leaves the outer transaction open', + (t) => { + const { target, changeset } = prepare(); + target.exec('BEGIN'); + t.assert.strictEqual( + target.applyChangeset(changeset, { filter: () => true }), true); + t.assert.strictEqual(target.isTransaction, true); + target.exec('ROLLBACK'); + t.assert.strictEqual(count(target, 'a'), 0); + t.assert.strictEqual(count(target, 'b'), 0); + }); +}); + test('database.applyChangeset() - changeset detached by filter', (t) => { const database1 = new Database(':memory:'); const database2 = new Database(':memory:');