GeezerVison is showing github.com — string_decoder: fix crash when calling __proto__.write() · styfle/node@24fc302
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/string_decoder.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,7 @@ const { |
43 | 43 | const internalUtil = require('internal/util'); |
44 | 44 | const { |
45 | 45 | ERR_INVALID_ARG_TYPE, |
| 46 | + ERR_INVALID_THIS, |
46 | 47 | ERR_UNKNOWN_ENCODING |
47 | 48 | } = require('internal/errors').codes; |
48 | 49 | const isEncoding = Buffer[internalUtil.kIsEncodingSymbol]; |
@@ -101,6 +102,9 @@ StringDecoder.prototype.write = function write(buf) { |
101 | 102 | throw new ERR_INVALID_ARG_TYPE('buf', |
102 | 103 | ['Buffer', 'TypedArray', 'DataView'], |
103 | 104 | buf); |
| 105 | + if (!this[kNativeDecoder]) { |
| 106 | + throw new ERR_INVALID_THIS('StringDecoder'); |
| 107 | + } |
104 | 108 | return decode(this[kNativeDecoder], buf); |
105 | 109 | }; |
106 | 110 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-string-decoder.js +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -210,6 +210,13 @@ if (common.enoughTestMem) { |
210 | 210 | ); |
211 | 211 | } |
212 | 212 |
|
| 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 | + |
213 | 220 | // Test verifies that StringDecoder will correctly decode the given input |
214 | 221 | // buffer with the given encoding to the expected output. It will attempt all |
215 | 222 | // possible ways to write() the input buffer, see writeSequences(). The |
|