Skip to content

Commit 97c435f

Browse files
kanongilMarsup
andauthored
Fix invalid hostname parsing for ipv6 formatted host header (#4564)
* Fix invalid hostname parsing for ipv6 formatted host header * Apply suggestions from code review --------- Co-authored-by: Nicolas Morel <nicolas@morel.io>
1 parent 62032e6 commit 97c435f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ internals.Info = class {
630630
this.received = received;
631631
this.referrer = req.headers.referrer || req.headers.referer || '';
632632
this.host = host;
633-
this.hostname = host.split(':')[0];
633+
this.hostname = /^(.*?)(?::\d+)?$/.exec(host)[1];
634634
this.id = `${received}:${request._core.info.id}:${request._core._counter()}`;
635635

636636
this._remoteAddress = null;

test/request.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ describe('Request.Generator', () => {
123123

124124
describe('Request', () => {
125125

126+
it('sets host and hostname', async () => {
127+
128+
const server = Hapi.server();
129+
130+
const handler = (request) => {
131+
132+
return [request.info.host, request.info.hostname].join('|');
133+
};
134+
135+
server.route({ method: 'GET', path: '/', handler });
136+
137+
const res1 = await server.inject({ url: '/', headers: { host: 'host' } });
138+
expect(res1.payload).to.equal('host|host');
139+
140+
const res2 = await server.inject({ url: '/', headers: { host: 'host:123' } });
141+
expect(res2.payload).to.equal('host:123|host');
142+
143+
const res3 = await server.inject({ url: '/', headers: { host: '127.0.0.1' } });
144+
expect(res3.payload).to.equal('127.0.0.1|127.0.0.1');
145+
146+
const res4 = await server.inject({ url: '/', headers: { host: '127.0.0.1:123' } });
147+
expect(res4.payload).to.equal('127.0.0.1:123|127.0.0.1');
148+
149+
const res5 = await server.inject({ url: '/', headers: { host: '[::1]' } });
150+
expect(res5.payload).to.equal('[::1]|[::1]');
151+
152+
const res6 = await server.inject({ url: '/', headers: { host: '[::1]:123' } });
153+
expect(res6.payload).to.equal('[::1]:123|[::1]');
154+
});
155+
126156
it('sets client address (default)', async (flags) => {
127157

128158
const server = Hapi.server();

0 commit comments

Comments
 (0)