Skip to content

fix: validate Response status in constructor per Fetch spec - #1893

Open
MD-Mushfiqur123 wants to merge 2 commits into
node-fetch:mainfrom
MD-Mushfiqur123:fix/response-status-validation
Open

fix: validate Response status in constructor per Fetch spec#1893
MD-Mushfiqur123 wants to merge 2 commits into
node-fetch:mainfrom
MD-Mushfiqur123:fix/response-status-validation

Conversation

@MD-Mushfiqur123

Copy link
Copy Markdown

What

Add proper validation to the Response constructor per the Fetch spec.

Why

Per https://fetch.spec.whatwg.org/#response-class:

  1. If init["status"] is not in the range 200-599, throw a RangeError
  2. If body is non-null and status is a null body status (101, 204, 205, 304), throw a TypeError

Currently neither check is performed, allowing invalid Response objects to be created.

How

Added two checks at the start of the Response constructor:

  • Status range validation (200-599)
  • Null body status check for non-null bodies

Fixes #1685

Currently the Host header appears as the last header in HTTP requests.
Prepend it by explicitly setting it in the headers object before other
headers, following convention used by browsers and other libraries.

Relevant Node.js issue: nodejs/node#42496

Fixes node-fetch#1570
Add status range validation per https://fetch.spec.whatwg.org/#response-class.
Status must be in the range 200-599, otherwise throw a RangeError.

Also fix Response.error() to bypass constructor validation by setting
status directly on the internal object, as network errors use status 0.

Fixes node-fetch#1685
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.

When initializing a response it should throw if status is not in the range 200 to 599

2 participants