sqlite: throw on oversized strings instead of aborting

Several conversions from SQLite text to V8 strings passed a length of
-1 to String::NewFromUtf8(). With that length, V8 skips its length
check and aborts the process when the UTF-8 text exceeds
String::kMaxLength.

SQLite error messages embed the offending identifier or token, so
CreateSQLiteErrorImpl() could hit this. NullableSQLiteStringToValue(),
used for setAuthorizer() callback arguments and statement.columns()
metadata, had the same problem, and the authorizer also called
ToLocalChecked() on each argument.

Convert error messages with Utf8StringMaybeOneByte(), and check the
length up front in NullableSQLiteStringToValue(), so oversized strings
throw ERR_STRING_TOO_LONG as column values already do. In the
authorizer, deny the action and let the pending error reach the caller.

Signed-off-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
Assisted-by: claude:opus-5.5
PR-URL: https://github.com/nodejs/node/pull/66521
Fixes: https://github.com/nodejs/node/issues/66520
Fixes: https://github.com/nodejs/node/issues/66522
Reviewed-By: Guilherme Araújo <arauujogui@gmail.com>
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Trivikram Kamat authored and GitHub committed 2026-10-07 02:13:23 +00:00
1 parent 1bcf30b52a
commit aa1bf2cc37
2 files changed
+50 -12

No files matched your search

+21 -11
View File
@@ -284,7 +284,9 @@ MaybeLocal<Object> CreateSQLiteErrorImpl(Isolate* isolate,
Local<Context> context = isolate->GetCurrentContext();
Local<String> js_msg;
Local<Object> e;
if (!String::NewFromUtf8(isolate, message).ToLocal(&js_msg) ||
// SQLite error messages embed the offending identifier or token, so they
// can exceed String::kMaxLength.
if (!Utf8StringMaybeOneByte(isolate, message).ToLocal(&js_msg) ||
!Exception::Error(js_msg)->ToObject(context).ToLocal(&e) ||
e->Set(context, env->code_string(), env->err_sqlite_error_string())
.IsNothing()) {
@@ -399,7 +401,15 @@ inline MaybeLocal<Value> NullableSQLiteStringToValue(Isolate* isolate,
return Null(isolate);
}
return String::NewFromUtf8(isolate, str, NewStringType::kInternalized)
// With the default length of -1, V8 aborts on strings over kMaxLength.
const size_t len = strlen(str);
if (len > static_cast<size_t>(String::kMaxLength)) [[unlikely]] {
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
return MaybeLocal<Value>();
}
return String::NewFromUtf8(
isolate, str, NewStringType::kInternalized, static_cast<int>(len))
.As<Value>();
}
@@ -3631,15 +3641,15 @@ int Database::AuthorizerCallback(void* user_data,
Local<Function> callback = cb.As<Function>();
LocalVector<Value> js_argv(
isolate,
{
Integer::New(isolate, action_code),
NullableSQLiteStringToValue(isolate, param1).ToLocalChecked(),
NullableSQLiteStringToValue(isolate, param2).ToLocalChecked(),
NullableSQLiteStringToValue(isolate, param3).ToLocalChecked(),
NullableSQLiteStringToValue(isolate, param4).ToLocalChecked(),
});
LocalVector<Value> js_argv(isolate, {Integer::New(isolate, action_code)});
for (const char* param : {param1, param2, param3, param4}) {
Local<Value> arg;
if (!NullableSQLiteStringToValue(isolate, param).ToLocal(&arg)) {
db->SetIgnoreNextSQLiteError(true);
return SQLITE_DENY;
}
js_argv.push_back(arg);
}
MaybeLocal<Value> retval = callback->Call(
context, Undefined(isolate), js_argv.size(), js_argv.data());