Commit 2fdac690b47 for nodejs
commit 2fdac690b4707ba223e44829f5f9b5c5fd3e16f6
Author: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
Date: Fri Nov 27 10:33:24 2026 -0300
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 <lazerg2@gmail.com>
Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67768
Fixes: https://github.com/nodejs/node/issues/66487
Refs: https://github.com/nodejs/node/pull/66498
Reviewed-By: Guilherme Araújo <arauujogui@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc
index 00b8f40ce99..001718d853e 100644
--- a/src/node_sqlite.cc
+++ b/src/node_sqlite.cc
@@ -3471,10 +3471,15 @@ int Database::TraceCallback(unsigned int type,
Isolate* isolate = env->isolate();
HandleScope handle_scope(isolate);
+ // With the default length of -1, V8 aborts on strings over kMaxLength.
char* expanded = sqlite3_expanded_sql(static_cast<sqlite3_stmt*>(p));
Local<Value> sql_string;
if (expanded != nullptr) {
- bool ok = String::NewFromUtf8(isolate, expanded).ToLocal(&sql_string);
+ bool ok = String::NewFromUtf8(isolate,
+ expanded,
+ NewStringType::kNormal,
+ static_cast<int>(strlen(expanded)))
+ .ToLocal(&sql_string);
sqlite3_free(expanded);
if (!ok) {
return 0;
@@ -3483,7 +3488,11 @@ int Database::TraceCallback(unsigned int type,
// Fallback to source SQL if expanded is unavailable
const char* source = sqlite3_sql(static_cast<sqlite3_stmt*>(p));
if (source == nullptr ||
- !String::NewFromUtf8(isolate, source).ToLocal(&sql_string)) {
+ !String::NewFromUtf8(isolate,
+ source,
+ NewStringType::kNormal,
+ static_cast<int>(strlen(source)))
+ .ToLocal(&sql_string)) {
return 0;
}
}
@@ -3783,9 +3792,7 @@ MaybeLocal<Name> Statement::ColumnNameToName(const int column) {
return MaybeLocal<Name>();
}
- return String::NewFromUtf8(
- env()->isolate(), col_name, NewStringType::kInternalized)
- .As<Name>();
+ return NullableSQLiteStringToValue(env()->isolate(), col_name).As<Name>();
}
// Size of the stack array RowToObject passes to the template.
@@ -4225,7 +4232,8 @@ void Statement::SourceSQLGetter(const FunctionCallbackInfo<Value>& args) {
THROW_AND_RETURN_ON_BAD_STATE(
env, stmt->IsFinalized(), "statement has been finalized");
Local<String> sql;
- if (!String::NewFromUtf8(env->isolate(), sqlite3_sql(stmt->statement_.get()))
+ if (!Utf8StringMaybeOneByte(env->isolate(),
+ sqlite3_sql(stmt->statement_.get()))
.ToLocal(&sql)) {
return;
}
@@ -4245,7 +4253,7 @@ void Statement::ExpandedSQLGetter(const FunctionCallbackInfo<Value>& 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<String> 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 02c3faf0ac3..a8d46524586 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,26 @@ 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();
+ // Make sqlite3_expanded_sql() return NULL to test the source SQL fallback.
+ db.limits.length = constants.MAX_STRING_LENGTH;
+ stmt.run();
+ });
});