From 4cff4dcd9aa2d99f44fabc7b2ebfbcf5a44a788f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Sat, 10 Oct 2026 15:10:14 +0200 Subject: [PATCH] Revert "Add support for unix domain sockets (#109)" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 90ece3bf26fe72acffaff64413dd57fd97772259. Dropping a missing socket address from the result shifts every entry down by one, so @fastify/proxy-addr treats the last X-Forwarded-For entry as the socket address. Since 3.1.0, fastify's CI fails on test/trust-proxy.test.js ("trust proxy with number and null socket remoteAddress ignores forwarded headers"): request.ip is '1.1.1.1' instead of null. Reverting restores the behavior of 3.0.x and fixes fastify's CI. Signed-off-by: Gürgün Dayıoğlu --- README.md | 7 +++---- index.js | 14 +++++--------- test/test.js | 35 ----------------------------------- 3 files changed, 8 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index b1b2948..321666a 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,9 @@ const addresses = forwarded(req) ``` Parse the `X-Forwarded-For` header from the request. Returns an array -of the addresses, including the socket address for the `req` if present, in reverse -order (i.e. index `0` is the socket address if present and the last index is the -furthest address, typically the end-user). May return an empty array if no socket -address (i.e. unix domain socket) and no `X-Forwarded-For` header. +of the addresses, including the socket address for the `req`, in reverse +order (i.e. index `0` is the socket address and the last index is the +furthest address, typically the end-user). ## Testing diff --git a/index.js b/index.js index d838c76..1195216 100644 --- a/index.js +++ b/index.js @@ -18,23 +18,19 @@ function forwarded (req) { const socketAddr = req.socket.remoteAddress if (!header || typeof header !== 'string') { - return socketAddr ? [socketAddr] : [] + return [socketAddr] } else if (header.indexOf(',') === -1) { const remote = header.trim() - if (socketAddr) { - return (remote.length) - ? [socketAddr, remote] - : [socketAddr] - } else { - return (remote.length) ? [remote] : [] - } + return (remote.length) + ? [socketAddr, remote] + : [socketAddr] } else { return parse(header, socketAddr) } } function parse (header, socketAddr) { - const result = socketAddr ? [socketAddr] : [] + const result = [socketAddr] let end = header.length let start = end diff --git a/test/test.js b/test/test.js index 83f2a4b..b7e3439 100644 --- a/test/test.js +++ b/test/test.js @@ -138,41 +138,6 @@ test('should trim trailing OWS with tabs (HTAB)', function (t) { ]) }) -test('should handle unix domain sockets without X-Forwarded-For header', function (t) { - t.plan(1) - const req = createReq(undefined) - t.assert.deepStrictEqual(forwarded(req), []) -}) - -test('should handle unix domain sockets with X-Forwarded-For header containing no addresses', function (t) { - t.plan(1) - const req = createReq(undefined, { - 'x-forwarded-for': ' ' - }) - t.assert.deepStrictEqual(forwarded(req), []) -}) - -test('should handle unix domain sockets with X-Forwarded-For header containing one address', function (t) { - t.plan(1) - const req = createReq(undefined, { - 'x-forwarded-for': '10.0.0.1' - }) - t.assert.deepStrictEqual(forwarded(req), [ - '10.0.0.1' - ]) -}) - -test('should handle unix domain sockets with X-Forwarded-For header containing multiple addresses', function (t) { - t.plan(1) - const req = createReq(undefined, { - 'x-forwarded-for': '10.0.0.2, 10.0.0.1' - }) - t.assert.deepStrictEqual(forwarded(req), [ - '10.0.0.1', - '10.0.0.2' - ]) -}) - function createReq (socketAddr, headers) { return { socket: {