Skip to content
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

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

Merged
merged 1 commit into from
Mar 18, 2024
Merged
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 @@
---
"@remix-run/web-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
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:') {

Choose a reason for hiding this comment

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

This protocol check looks good, but we may also want to support a relative redirection as in an empty locationURL. e.g. Location: /home.

Copy link
Member Author

Choose a reason for hiding this comment

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

locationURL is constructed based on the request URL and takes this into consideration already. Tests exist as well:

const locationURL = location === null ? null : new URL(location, request.url);

reject(new FetchError('URL scheme must be a HTTP(S) scheme', 'bad-redirect-scheme'));
finalize();
return;
}

// HTTP-redirect fetch step 6 (counter increment)

Choose a reason for hiding this comment

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

Not from this PR but looks like some of the steps are invalid in here as the spec has evolved maybe? we could take a pass through in a separate PR some other time to get the comments updated

Suggested change
// HTTP-redirect fetch step 6 (counter increment)
// HTTP-redirect fetch step 7 (counter increment)

// Create a new Request object.
const requestOptions = {
Expand Down
9 changes: 9 additions & 0 deletions packages/fetch/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,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
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') {
res.statusCode = 301;
res.setHeader('Location', 'file://inspect');
res.end();
}

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