Skip to content

fix(fetch): do not follow redirect if scheme is not an HTTP(S) scheme #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-buckets-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-io/fetch": patch
---

If locationURL’s scheme is not an HTTP(S) scheme, then return a network error. https://fetch.spec.whatwg.org/#http-redirect-fetch
3 changes: 2 additions & 1 deletion .github/workflows/blob.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -42,9 +43,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down
16 changes: 2 additions & 14 deletions .github/workflows/fetch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -44,22 +45,16 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
- windows-latest
- macos-latest
project:
- fetch
exclude:
- os: windows-latest
node-version: 14
# On macOS, run tests with only the LTS environments.
- os: macos-latest
node-version: 14

steps:
- uses: actions/checkout@v2
Expand All @@ -75,12 +70,5 @@ jobs:
- name: Test (ESM)
run: yarn --cwd packages/${{matrix.project}} test -- --colors

# upload coverage only once
- name: Coveralls
uses: coverallsapp/github-action@master
if: matrix.node == '14' && matrix.os == 'ubuntu-latest'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Test (CJS)
run: yarn --cwd packages/${{matrix.project}} test:cjs
3 changes: 2 additions & 1 deletion .github/workflows/file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -42,9 +43,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/form-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -43,9 +44,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/stream.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
name: Typecheck
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand All @@ -43,9 +44,9 @@ jobs:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version:
- 14
- 16
os:
- ubuntu-latest
Expand Down Expand Up @@ -76,6 +77,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
node-version:
- 16
Expand Down
8 changes: 8 additions & 0 deletions packages/fetch/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ async function fetch(url, options_ = {}) {
return;
}

// https://fetch.spec.whatwg.org/#http-redirect-fetch
// 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network error.
if (locationURL.protocol !== 'http:' && locationURL.protocol !== 'https:') {
reject(new FetchError('URL scheme must be a HTTP(S) scheme', 'bad-redirect-scheme'));
finalize();
return;
}

// HTTP-redirect fetch step 6 (counter increment)
// Create a new Request object.
const requestOptions = {
Expand Down
11 changes: 10 additions & 1 deletion packages/fetch/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ describe('node-fetch', () => {
});
});

it("should not follow non HTTP(s) redirect", () => {
const url = `${base}redirect/301/file`;
const options = {
};
return expect(fetch(url, options))
.to.eventually.be.rejected.and.be.an.instanceOf(FetchError)
.and.have.property("type", "bad-redirect-scheme");
});

it('should allow not following redirect', () => {
const url = `${base}redirect/301`;
const options = {
Expand Down Expand Up @@ -1601,7 +1610,7 @@ describe('node-fetch', () => {
const parameters = new URLSearchParams();
parameters.append('key', 'value');
const request = new Request(base, {
method: 'POST',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
Expand Down
6 changes: 6 additions & 0 deletions packages/fetch/test/utils/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ export default class TestServer {
res.end();
}

if (p === '/redirect/301/file') {
Copy link
Preview

Copilot AI Apr 7, 2025

Choose a reason for hiding this comment

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

Consider using an 'else if' chain or an early return after processing a redirect to ensure that once a matching redirect case is found, no subsequent response is sent accidentally.

Copilot uses AI. Check for mistakes.

res.statusCode = 301;
res.setHeader('Location', 'file://inspect');
res.end();
}

if (p === '/redirect/301/rn') {
res.statusCode = 301
res.setHeader('Location', '/403')
Expand Down