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

Use node 7+ WHATWG parser for hostname, fixes #1002 #1004

Merged
merged 4 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 20 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Module dependencies.
*/

const URL = require('url').URL;
const net = require('net');
const contentType = require('content-type');
const stringify = require('url').format;
Copy link
Contributor Author

@fl0w fl0w Jun 17, 2017

Choose a reason for hiding this comment

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

Thought about destructing here (const { format: stringify, URL } = require('url')),
though I'm not seeing it elsewhere so assumed it was taboo in koajs/koa.

Copy link
Member

Choose a reason for hiding this comment

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

@fl0w i think we didn't add it for node v4 support previously

Expand Down Expand Up @@ -244,9 +245,28 @@ module.exports = {
get hostname() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This needs some adjusting - the code path is weird and for some reason I'm not using host whilst also assigning const host = this.host (relic from the past). I'll clean this up later today.

const host = this.host;
if (!host) return '';
if ('[' == host[0]) return this.URL.hostname; // IPv6
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

this.URL can be an empty object, return this.URL.hostname || '' here

return host.split(':')[0];
},

/**
* Get WHATWG parsed URL.
* Lazily memoized.
*
* @return {URL}
* @api public
*/

get URL() {
if (!this.memoizedURL) {
const protocol = this.protocol;
const host = this.host;
const originalUrl = this.originalUrl || ''; // avoid undefined in template string
this.memoizedURL = new URL(`${protocol}://${host}${originalUrl}`);
Copy link
Contributor Author

@fl0w fl0w Jun 19, 2017

Choose a reason for hiding this comment

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

URL will throw TypeError if something's off.
Koa generally has value || '' type of solutions in most cases.

Maybe wrap this in a try-catch and memoize empty object so that this.URL.hostname when malformed does not throw/crash the entire app?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A try-catch shouldn't deopt since node 7.x (v8/v8@9aac80f)

Copy link
Member

Choose a reason for hiding this comment

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

Maybe wrap this in a try-catch and memoize empty object so that this.URL.hostname when malformed does not throw/crash the entire app?

+1

}
return this.memoizedURL;
},

/**
* Check if the request is fresh, aka
* Last-Modified and/or the ETag
Expand Down
26 changes: 26 additions & 0 deletions test/request/hostname.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ describe('req.hostname', () => {
});
});

describe('with IPv6 in host', () => {
it('should parse localhost void of port', () => {
const req = request();
req.header.host = '[::1]';
assert.equal(req.hostname, '[::1]');
});

it('should parse localhost with port 80', () => {
const req = request();
req.header.host = '[::1]:80';
assert.equal(req.hostname, '[::1]');
});

it('should parse localhost with non special schema port', () => {
const req = request();
req.header.host = '[::1]:1337';
assert.equal(req.hostname, '[::1]');
});

it('should reduce IPv6 with non special schema port, as hostname', () => {
const req = request();
req.header.host = '[2001:cdba:0000:0000:0000:0000:3257:9652]:1337';
assert.equal(req.hostname, '[2001:cdba::3257:9652]');
});
});

describe('when X-Forwarded-Host is present', () => {
describe('and proxy is not trusted', () => {
it('should be ignored', () => {
Expand Down