Skip to content

Commit

Permalink
string_decoder: fix crash when calling __proto__.write()
Browse files Browse the repository at this point in the history
This makes the function throw an exception from JS instead of crashing.

Fixes: #41949
Signed-off-by: Darshan Sen <raisinten@gmail.com>

PR-URL: #42062
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
  • Loading branch information
RaisinTen authored and danielleadams committed Apr 24, 2022
1 parent 962a8ec commit f21f104
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/string_decoder.js
Expand Up @@ -43,6 +43,7 @@ const {
const internalUtil = require('internal/util');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
ERR_UNKNOWN_ENCODING
} = require('internal/errors').codes;
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
Expand Down Expand Up @@ -101,6 +102,9 @@ StringDecoder.prototype.write = function write(buf) {
throw new ERR_INVALID_ARG_TYPE('buf',
['Buffer', 'TypedArray', 'DataView'],
buf);
if (!this[kNativeDecoder]) {
throw new ERR_INVALID_THIS('StringDecoder');
}
return decode(this[kNativeDecoder], buf);
};

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-string-decoder.js
Expand Up @@ -210,6 +210,13 @@ if (common.enoughTestMem) {
);
}

assert.throws(
() => new StringDecoder('utf8').__proto__.write(Buffer.from('abc')), // eslint-disable-line no-proto
{
code: 'ERR_INVALID_THIS',
}
);

// Test verifies that StringDecoder will correctly decode the given input
// buffer with the given encoding to the expected output. It will attempt all
// possible ways to write() the input buffer, see writeSequences(). The
Expand Down

0 comments on commit f21f104

Please sign in to comment.