Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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 <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed Feb 20, 2022
commit abb6183d833c755a82eb29337bf2a31b26c40a68
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,
Comment thread
RaisinTen marked this conversation as resolved.
Outdated
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