Skip to content

Commit 25b5f62

Browse files
committed
fix: do not follow redirect if scheme is not an HTTP(S) scheme
1 parent 050356b commit 25b5f62

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

.changeset/healthy-buckets-drum.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/web-fetch": patch
3+
---
4+
5+
If locationURL’s scheme is not an HTTP(S) scheme, then return a network error. https://fetch.spec.whatwg.org/#http-redirect-fetch

packages/fetch/src/fetch.js

+8
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ async function fetch(url, options_ = {}) {
180180
return;
181181
}
182182

183+
// https://fetch.spec.whatwg.org/#http-redirect-fetch
184+
// 6. If locationURL’s scheme is not an HTTP(S) scheme, then return a network error.
185+
if (locationURL.protocol !== 'http:' && locationURL.protocol !== 'https:') {
186+
reject(new FetchError('URL scheme must be a HTTP(S) scheme', 'bad-redirect-scheme'));
187+
finalize();
188+
return;
189+
}
190+
183191
// HTTP-redirect fetch step 6 (counter increment)
184192
// Create a new Request object.
185193
const requestOptions = {

packages/fetch/test/main.js

+9
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ describe("node-fetch", () => {
461461
});
462462
});
463463

464+
it("should not follow non HTTP(s) redirect", () => {
465+
const url = `${base}redirect/301/file`;
466+
const options = {
467+
};
468+
return expect(fetch(url, options))
469+
.to.eventually.be.rejected.and.be.an.instanceOf(FetchError)
470+
.and.have.property("type", "bad-redirect-scheme");
471+
});
472+
464473
it("should allow not following redirect", () => {
465474
const url = `${base}redirect/301`;
466475
const options = {

packages/fetch/test/utils/server.js

+6
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ export default class TestServer {
235235
res.end();
236236
}
237237

238+
if (p === '/redirect/301/file') {
239+
res.statusCode = 301;
240+
res.setHeader('Location', 'file://inspect');
241+
res.end();
242+
}
243+
238244
if (p === '/redirect/301/rn') {
239245
res.statusCode = 301
240246
res.setHeader('Location', '/403')

0 commit comments

Comments
 (0)