nodejs/node · #67768
sqlite: fix abort on oversized SQL text
src/node_sqlite.cc15 + / 7 −
@@ -3386,10 +3386,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;@@ -3398,7 +3403,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; } }@@ -3698,9 +3707,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.@@ -4140,7 +4147,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; }@@ -4160,7 +4168,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)) {test/pummel/test-sqlite-statement-too-long.js24 + / 1 −
@@ -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();+ }); });