Skip to content

Commit 90ece3b

Browse files
authored
Add support for unix domain sockets (#109)
1 parent 69c9dad commit 90ece3b

3 files changed

Lines changed: 48 additions & 8 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ const addresses = forwarded(req)
2828
```
2929

3030
Parse the `X-Forwarded-For` header from the request. Returns an array
31-
of the addresses, including the socket address for the `req`, in reverse
32-
order (i.e. index `0` is the socket address and the last index is the
33-
furthest address, typically the end-user).
31+
of the addresses, including the socket address for the `req` if present, in reverse
32+
order (i.e. index `0` is the socket address if present and the last index is the
33+
furthest address, typically the end-user). May return an empty array if no socket
34+
address (i.e. unix domain socket) and no `X-Forwarded-For` header.
3435

3536
## Testing
3637

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ function forwarded (req) {
1818
const socketAddr = req.socket.remoteAddress
1919

2020
if (!header || typeof header !== 'string') {
21-
return [socketAddr]
21+
return socketAddr ? [socketAddr] : []
2222
} else if (header.indexOf(',') === -1) {
2323
const remote = header.trim()
24-
return (remote.length)
25-
? [socketAddr, remote]
26-
: [socketAddr]
24+
if (socketAddr) {
25+
return (remote.length)
26+
? [socketAddr, remote]
27+
: [socketAddr]
28+
} else {
29+
return (remote.length) ? [remote] : []
30+
}
2731
} else {
2832
return parse(header, socketAddr)
2933
}
3034
}
3135

3236
function parse (header, socketAddr) {
33-
const result = [socketAddr]
37+
const result = socketAddr ? [socketAddr] : []
3438

3539
let end = header.length
3640
let start = end

test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,41 @@ test('should trim trailing OWS with tabs (HTAB)', function (t) {
138138
])
139139
})
140140

141+
test('should handle unix domain sockets without X-Forwarded-For header', function (t) {
142+
t.plan(1)
143+
const req = createReq(undefined)
144+
t.assert.deepStrictEqual(forwarded(req), [])
145+
})
146+
147+
test('should handle unix domain sockets with X-Forwarded-For header containing no addresses', function (t) {
148+
t.plan(1)
149+
const req = createReq(undefined, {
150+
'x-forwarded-for': ' '
151+
})
152+
t.assert.deepStrictEqual(forwarded(req), [])
153+
})
154+
155+
test('should handle unix domain sockets with X-Forwarded-For header containing one address', function (t) {
156+
t.plan(1)
157+
const req = createReq(undefined, {
158+
'x-forwarded-for': '10.0.0.1'
159+
})
160+
t.assert.deepStrictEqual(forwarded(req), [
161+
'10.0.0.1'
162+
])
163+
})
164+
165+
test('should handle unix domain sockets with X-Forwarded-For header containing multiple addresses', function (t) {
166+
t.plan(1)
167+
const req = createReq(undefined, {
168+
'x-forwarded-for': '10.0.0.2, 10.0.0.1'
169+
})
170+
t.assert.deepStrictEqual(forwarded(req), [
171+
'10.0.0.1',
172+
'10.0.0.2'
173+
])
174+
})
175+
141176
function createReq (socketAddr, headers) {
142177
return {
143178
socket: {

0 commit comments

Comments
 (0)