Skip to content

Commit bc4915c

Browse files
committed
sqlite: throw on oversized error messages
SQLite error messages embed the offending identifier or token, so they can exceed String::kMaxLength. CreateSQLiteErrorImpl() converted them with String::NewFromUtf8() and the default length of -1, which skips V8's length check and aborts the process. Convert the message with Utf8StringMaybeOneByte() so an oversized message throws ERR_STRING_TOO_LONG instead. Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Assisted-by: claude:opus-5.5
1 parent 85df70e commit bc4915c

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎src/node_sqlite.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ MaybeLocal<Object> CreateSQLiteErrorImpl(Isolate* isolate,
284284
Local<Context> context = isolate->GetCurrentContext();
285285
Local<String> js_msg;
286286
Local<Object> e;
287-
if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) ||
287+
// SQLite error messages embed the offending identifier or token, so they
288+
// can exceed String::kMaxLength.
289+
if (!Utf8StringMaybeOneByte(isolate, message).ToLocal(&js_msg) ||
288290
!Exception::Error(js_msg)->ToObject(context).ToLocal(&e) ||
289291
e->Set(context, env->code_string(), env->err_sqlite_error_string())
290292
.IsNothing()) {

‎test/parallel/test-sqlite-statement.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,15 @@ suite('values larger than the maximum string length', { skip: !enoughTestMem },
14581458
}, tooLong);
14591459
});
14601460

1461+
test('prepare() throws when the SQLite error message is too long', (t) => {
1462+
using db = new Database(':memory:');
1463+
// '\u20ac' is 3 bytes in UTF-8, and SQLite repeats the name in the error.
1464+
const name = '\u20ac'.repeat(Math.ceil(constants.MAX_STRING_LENGTH / 3) + 10);
1465+
t.assert.throws(() => {
1466+
db.prepare(`SELECT 1 FROM "${name}"`);
1467+
}, tooLong);
1468+
});
1469+
14611470
test('exec() surfaces the error from a user-defined function', (t) => {
14621471
using db = new Database(':memory:');
14631472
db.exec('CREATE TABLE data(val TEXT)');

0 commit comments

Comments
 (0)