GeezerVison is showing github.com — sqlite: copy changeset before applying it · nodejs/node@caedbe3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +29-7Lines changed: 29 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,6 +25,7 @@ namespace sqlite { | 25 | 25 |
| 26 | 26 | using v8::Array; | 27 | 27 | using v8::ArrayBuffer; | | 28 | +using v8::BackingStore; | 28 | 29 | using v8::BackingStoreInitializationMode; | 29 | 30 | using v8::BackingStoreOnFailureMode; | 30 | 31 | using v8::BigInt; | @@ -2533,21 +2534,42 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) { | 2533 | 2534 | } | 2534 | 2535 | } | 2535 | 2536 |
| 2536 | | - // Keep the database alive during sqlite3changeset_apply(), which may | 2537 | | - // call conflict or filter callbacks that trigger JavaScript execution. | 2538 | | - // If the JavaScript callback drops all references to the database, | 2539 | | - // the DatabaseSync could otherwise be garbage-collected while the | 2540 | | - // callback is still executing, causing a use-after-free. | | 2537 | + // Keep the database alive in case a callback drops all references to it, | | 2538 | + // which could otherwise let it be garbage-collected mid-callback. | 2541 | 2539 | BaseObjectPtr<DatabaseSync> guard(db); | 2542 | 2540 |
| 2543 | 2541 | ArrayBufferViewContents<uint8_t> buf(args[0]); | | 2542 | + if (buf.length() > std::numeric_limits<int>::max()) { | | 2543 | + THROW_ERR_OUT_OF_RANGE(env, "The changeset is too large."); | | 2544 | + return; | | 2545 | + } | | 2546 | + | | 2547 | + // A callback may detach/modify the input buffer mid-apply, so copy it. | | 2548 | + // With no callbacks, no JS runs during sqlite3changeset_apply(), so no | | 2549 | + // copy is needed. | | 2550 | + std::unique_ptr<BackingStore> changeset; | | 2551 | + if (buf.length() > 0 && | | 2552 | + (context.filterCallback || context.conflictCallback)) { | | 2553 | + changeset = ArrayBuffer::NewBackingStore( | | 2554 | + env->isolate(), | | 2555 | + buf.length(), | | 2556 | + BackingStoreInitializationMode::kUninitialized, | | 2557 | + BackingStoreOnFailureMode::kReturnNull); | | 2558 | + if (!changeset) { | | 2559 | + THROW_ERR_MEMORY_ALLOCATION_FAILED(env); | | 2560 | + return; | | 2561 | + } | | 2562 | + std::memcpy(changeset->Data(), buf.data(), buf.length()); | | 2563 | + } | | 2564 | + | 2544 | 2565 | int r; | 2545 | 2566 | { | 2546 | 2567 | CallbackDepthGuard guard(db); | 2547 | 2568 | r = sqlite3changeset_apply( | 2548 | 2569 | db->connection_, | 2549 | | - buf.length(), | 2550 | | - const_cast<void*>(static_cast<const void*>(buf.data())), | | 2570 | + static_cast<int>(buf.length()), | | 2571 | + changeset ? changeset->Data() | | 2572 | + : const_cast<void*>(static_cast<const void*>(buf.data())), | 2551 | 2573 | context.filterCallback ? xFilter : nullptr, | 2552 | 2574 | xConflict, | 2553 | 2575 | static_cast<void*>(&context)); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-session.js +48Lines changed: 48 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -282,6 +282,25 @@ suite('conflict resolution', () => { | 282 | 282 | [{ value: 'world' }]); // unchanged | 283 | 283 | }); | 284 | 284 |
| | 285 | + test('database.applyChangeset() - changeset detached by onConflict', (t) => { | | 286 | + const { database2, changeset } = prepareConflict(); | | 287 | + const result = database2.applyChangeset(changeset, { | | 288 | + onConflict: () => { | | 289 | + const transferred = structuredClone(changeset.buffer, { | | 290 | + transfer: [changeset.buffer], | | 291 | + }); | | 292 | + new Uint8Array(transferred).fill(0); | | 293 | + return constants.SQLITE_CHANGESET_REPLACE; | | 294 | + } | | 295 | + }); | | 296 | + | | 297 | + t.assert.strictEqual(result, true); | | 298 | + t.assert.strictEqual(changeset.byteLength, 0); | | 299 | + deepStrictEqual(t)( | | 300 | + database2.prepare('SELECT * FROM data ORDER BY key').all(), | | 301 | + [{ key: 1, value: 'hello' }, { key: 2, value: 'foo' }]); | | 302 | + }); | | 303 | + | 285 | 304 | test('database.applyChangeset() - SQLITE_CHANGESET_DATA conflict handled with SQLITE_CHANGESET_REPLACE', (t) => { | 286 | 305 | const { database2, changeset } = prepareDataConflict(); | 287 | 306 | let conflictType = null; | @@ -413,6 +432,35 @@ test('filter handler throws', (t) => { | 413 | 432 | }); | 414 | 433 | }); | 415 | 434 |
| | 435 | +test('database.applyChangeset() - changeset detached by filter', (t) => { | | 436 | + const database1 = new DatabaseSync(':memory:'); | | 437 | + const database2 = new DatabaseSync(':memory:'); | | 438 | + database1.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); | | 439 | + database2.exec('CREATE TABLE data(key INTEGER PRIMARY KEY)'); | | 440 | + | | 441 | + const session = database1.createSession(); | | 442 | + database1.exec('INSERT INTO data VALUES (1), (2), (3)'); | | 443 | + const changeset = session.changeset(); | | 444 | + | | 445 | + const result = database2.applyChangeset(changeset, { | | 446 | + filter: () => { | | 447 | + const transferred = structuredClone(changeset.buffer, { | | 448 | + transfer: [changeset.buffer], | | 449 | + }); | | 450 | + new Uint8Array(transferred).fill(0); | | 451 | + return true; | | 452 | + } | | 453 | + }); | | 454 | + | | 455 | + t.assert.strictEqual(result, true); | | 456 | + t.assert.strictEqual(changeset.byteLength, 0); | | 457 | + deepStrictEqual(t)(database2.prepare('SELECT * FROM data').all(), [ | | 458 | + { key: 1 }, | | 459 | + { key: 2 }, | | 460 | + { key: 3 }, | | 461 | + ]); | | 462 | +}); | | 463 | + | 416 | 464 | test('database.createSession() - filter changes', (t) => { | 417 | 465 | const database1 = new DatabaseSync(':memory:'); | 418 | 466 | const database2 = new DatabaseSync(':memory:'); | |
|