Skip to content

Commit 958ba9c

Browse files
committed
zlib: fix input validation in crc32 fast path
1 parent b1c01fc commit 958ba9c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/node_zlib.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,8 +1683,16 @@ static uint32_t FastCRC32(v8::Local<v8::Value> receiver,
16831683
uint32_t value,
16841684
// NOLINTNEXTLINE(runtime/references)
16851685
v8::FastApiCallbackOptions& options) {
1686-
TRACK_V8_FAST_API_CALL("zlib.crc32");
16871686
v8::HandleScope handle_scope(options.isolate);
1687+
if (!data->IsArrayBufferView() && !data->IsString()) {
1688+
TRACK_V8_FAST_API_CALL("zlib.crc32.error");
1689+
THROW_ERR_INVALID_ARG_TYPE(
1690+
options.isolate,
1691+
"The \"data\" argument must be of type string or an instance of "
1692+
"Buffer, TypedArray, or DataView.");
1693+
return 0;
1694+
}
1695+
TRACK_V8_FAST_API_CALL("zlib.crc32");
16881696
return CRC32Impl(options.isolate, data, value);
16891697
}
16901698

test/sequential/test-zlib-crc32-fast-api.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,21 @@ const zlib = require('zlib');
1818
testFastPath();
1919
testFastPath();
2020

21+
// Test that invalid input types throw after optimization
22+
assert.throws(() => zlib.crc32(123), {
23+
code: 'ERR_INVALID_ARG_TYPE',
24+
});
25+
assert.throws(() => zlib.crc32(null), {
26+
code: 'ERR_INVALID_ARG_TYPE',
27+
});
28+
assert.throws(() => zlib.crc32({}), {
29+
code: 'ERR_INVALID_ARG_TYPE',
30+
});
31+
2132
if (common.isDebug) {
2233
const { internalBinding } = require('internal/test/binding');
2334
const { getV8FastApiCallCount } = internalBinding('debug');
2435
assert.strictEqual(getV8FastApiCallCount('zlib.crc32'), 2);
36+
assert.strictEqual(getV8FastApiCallCount('zlib.crc32.error'), 3);
2537
}
2638
}

0 commit comments

Comments
 (0)