Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;
Expand All @@ -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<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;
}
}
Expand Down Expand Up @@ -3698,9 +3709,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.
Expand Down Expand Up @@ -4140,7 +4149,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;
}
Expand All @@ -4160,7 +4170,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)) {
Expand Down
26 changes: 25 additions & 1 deletion test/pummel/test-sqlite-statement-too-long.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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();
});
});
Loading