Skip to content

Commit 903e779

Browse files
committed
sqlite: validate aggregate() name and options
`Database.prototype.aggregate()` cast its arguments without checking their types, so a missing `options` leaked a raw V8 TypeError and a non-string `name` was silently coerced. Throw `ERR_INVALID_ARG_TYPE` like the other sqlite bindings do. Assisted-by: Claude Code Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
1 parent 4486dce commit 903e779

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,19 @@ void Database::AggregateFunction(const FunctionCallbackInfo<Value>& args) {
26932693
Environment* env = Environment::GetCurrent(args);
26942694
THROW_AND_RETURN_ON_BAD_STATE(env, !db->IsOpen(), "database is not open");
26952695
THROW_AND_RETURN_IF_IN_AUTHORIZER(env, db);
2696+
2697+
if (!args[0]->IsString()) {
2698+
THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
2699+
"The \"name\" argument must be a string.");
2700+
return;
2701+
}
2702+
2703+
if (!args[1]->IsObject()) {
2704+
THROW_ERR_INVALID_ARG_TYPE(env->isolate(),
2705+
"The \"options\" argument must be an object.");
2706+
return;
2707+
}
2708+
26962709
Utf8Value name(env->isolate(), args[0].As<String>());
26972710
Local<Object> options = args[1].As<Object>();
26982711
Local<Value> start_v;

‎test/parallel/test-sqlite-aggregate-function.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ describe('Database.prototype.aggregate()', () => {
2020
return fn;
2121
}
2222

23+
test('throws if name is not a string', (t) => {
24+
t.assert.throws(() => {
25+
db.aggregate(123, { start: 0, step: () => {} });
26+
}, {
27+
code: 'ERR_INVALID_ARG_TYPE',
28+
message: 'The "name" argument must be a string.'
29+
});
30+
});
31+
32+
test('throws if options is not an object', (t) => {
33+
t.assert.throws(() => {
34+
db.aggregate('sum');
35+
}, {
36+
code: 'ERR_INVALID_ARG_TYPE',
37+
message: 'The "options" argument must be an object.'
38+
});
39+
});
40+
2341
test('throws if options.start is not provided', (t) => {
2442
t.assert.throws(() => {
2543
db.aggregate('sum', {

0 commit comments

Comments
 (0)