diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-10-26 16:27:51 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-11-05 20:19:09 +0100 |
commit | 237be2ed9e25e4d56deaf3935be0c217913e5a75 (patch) | |
tree | f3389797e763be54b82a159a9cd780ea0f2dbfd3 /lib/internal | |
parent | afd29c9502449121aacba37b253dc39e159aae03 (diff) | |
download | android-node-v8-237be2ed9e25e4d56deaf3935be0c217913e5a75.tar.gz android-node-v8-237be2ed9e25e4d56deaf3935be0c217913e5a75.tar.bz2 android-node-v8-237be2ed9e25e4d56deaf3935be0c217913e5a75.zip |
encoding: make TextDecoder handle BOM correctly
Do not accept the BOM if it comes from a different encoding, and
only discard the BOM after it has actually been read (including
when it is spread over multiple chunks in streaming mode).
Fixes: https://github.com/nodejs/node/issues/25315
PR-URL: https://github.com/nodejs/node/pull/30132
Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'lib/internal')
-rw-r--r-- | lib/internal/encoding.js | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index dabcd5eacc..16de0c986e 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -484,25 +484,22 @@ function makeTextDecoderJS() { this[kFlags] |= CONVERTER_FLAGS_FLUSH; } - if (!this[kBOMSeen] && !(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) { - if (this[kEncoding] === 'utf-8') { - if (input.length >= 3 && - input[0] === 0xEF && input[1] === 0xBB && input[2] === 0xBF) { - input = input.slice(3); - } - } else if (this[kEncoding] === 'utf-16le') { - if (input.length >= 2 && input[0] === 0xFF && input[1] === 0xFE) { - input = input.slice(2); - } + let result = this[kFlags] & CONVERTER_FLAGS_FLUSH ? + this[kHandle].end(input) : + this[kHandle].write(input); + + if (result.length > 0 && + !this[kBOMSeen] && + !(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) { + // If the very first result in the stream is a BOM, and we are not + // explicitly told to ignore it, then we discard it. + if (result[0] === '\ufeff') { + result = result.slice(1); } this[kBOMSeen] = true; } - if (this[kFlags] & CONVERTER_FLAGS_FLUSH) { - return this[kHandle].end(input); - } - - return this[kHandle].write(input); + return result; } } |