Skip to content

Commit edb1934

Browse files
committed
Fixed code execution vulnerability due to Object coercion
refs GHSA-jqv5-7xpx-qj74 fixes TryGhost/Toolbox#491 - when you call `ToString()` on `Napi::Value`, it calls `napi_coerce_to_string` underneath, which has the ability to run arbitrary JS code if the passed in value is a crafted object - both remote code execution or denial-of-service are possible via this vulnerability - `toString()` on an Object returns `[object Object]` so instead of calling the function, we're going to hardcode it to prevent this issue Credits: Dave McDaniel of Cisco Talos
1 parent 3a48888 commit edb1934

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/statement.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ template <class T> Values::Field*
208208
return new Values::Float(pos, source.ToNumber().DoubleValue());
209209
}
210210
else if (source.IsObject()) {
211-
Napi::String napiVal = source.ToString();
211+
Napi::String napiVal = Napi::String::New(source.Env(), "[object Object]");
212212
// Check whether toString returned a value that is not undefined.
213213
if(napiVal.Type() == 0) {
214214
return NULL;

test/other_objects.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,20 @@ describe('data types', function() {
9595
});
9696
});
9797

98+
it('should ignore faulty toString in array', function(done) {
99+
const faulty = [[{toString: null}], 1];
100+
db.all('SELECT * FROM txt_table WHERE txt = ? LIMIT ?', faulty, function (err) {
101+
assert.equal(err, null);
102+
done();
103+
});
104+
});
105+
106+
it('should ignore faulty toString set to function', function(done) {
107+
const faulty = [[{toString: function () {console.log('oh no');}}], 1];
108+
db.all('SELECT * FROM txt_table WHERE txt = ? LIMIT ?', faulty, function (err) {
109+
assert.equal(err, undefined);
110+
done();
111+
});
112+
});
113+
98114
});

0 commit comments

Comments
 (0)