Skip to content

Add optional support for unix domain sockets - #119

Open
fenichelar wants to merge 4 commits into
fastify:mainfrom
fenichelar:main
Open

fenichelar wants to merge 4 commits into
fastify:mainfrom
fenichelar:main

Conversation

@fenichelar

Copy link
Copy Markdown

Adds optional support for unix domain sockets. Removes @fastify/forwarded dependency to do so.

Checklist

Signed-off-by: Alec Fenichel <[email protected]>
@Tony133
Tony133 requested a review from a team October 13, 2026 13:24
Comment thread README.md
```

### proxyaddr(req, trust)
### proxyaddr(req, trust, uds)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not 100% convinced that a new parameter should be added; why not included within the trust one?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The review found an issue that should be addressed before approval.

10 open issues
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 uds argument to proxyaddr and proxyaddr.all.
  • Replaces @fastify/forwarded with 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

Comment thread README.md
Comment on lines +33 to +34
The optional `uds` (Unix domain socket) argument indicates whether
an undefined socket remote address should be treated as trusted.
Comment thread README.md
from the socket address.

### proxyaddr.all(req, [trust])
### proxyaddr.all(req, [trust], [uds])
Comment thread index.js
module.exports.proxyaddr = proxyaddr
module.exports.all = alladdrs
module.exports.compile = compile
module.exports.forwarded = forwarded
Comment thread index.js
return []
}

header = header.trim()
Comment thread index.js
Comment on lines +63 to +64
// Based on https://github.com/fastify/forwarded
// a fork of https://github.com/jshttp/forwarded
Comment thread index.js
*/

function alladdrs (req, trust) {
function alladdrs (req, trust, uds) {
Comment thread index.js
const addrs = forwarded(req)
const socketAddr = req.socket.remoteAddress
const header = forwarded(req.headers['x-forwarded-for'])
const addrs = uds && !socketAddr ? header : [socketAddr, ...header]
Comment thread index.js
*/

function proxyaddr (req, trust) {
function proxyaddr (req, trust, uds) {
Comment thread test/base.test.js
Comment on lines +389 to +394
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')
})
Comment thread test/forwarded.test.js
Comment on lines +10 to +12
test('with no x-forwarded-for header should return empty array', function (t) {
t.assert.deepStrictEqual(proxyaddr.forwarded(), [])
})

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why do you need to remove forwarded

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants