From 96d22e2933c4541a6d7c9526304cac3a293134ea Mon Sep 17 00:00:00 2001 From: Mayank Dhingra Date: Tue, 24 Nov 2026 11:46:12 -0500 Subject: [PATCH] fix: discard the initial UTF-8 BOM when decoding text --- lib/blob.js | 2 +- lib/body.js | 5 +++-- test/blob.js | 16 ++++++++++++++++ test/body.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/lib/blob.js b/lib/blob.js index 121b173..7b69b5f 100644 --- a/lib/blob.js +++ b/lib/blob.js @@ -45,7 +45,7 @@ class Blob { } text () { - return Promise.resolve(this[BUFFER].toString()) + return Promise.resolve(this[BUFFER].toString().replace(/^\uFEFF/, '')) } arrayBuffer () { diff --git a/lib/body.js b/lib/body.js index f38ee80..eac3ad2 100644 --- a/lib/body.js +++ b/lib/body.js @@ -16,6 +16,7 @@ try { const INTERNALS = Symbol('Body internals') const CONSUME_BODY = Symbol('consumeBody') +const utf8Decode = buffer => buffer.toString().replace(/^\uFEFF/, '') class Body { constructor (bodyArg, options = {}) { @@ -74,7 +75,7 @@ class Body { async json () { const buf = await this[CONSUME_BODY]() try { - return JSON.parse(buf.toString()) + return JSON.parse(utf8Decode(buf)) } catch (er) { throw new FetchError( `invalid json response body at ${this.url} reason: ${er.message}`, @@ -84,7 +85,7 @@ class Body { } text () { - return this[CONSUME_BODY]().then(buf => buf.toString()) + return this[CONSUME_BODY]().then(utf8Decode) } buffer () { diff --git a/test/blob.js b/test/blob.js index a239ccc..e67a971 100644 --- a/test/blob.js +++ b/test/blob.js @@ -69,3 +69,19 @@ t.test('expose the BUFFER symbol as read-only static property', t => { t.throws(() => Blob.BUFFER = 'fubber') t.end() }) + +t.test('UTF-8 text decoding discards only the initial BOM', async t => { + for (const [text, expected] of [ + ['\uFEFFtext', 'text'], + ['\uFEFF\uFEFFtext', '\uFEFFtext'], + ['text\uFEFFinside', 'text\uFEFFinside'], + ['\uFEFF', ''], + ]) { + const bytes = Buffer.from(text) + const b = new Blob([bytes]) + t.equal(await b.text(), expected) + t.equal(b.size, bytes.length) + t.same(Buffer.from(await b.arrayBuffer()), bytes) + t.same(await b.stream().concat(), bytes) + } +}) diff --git a/test/body.js b/test/body.js index 99b23ed..03118ec 100644 --- a/test/body.js +++ b/test/body.js @@ -399,3 +399,54 @@ t.test('convert body', t => { t.end() }) + +t.test('UTF-8 text decoding discards only the initial BOM', async t => { + const cases = [ + [Buffer.from('\uFEFFtext'), 'text'], + [Buffer.from('\uFEFF\uFEFFtext'), '\uFEFFtext'], + [Buffer.from('text\uFEFFinside'), 'text\uFEFFinside'], + [Buffer.from('\uFEFF'), ''], + [Buffer.alloc(0), ''], + [Buffer.from([0xef, 0xbb]), '\uFFFD'], + [Buffer.from([0xff, 0xef, 0xbb, 0xbf]), '\uFFFD\uFEFF'], + ] + for (const [bytes, expected] of cases) { + t.equal(await new Body(bytes).text(), expected) + } +}) + +t.test('JSON decoding discards the initial UTF-8 BOM', async t => { + const bytes = Buffer.from('\uFEFF{"inner":"a\uFEFFb"}') + const b = new Body(bytes) + t.same(await b.json(), { inner: 'a\uFEFFb' }) + t.equal(b.bodyUsed, true) + await t.rejects(b.text(), TypeError) +}) + +t.test('JSON decoding does not discard interior or repeated BOMs', async t => { + for (const text of ['\uFEFF\uFEFF{}', ' \uFEFF{}']) { + await t.rejects(new Body(text).json(), { type: 'invalid-json' }) + } +}) + +t.test('UTF-8 BOM bytes survive raw body reads', async t => { + const bytes = Buffer.from('\uFEFFtext') + t.same(await new Body(bytes).buffer(), bytes) + t.same(Buffer.from(await new Body(bytes).arrayBuffer()), bytes) + const blob = await new Body(bytes).blob() + t.equal(blob.size, bytes.length) + t.same(Buffer.from(await blob.arrayBuffer()), bytes) + t.equal(await new Body(bytes).textConverted(), 'text') +}) + +t.test('UTF-8 BOM split across stream chunks', async t => { + const make = () => { + const stream = new Minipass() + stream.write(Buffer.from([0xef])) + stream.write(Buffer.from([0xbb])) + stream.end(Buffer.concat([Buffer.from([0xbf]), Buffer.from('{"ok":true}')])) + return new Body(stream) + } + t.equal(await make().text(), '{"ok":true}') + t.same(await make().json(), { ok: true }) +})