Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string_decoder: fix crash when calling __proto__.write() using JS #42062

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/string_decoder.js
Expand Up @@ -42,6 +42,7 @@ const {
} = internalBinding('string_decoder');
const internalUtil = require('internal/util');
const {
ERR_ILLEGAL_CONSTRUCTOR,
RaisinTen marked this conversation as resolved.
Show resolved Hide resolved
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
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