Skip to content

Commit 5bd6fd0

Browse files
RaisinTendanielleadams
authored andcommitted
string_decoder: fix crash when calling __proto__.write()
This makes the function throw an exception from JS instead of crashing. Fixes: #41949 Signed-off-by: Darshan Sen <[email protected]> PR-URL: #42062 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Mestery <[email protected]>
1 parent 30908e7 commit 5bd6fd0

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/string_decoder.js

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const {
4343
const internalUtil = require('internal/util');
4444
const {
4545
ERR_INVALID_ARG_TYPE,
46+
ERR_INVALID_THIS,
4647
ERR_UNKNOWN_ENCODING
4748
} = require('internal/errors').codes;
4849
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
@@ -101,6 +102,9 @@ StringDecoder.prototype.write = function write(buf) {
101102
throw new ERR_INVALID_ARG_TYPE('buf',
102103
['Buffer', 'TypedArray', 'DataView'],
103104
buf);
105+
if (!this[kNativeDecoder]) {
106+
throw new ERR_INVALID_THIS('StringDecoder');
107+
}
104108
return decode(this[kNativeDecoder], buf);
105109
};
106110

test/parallel/test-string-decoder.js

+7
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ if (common.enoughTestMem) {
210210
);
211211
}
212212

213+
assert.throws(
214+
() => new StringDecoder('utf8').__proto__.write(Buffer.from('abc')), // eslint-disable-line no-proto
215+
{
216+
code: 'ERR_INVALID_THIS',
217+
}
218+
);
219+
213220
// Test verifies that StringDecoder will correctly decode the given input
214221
// buffer with the given encoding to the expected output. It will attempt all
215222
// possible ways to write() the input buffer, see writeSequences(). The

0 commit comments

Comments
 (0)