Repository navigation
Add optional support for unix domain sockets - #119
fenichelar wants to merge 4 commits into
Conversation
Signed-off-by: Alec Fenichel <[email protected]>
Signed-off-by: Alec Fenichel <[email protected]>
Signed-off-by: Alec Fenichel <[email protected]>
Signed-off-by: Alec Fenichel <[email protected]>
| ``` | ||
|
|
||
| ### proxyaddr(req, trust) | ||
| ### proxyaddr(req, trust, uds) |
There was a problem hiding this comment.
Not 100% convinced that a new parameter should be added; why not included within the trust one?
There was a problem hiding this comment.
I was thinking this would help clarify the distinction between this and regular trust. When an IP address is trusted that trust applies in the socket and in the X-Forwarded-For header. When UDS is set, it signals to the function that the socket address is expected to be undefined and that the first IP address in the X-Forwarded-For header is the starting point.
That being said, happy to change it to use the trust parameter if that is preferable. Any suggestions on what to put in the trust parameter to indicate unix domain sockets are being used?
There was a problem hiding this comment.
See your point; agree that it will be better to keep it separate. Maybe we can create a new opts object that has this property. In that way is a bit more explicit than a positional argument, and if more flags start to being added, we can also add it to that baggage without requiring to adjust positional args.
There was a problem hiding this comment.
🟡 Changes recommended
The review found an issue that should be addressed before approval.
10 open issues
This wording impliesudscan be enabled generally. However, Node.js documents that… · New The signature now lists[uds], but this section never explains what it does. The paragraph right… · New [P2] The new publicforwardedexport is absent fromtypes/index.d.ts, so TypeScript consumers… · New [P2] Trimming the whole multi-address header changes the previous parser's trust-boundary behavior:… · New [P2] The copied parser's [upstream MIT notice](https://github.com/fastify/forwarded/blob/33dbfbb6ae0… · New [P2] Theproxyaddr.alldeclaration intypes/index.d.tsstill accepts at most two arguments, so… · New [P1] Preserve the socket's hop in the trust callback index. Withudsenabled, removing the socket… · New [P2] The newudsargument is missing from theproxyaddrdeclaration intypes/index.d.ts.… · New Should fix: There is only one test for the newudsflag, and it covers a single string trust… · New This test is identical to the one above it: both callproxyaddr.forwarded()with no argument, so… · New
What changed in this PR
Adds opt-in Unix domain socket support to request-address resolution, allowing sockets without a remote address to be skipped when evaluating forwarded addresses.
Changes:
- Adds an optional
udsargument toproxyaddrandproxyaddr.all. - Replaces
@fastify/forwardedwith an internal, publicly exported header parser. - Adds parser and Unix domain socket tests and updates API documentation.
| File | Description |
|---|---|
| test/forwarded.test.js | Updated as part of this pull request. |
| test/base.test.js | Updated as part of this pull request. |
| README.md | Updated as part of this pull request. |
| package.json | Updated as part of this pull request. |
| index.js | Updated as part of this pull request. |
Review effort: Max
| The optional `uds` (Unix domain socket) argument indicates whether | ||
| an undefined socket remote address should be treated as trusted. |
| from the socket address. | ||
|
|
||
| ### proxyaddr.all(req, [trust]) | ||
| ### proxyaddr.all(req, [trust], [uds]) |
| module.exports.proxyaddr = proxyaddr | ||
| module.exports.all = alladdrs | ||
| module.exports.compile = compile | ||
| module.exports.forwarded = forwarded |
| return [] | ||
| } | ||
|
|
||
| header = header.trim() |
| // Based on https://github.com/fastify/forwarded | ||
| // a fork of https://github.com/jshttp/forwarded |
| */ | ||
|
|
||
| function alladdrs (req, trust) { | ||
| function alladdrs (req, trust, uds) { |
| const addrs = forwarded(req) | ||
| const socketAddr = req.socket.remoteAddress | ||
| const header = forwarded(req.headers['x-forwarded-for']) | ||
| const addrs = uds && !socketAddr ? header : [socketAddr, ...header] |
| */ | ||
|
|
||
| function proxyaddr (req, trust) { | ||
| function proxyaddr (req, trust, uds) { |
| test('when socket address undefined and uds set should skip socket', function (t) { | ||
| const req = createReq(undefined, { | ||
| 'x-forwarded-for': '127.0.0.1, 10.0.0.1' | ||
| }) | ||
| t.assert.strictEqual(proxyaddr(req, '127.0.0.1', true), '10.0.0.1') | ||
| }) |
| test('with no x-forwarded-for header should return empty array', function (t) { | ||
| t.assert.deepStrictEqual(proxyaddr.forwarded(), []) | ||
| }) |
mcollina
left a comment
There was a problem hiding this comment.
why do you need to remove forwarded

Adds optional support for unix domain sockets. Removes
@fastify/forwardeddependency to do so.Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct