Skip to content

fix(http): strip the BOM for every UTF-8 responseEncoding spelling - #11514

Open
JayPokale wants to merge 3 commits into
axios:v1.xfrom
JayPokale:fix-http-utf8-bom-aliases
Open

JayPokale wants to merge 3 commits into
axios:v1.xfrom
JayPokale:fix-http-utf8-bom-aliases

Conversation

@JayPokale

@JayPokale JayPokale commented Nov 27, 2026 •

Copy link
Copy Markdown

Summary

The Node http adapter removes a leading UTF-8 byte order mark from buffered text and JSON responses, but only when responseEncoding is unset or exactly 'utf8'. The README documents utf-8, UTF-8 and UTF8 as equivalent, and Buffer#toString decodes them the same way, yet with those spellings U+FEFF stays at the start of the data. A BOM-prefixed JSON response then comes back as an unparsed string, or rejects with ERR_BAD_RESPONSE when responseType: 'json' is used with transitional.silentJSONParsing: false. The fetch adapter already strips the BOM regardless of responseEncoding.

This adds one helper, isUTF8Encoding, that matches an unset encoding or /^utf-?8$/i, and uses it at both BOM sites (buffered responses and data: URLs).

Linked issue

No issue. Related: #11477 proposed the same change and was closed by its author without review.

Changes

  • lib/adapters/http.js: isUTF8Encoding helper used at the two stripBOM sites.
  • tests/unit/adapters/responseEncodingBom.test.js: new tests for utf8, utf-8, UTF-8 and UTF8 on buffered text, JSON (including strict parsing) and data: URL responses, plus negative cases that must keep the BOM (other encodings, arraybuffer, stream).
  • PRE_RELEASE_CHANGELOG.md: Bug Fixes entry.

Validation

  • Node v26.12.0, vitest 5.1.0, npm ci --ignore-scripts.
  • npx vitest run --project unit tests/unit/adapters/responseEncodingBom.test.js: 16 of 24 tests fail on the base lib/adapters/http.js (for example SyntaxError: Unexpected token '' ... is not valid JSON) and all 24 pass with this change (24 now includes a stream-response case).
  • npm run test:vitest:unit (whole unit project, one worker): 78 files, 1823 tests pass.
  • npm run lint: passes. npm run build: passes. prettier --check on the changed files: clean.
  • Browser suites do not apply: lib/adapters/http.js is replaced with a null module in browser builds.
  • GitHub Actions status for the latest commit: not recorded yet (first-time contributor runs wait for approval); I will update this section when they run.

Compatibility and documentation

  • Patch release. Node HTTP adapter only. No API or type changes, so index.d.ts, index.d.cts and PRE_RELEASE_DOCS.md need no update (README.md already lists these spellings).
  • Other encodings and arraybuffer/stream responses are unchanged.

Checklist

  • Scope, target branch, and linked issue/rationale checked
  • PRE_RELEASE_CHANGELOG.md entry added or updated (or AGENTS.md's automated release-preparation exception explained)
  • Regression tests and relevant negative/platform cases covered (or N/A with reason)
  • Applicable local checks completed, with actual commands and results recorded above
  • Deferred release docs and ESM/CJS types/tests updated where needed (or N/A with reason)
  • Semver impact stated and intentional breaking changes explained
  • Final diff reviewed for security, unrelated changes, generated artifacts, and test/debug leftovers
  • Latest GitHub Actions status recorded separately from review/security bot results

This PR was prepared with AI assistance (Claude). I ran the new tests against both versions of the adapter and ran the full unit suite myself. I cannot apply labels as an outside contributor, so a maintainer needs to apply ai-assisted.

The Node HTTP adapter only removed a leading UTF-8 byte order mark when
responseEncoding was unset or exactly 'utf8'. The README lists 'utf-8',
'UTF-8' and 'UTF8' as equivalent options, and Buffer#toString decodes them
the same way, but with those spellings U+FEFF stayed at the start of text
and data: URL responses. A JSON response with a BOM was then returned
unparsed, or rejected with ERR_BAD_RESPONSE when responseType is 'json' and
transitional.silentJSONParsing is false. The fetch adapter already strips
the BOM whatever responseEncoding is.

Match the encoding with /^utf-?8$/i, or an unset encoding, at both places
that strip the BOM. Other encodings and arraybuffer or stream responses are
unchanged.
@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5RetriggerView in Greptile

[Medium impact] The PR appears safe to merge.

Summary

The Node HTTP adapter now strips a leading BOM for case-insensitive utf8 and utf-8 spellings.

  • The Node HTTP adapter strips leading BOMs for every UTF-8 spelling.

Reviews (5) · Last reviewed commit: "Merge v1.x into fix-http-utf8-bom-aliase..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Served a BOM-prefixed JSON and text body to the http adapter. With responseEncoding utf-8, UTF8 and UTF-8, the JSON now parses, and text and data URLs come back without the BOM, as utf8 and the default already did; master returned the BOM-prefixed string. latin1 and utf16le are unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants