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

Follow redirects with encoded URI #564

Merged
merged 5 commits into from
Aug 23, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"decompress-response": "^3.3.0",
"duplexer3": "^0.1.4",
"get-stream": "^4.0.0",
"mimic-response": "^1.0.0",
"mimic-response": "^1.0.1",
"p-cancelable": "^0.5.0",
"to-readable-stream": "^1.0.0",
"url-parse-lax": "^3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion source/request-as-event-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = options => {
redirectUrl = (new URLGlobal(bufferString, urlLib.format(options))).toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think using urlLib.format to get the baseUrl has the same problem as was discussed in #519 (comment).

I can take a look at adding a test and fixing today.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The baseUrl won’t have the right query string, but I don’t think there’s any value for new url for which it would affect the result. So this seems fine.


try {
redirectUrl = decodeURI(redirectUrl);
decodeURI(redirectUrl);
} catch (error) {
emitter.emit('error', error);
return;
Expand Down
13 changes: 13 additions & 0 deletions test/arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ test.before('setup', async () => {
response.end(request.url);
});

s.on('/?test=it’s+ok', (request, response) => {
response.end(request.url);
});

s.on('/stream', (request, response) => {
response.end('ok');
});
Expand Down Expand Up @@ -99,6 +103,15 @@ test('overrides querystring from opts', async t => {
t.is(response.body, '/?test=wow');
});

test('escapes query parameter values', async t => {
const response = await got(`${s.url}`, {
query: {
test: `it’s ok`
}
});
t.is(response.body, '/?test=it%E2%80%99s+ok');
});

test('the `query` option can be a URLSearchParams', async t => {
const query = new URLSearchParams({test: 'wow'});
const {body} = await got(s.url, {query});
Expand Down
8 changes: 7 additions & 1 deletion test/helpers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ exports.createServer = async () => {
const port = await getPort();

const s = http.createServer((request, response) => {
s.emit(request.url, request, response);
const event = decodeURI(request.url);
if (s.listeners(event).length === 0) {
response.writeHead(404, 'Not Found');
response.end(`No listener for ${event}`);
} else {
s.emit(event, request, response);
}
});

s.host = host;
Expand Down
25 changes: 18 additions & 7 deletions test/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ let http;
let https;

test.before('setup', async () => {
const reached = (request, response) => {
response.end('reached');
};
https = await createSSLServer();
http = await createServer();

Expand All @@ -25,9 +28,7 @@ test.before('setup', async () => {

// HTTP Handlers

http.on('/', (request, response) => {
response.end('reached');
});
http.on('/', reached);

http.on('/finite', (request, response) => {
response.writeHead(302, {
Expand All @@ -36,9 +37,8 @@ test.before('setup', async () => {
response.end();
});

http.on('/utf8-url-áé', (request, response) => {
response.end('reached');
});
http.on('/utf8-url-áé', reached);
http.on('/?test=it’s+ok', reached);

http.on('/redirect-with-utf8-binary', (request, response) => {
response.writeHead(302, {
Expand All @@ -47,6 +47,13 @@ test.before('setup', async () => {
response.end();
});

http.on('/redirect-with-uri-encoded-location', (request, response) => {
response.writeHead(302, {
location: new URL('/?test=it’s+ok', http.url).toString()
});
response.end();
});

http.on('/endless', (request, response) => {
response.writeHead(302, {
location: `${http.url}/endless`
Expand Down Expand Up @@ -187,10 +194,14 @@ test('redirect response contains old url', async t => {
t.is(requestUrl, `${http.url}/finite`);
});

test('redirect response contains utf8 with binary encoding', async t => {
test('redirect response contains UTF-8 with binary encoding', async t => {
t.is((await got(`${http.url}/redirect-with-utf8-binary`)).body, 'reached');
});

test('redirect response contains UTF-8 with URI encoding', async t => {
t.is((await got(`${http.url}/redirect-with-uri-encoded-location`)).body, 'reached');
});

test('throws on malformed redirect URI', async t => {
const error = await t.throwsAsync(got(`${http.url}/malformedRedirect`));
t.is(error.name, 'URIError');
Expand Down