Skip to content

Commit

Permalink
Gracefully handle invalid Location redirect URLs (#605)
Browse files Browse the repository at this point in the history
* Gracefully handle invalid `Location` redirect URLs
  • Loading branch information
brandon93s authored and szmarczak committed Sep 8, 2018
1 parent 0ddf3ac commit 7ae6939
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/request-as-event-emitter.js
Expand Up @@ -120,9 +120,10 @@ module.exports = options => {
}

const bufferString = Buffer.from(response.headers.location, 'binary').toString();
redirectUrl = (new URL(bufferString, urlLib.format(options))).toString();

try {
// Handles invalid URLs. See https://github.com/sindresorhus/got/issues/604
redirectUrl = (new URL(bufferString, urlLib.format(options))).toString();
decodeURI(redirectUrl);
} catch (error) {
emitter.emit('error', error);
Expand Down
12 changes: 12 additions & 0 deletions test/redirects.js
Expand Up @@ -110,6 +110,13 @@ test.before('setup', async () => {
response.end();
});

http.on('/invalidRedirect', (request, response) => {
response.writeHead(302, {
location: 'http://'
});
response.end();
});

await http.listen(http.port);
await https.listen(https.port);
});
Expand Down Expand Up @@ -206,3 +213,8 @@ test('throws on malformed redirect URI', async t => {
const error = await t.throwsAsync(got(`${http.url}/malformedRedirect`));
t.is(error.name, 'URIError');
});

test('throws on invalid redirect URL', async t => {
const error = await t.throwsAsync(got(`${http.url}/invalidRedirect`));
t.is(error.code, 'ERR_INVALID_URL');
});

0 comments on commit 7ae6939

Please sign in to comment.