npm / minipass-fetch

An implementation of window.fetch in Node.js using Minipass streams
Other
54 stars 11 forks source link

fix: TypeError crashes program in case of invalid url in redirect #59

Closed MohGanji closed 1 year ago

MohGanji commented 2 years ago

Description

Calling fetch() on a server that responds with an invalid URL breaks the execution of the program with the following unhandled error:

internal/url.js:259
  throw new ERR_INVALID_URL(input);
  ^

TypeError [ERR_INVALID_URL]: Invalid URL: :some:weird%invalid%url%

Steps to reproduce:

  1. To simulate the environment that the bug appears in, set up a local server redirecting to an invalid URL
const http = require('http');
const server = http.createServer((req, res, next) => {
    res.writeHead(301, {'Location' : 'http://www.stackoverflow.com%'});
    res.end();
});
server.listen(process.env.PORT || 3000);
  1. Then running this code throws error and crashes the program despite fetch() being wrapped in a try/catch block.
let fetch = require('minipass-fetch')

(async function {
    try{
        var res = await fetch(
            'http://localhost:3000',
        //  {redirect: 'manual'}
        )
        console.log('val: ', res)
    } catch (err) {
        console.log('err: ', err)
    }
})()

References

This behavior is inconsistent with current node-fetch. Similar issue reported #1386 and fixed #1387 in node-fetch.

wraithgar commented 2 years ago

@MohGanji can you rebase this pr against main? The CI has been fixed so we should be able to see if tests pass here. Your tests will also need to be tweaked to use async/await like the rest of the tests were so that linting passes.

wraithgar commented 1 year ago

https://github.com/npm/minipass-fetch/pull/100