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: nodejs#41949
Signed-off-by: Darshan Sen <[email protected]>
  • Loading branch information
RaisinTen committed Feb 20, 2022
1 parent dde2f78 commit abb6183
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
} = internalBinding('string_decoder');
const internalUtil = require('internal/util');
const {
ERR_ILLEGAL_CONSTRUCTOR,
ERR_INVALID_ARG_TYPE,
ERR_UNKNOWN_ENCODING
} = require('internal/errors').codes;
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_ILLEGAL_CONSTRUCTOR();
}
return decode(this[kNativeDecoder], buf);
};

Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-string-decoder.js
Original file line number Diff line number Diff line change
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_ILLEGAL_CONSTRUCTOR',
}
);

// 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 abb6183

Please sign in to comment.