Skip to content

Commit a0efdc4

Browse files
joshkeldevinivyDJMcK
authored
Fix handling of no remoteAddress available (#4396)
* Fix handling of no remoteAddress available Fixes #4395 * Tidy tests Co-authored-by: David McKeown <DJMcK@users.noreply.github.com> * Lint Co-authored-by: devin ivy <devinivy@gmail.com> Co-authored-by: David McKeown <DJMcK@users.noreply.github.com>
1 parent c07b506 commit a0efdc4

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ internals.Info = class {
658658
if (!this._remoteAddress) {
659659
const ipv6Prefix = '::ffff:';
660660
const socketAddress = this._request.raw.req.socket.remoteAddress;
661-
if (socketAddress.startsWith(ipv6Prefix) && socketAddress.includes('.', ipv6Prefix.length)) {
661+
if (socketAddress && socketAddress.startsWith(ipv6Prefix) && socketAddress.includes('.', ipv6Prefix.length)) {
662662
// Normalize IPv4-mapped IPv6 address, e.g. ::ffff:127.0.0.1 -> 127.0.0.1
663663
this._remoteAddress = socketAddress.slice(ipv6Prefix.length);
664664
}

test/request.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,43 @@ describe('Request', () => {
222222
expect(payload.toString()).to.equal('100.100.100.100');
223223
});
224224

225+
it('sets client address to nothing when not available', async (flags) => {
226+
227+
const server = Hapi.server();
228+
const abortedReqTeam = new Teamwork.Team();
229+
let remoteAddr = 'not executed';
230+
231+
server.route({
232+
method: 'GET',
233+
path: '/',
234+
options: {
235+
handler: async (request, h) => {
236+
237+
req.destroy();
238+
239+
while (request.active()) {
240+
await Hoek.wait(5);
241+
}
242+
243+
abortedReqTeam.attend();
244+
245+
remoteAddr = request.info.remoteAddress;
246+
return null;
247+
}
248+
}
249+
});
250+
251+
await server.start();
252+
flags.onCleanup = () => server.stop();
253+
254+
const req = Http.get(server.info.uri, Hoek.ignore);
255+
req.on('error', Hoek.ignore);
256+
257+
await abortedReqTeam.work;
258+
259+
expect(remoteAddr).to.equal(undefined);
260+
});
261+
225262
it('sets port to nothing when not available', async () => {
226263

227264
const server = Hapi.server({ debug: false });

0 commit comments

Comments
 (0)