From f25ae20bc431f1df644af385a9b901052e7563f4 Mon Sep 17 00:00:00 2001 From: Christian Aurich Zanettini Martins Date: Mon, 16 Nov 2026 00:59:50 -0300 Subject: [PATCH] sqlite: fix abort on oversized SQL text `expandedSQL`, `sourceSQL`, column names and the `sqlite.db.query` trace callback passed SQL text to `String::NewFromUtf8()` without a length, and V8 aborted on text longer than `String::kMaxLength`. Pass the length instead. The getters and column names now throw `ERR_STRING_TOO_LONG`, and the trace callback skips the event so the statement still runs. Co-authored-by: lazerg Signed-off-by: Christian Aurich Zanettini Martins --- src/node_sqlite.cc | 24 ++++++++++++----- test/pummel/test-sqlite-statement-too-long.js | 26 ++++++++++++++++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 2f4050cb8c16..c92dcc801ecb 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -3386,10 +3386,17 @@ int Database::TraceCallback(unsigned int type, Isolate* isolate = env->isolate(); HandleScope handle_scope(isolate); + // With an explicit length, V8 returns an empty handle instead of aborting + // when the SQL text is longer than String::kMaxLength. The event is then + // skipped, so that tracing does not make the statement fail. char* expanded = sqlite3_expanded_sql(static_cast(p)); Local sql_string; if (expanded != nullptr) { - bool ok = String::NewFromUtf8(isolate, expanded).ToLocal(&sql_string); + bool ok = String::NewFromUtf8(isolate, + expanded, + NewStringType::kNormal, + static_cast(strlen(expanded))) + .ToLocal(&sql_string); sqlite3_free(expanded); if (!ok) { return 0; @@ -3398,7 +3405,11 @@ int Database::TraceCallback(unsigned int type, // Fallback to source SQL if expanded is unavailable const char* source = sqlite3_sql(static_cast(p)); if (source == nullptr || - !String::NewFromUtf8(isolate, source).ToLocal(&sql_string)) { + !String::NewFromUtf8(isolate, + source, + NewStringType::kNormal, + static_cast(strlen(source))) + .ToLocal(&sql_string)) { return 0; } } @@ -3698,9 +3709,7 @@ MaybeLocal Statement::ColumnNameToName(const int column) { return MaybeLocal(); } - return String::NewFromUtf8( - env()->isolate(), col_name, NewStringType::kInternalized) - .As(); + return NullableSQLiteStringToValue(env()->isolate(), col_name).As(); } // Size of the stack array RowToObject passes to the template. @@ -4140,7 +4149,8 @@ void Statement::SourceSQLGetter(const FunctionCallbackInfo& args) { THROW_AND_RETURN_ON_BAD_STATE( env, stmt->IsFinalized(), "statement has been finalized"); Local sql; - if (!String::NewFromUtf8(env->isolate(), sqlite3_sql(stmt->statement_.get())) + if (!Utf8StringMaybeOneByte(env->isolate(), + sqlite3_sql(stmt->statement_.get())) .ToLocal(&sql)) { return; } @@ -4160,7 +4170,7 @@ void Statement::ExpandedSQLGetter(const FunctionCallbackInfo& args) { return THROW_ERR_SQLITE_ERROR( env->isolate(), "Expanded SQL text would exceed configured limits"); } - auto maybe_expanded = String::NewFromUtf8(env->isolate(), expanded); + auto maybe_expanded = Utf8StringMaybeOneByte(env->isolate(), expanded); sqlite3_free(expanded); Local result; if (!maybe_expanded.ToLocal(&result)) { diff --git a/test/pummel/test-sqlite-statement-too-long.js b/test/pummel/test-sqlite-statement-too-long.js index 02c3faf0ac38..4dc2ce886a57 100644 --- a/test/pummel/test-sqlite-statement-too-long.js +++ b/test/pummel/test-sqlite-statement-too-long.js @@ -1,9 +1,10 @@ 'use strict'; -const { enoughTestMem, skipIfSQLiteMissing } = require('../common'); +const { enoughTestMem, mustNotCall, skipIfSQLiteMissing } = require('../common'); skipIfSQLiteMissing(); const { Database, constants: { SQLITE_OK } } = require('node:sqlite'); const { constants } = require('node:buffer'); +const dc = require('node:diagnostics_channel'); const { suite, test } = require('node:test'); // These queries allocate strings and blobs of several hundred megabytes. @@ -57,4 +58,27 @@ suite('values larger than the maximum string length', { skip: !enoughTestMem }, using stmt = db.prepare('SELECT count(*) AS count FROM data'); t.assert.deepStrictEqual(stmt.get(), { __proto__: null, count: 0 }); }); + + test('sourceSQL, expandedSQL and column names throw for oversized SQL', (t) => { + using db = new Database(':memory:'); + using stmt = db.prepare(`SELECT 1 AS "${longIdentifier}"`); + t.assert.throws(() => stmt.sourceSQL, tooLong); + t.assert.throws(() => stmt.expandedSQL, tooLong); + t.assert.throws(() => stmt.get(), tooLong); + t.assert.throws(() => stmt.columns(), tooLong); + }); + + test('sqlite.db.query skips statements with oversized SQL', (t) => { + const onQuery = mustNotCall(); + dc.subscribe('sqlite.db.query', onQuery); + t.after(() => dc.unsubscribe('sqlite.db.query', onQuery)); + + using db = new Database(':memory:'); + using stmt = db.prepare(`SELECT 1 AS "${longIdentifier}"`); + stmt.run(); + // sqlite3_expanded_sql() returns NULL when its result would exceed the + // length limit, and the callback falls back to the source SQL. + db.limits.length = constants.MAX_STRING_LENGTH; + stmt.run(); + }); });