From 72180e4ffc9c78660b53a9f25ea20cb755748e9b Mon Sep 17 00:00:00 2001 From: Guofeng Lin Date: Mon, 20 May 2024 16:40:15 +0900 Subject: [PATCH 1/5] feat: change node clientAddress use x-forwarded-for when ``` adapter: node({ mode: 'standalone', }) ``` --- packages/astro/src/core/app/node.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/app/node.ts b/packages/astro/src/core/app/node.ts index c28fa8754d7c..537c7f5e778b 100644 --- a/packages/astro/src/core/app/node.ts +++ b/packages/astro/src/core/app/node.ts @@ -81,7 +81,11 @@ export class NodeApp extends App { Object.assign(options, makeRequestBody(req)); } const request = new Request(url, options); - if (req.socket?.remoteAddress) { + + const clientIp = req.headers['x-forwarded-for']; + if (clientIp) { + Reflect.set(request, clientAddressSymbol, clientIp); + } else if (req.socket?.remoteAddress) { Reflect.set(request, clientAddressSymbol, req.socket.remoteAddress); } return request; From 9898431a1b9437341cd724e6ab666c533eda7d13 Mon Sep 17 00:00:00 2001 From: linguofeng Date: Tue, 21 May 2024 11:18:09 +0900 Subject: [PATCH 2/5] feat: prefer using x-forwarded-for as clientAddress --- .changeset/healthy-planets-dream.md | 5 ++++ .../astro/test/client-address-node.test.js | 27 +++++++++++++++++++ .../client-address-node/astro.config.mjs | 8 ++++++ .../fixtures/client-address-node/package.json | 9 +++++++ .../client-address-node/src/pages/index.astro | 13 +++++++++ packages/astro/test/test-utils.js | 8 ++++++ pnpm-lock.yaml | 9 +++++++ 7 files changed, 79 insertions(+) create mode 100644 .changeset/healthy-planets-dream.md create mode 100644 packages/astro/test/client-address-node.test.js create mode 100644 packages/astro/test/fixtures/client-address-node/astro.config.mjs create mode 100644 packages/astro/test/fixtures/client-address-node/package.json create mode 100644 packages/astro/test/fixtures/client-address-node/src/pages/index.astro diff --git a/.changeset/healthy-planets-dream.md b/.changeset/healthy-planets-dream.md new file mode 100644 index 000000000000..4321ccf877f2 --- /dev/null +++ b/.changeset/healthy-planets-dream.md @@ -0,0 +1,5 @@ +--- +"astro": major +--- + +prefer using x-forwarded-for as clientAddress diff --git a/packages/astro/test/client-address-node.test.js b/packages/astro/test/client-address-node.test.js new file mode 100644 index 000000000000..8114ea42d210 --- /dev/null +++ b/packages/astro/test/client-address-node.test.js @@ -0,0 +1,27 @@ +import * as assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import * as cheerio from 'cheerio'; +import { loadFixture } from './test-utils.js'; +import { createRequestAndResponse } from './units/test-utils.js'; + +describe('NodeClientAddress', () => { + it('clientAddress is 1.1.1.1', async () => { + const fixture = await loadFixture({ + root: './fixtures/client-address-node/', + }); + await fixture.build(); + const handle = await fixture.loadNodeAdapterHandler(); + const { req, res, text } = createRequestAndResponse({ + method: 'GET', + url: '/', + headers: { + 'x-forwarded-for': '1.1.1.1', + }, + }); + handle(req, res); + const html = await text(); + const $ = cheerio.load(html); + assert.equal(res.statusCode, 200); + assert.equal($('#address').text(), '1.1.1.1'); + }); +}); diff --git a/packages/astro/test/fixtures/client-address-node/astro.config.mjs b/packages/astro/test/fixtures/client-address-node/astro.config.mjs new file mode 100644 index 000000000000..ad9b3a3b16df --- /dev/null +++ b/packages/astro/test/fixtures/client-address-node/astro.config.mjs @@ -0,0 +1,8 @@ +import node from '@astrojs/node'; +import { defineConfig } from 'astro/config'; + +// https://astro.build/config +export default defineConfig({ + output: 'server', + adapter: node({ mode: 'middleware' }), +}); diff --git a/packages/astro/test/fixtures/client-address-node/package.json b/packages/astro/test/fixtures/client-address-node/package.json new file mode 100644 index 000000000000..4b1c6a5ee098 --- /dev/null +++ b/packages/astro/test/fixtures/client-address-node/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/client-address-node", + "version": "0.0.0", + "private": true, + "dependencies": { + "@astrojs/node": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/client-address-node/src/pages/index.astro b/packages/astro/test/fixtures/client-address-node/src/pages/index.astro new file mode 100644 index 000000000000..b720cfd34f85 --- /dev/null +++ b/packages/astro/test/fixtures/client-address-node/src/pages/index.astro @@ -0,0 +1,13 @@ +--- +export const prerender = false; +const address = Astro.clientAddress; +--- + + + Astro.clientAddress + + +

Astro.clientAddress

+
{ address }
+ + diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index bd7e1f9035ec..65968a0bce09 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -25,6 +25,8 @@ process.env.ASTRO_TELEMETRY_DISABLED = true; * @typedef {import('../src/core/app/index').App} App * @typedef {import('../src/cli/check/index').AstroChecker} AstroChecker * @typedef {import('../src/cli/check/index').CheckPayload} CheckPayload + * @typedef {import('http').IncomingMessage} NodeRequest + * @typedef {import('http').ServerResponse} NodeResponse * * * @typedef {Object} Fixture @@ -40,6 +42,7 @@ process.env.ASTRO_TELEMETRY_DISABLED = true; * @property {typeof preview} preview * @property {() => Promise} clean * @property {() => Promise} loadTestAdapterApp + * @property {() => Promise<(req: NodeRequest, res: NodeResponse) => void>} loadNodeAdapterHandler * @property {() => Promise} onNextChange * @property {typeof check} check * @property {typeof sync} sync @@ -213,6 +216,11 @@ export async function loadFixture(inlineConfig) { }); } }, + loadNodeAdapterHandler: async () => { + const url = new URL(`./server/entry.mjs?id=${fixtureId}`, config.outDir); + const { handler } = await import(url); + return handler; + }, loadTestAdapterApp: async (streaming) => { const url = new URL(`./server/entry.mjs?id=${fixtureId}`, config.outDir); const { createApp, manifest } = await import(url); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d2ec86c1f0d1..d297a22e07e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2381,6 +2381,15 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/client-address-node: + dependencies: + '@astrojs/node': + specifier: workspace:* + version: link:../../../../integrations/node + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/code-component: dependencies: astro: From 96ec026d0aef8934b476cdffdd92142cf5bc86e6 Mon Sep 17 00:00:00 2001 From: Guofeng Lin Date: Tue, 21 May 2024 18:01:41 +0900 Subject: [PATCH 3/5] Update .changeset/healthy-planets-dream.md Co-authored-by: Emanuele Stoppa --- .changeset/healthy-planets-dream.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/healthy-planets-dream.md b/.changeset/healthy-planets-dream.md index 4321ccf877f2..953bb8588739 100644 --- a/.changeset/healthy-planets-dream.md +++ b/.changeset/healthy-planets-dream.md @@ -1,5 +1,5 @@ --- -"astro": major +"astro": minor --- prefer using x-forwarded-for as clientAddress From f7c2a303776a78a3f0d08b6f560da4738d0a49f6 Mon Sep 17 00:00:00 2001 From: Guofeng Lin Date: Tue, 21 May 2024 18:01:47 +0900 Subject: [PATCH 4/5] Update .changeset/healthy-planets-dream.md Co-authored-by: Emanuele Stoppa --- .changeset/healthy-planets-dream.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/healthy-planets-dream.md b/.changeset/healthy-planets-dream.md index 953bb8588739..59ca375be8fa 100644 --- a/.changeset/healthy-planets-dream.md +++ b/.changeset/healthy-planets-dream.md @@ -2,4 +2,4 @@ "astro": minor --- -prefer using x-forwarded-for as clientAddress +The adapter now uses **first** the header `x-forwarded-for` for computing `clientAddress`. From 9155b818ce609d857ab620d7440c787b98750699 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 22 May 2024 08:32:45 +0100 Subject: [PATCH 5/5] Apply suggestions from code review --- .changeset/healthy-planets-dream.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.changeset/healthy-planets-dream.md b/.changeset/healthy-planets-dream.md index 59ca375be8fa..52f42fc5e96c 100644 --- a/.changeset/healthy-planets-dream.md +++ b/.changeset/healthy-planets-dream.md @@ -2,4 +2,6 @@ "astro": minor --- -The adapter now uses **first** the header `x-forwarded-for` for computing `clientAddress`. +Updates Astro's code for adapters to use the header `x-forwarded-for` to initialize the `clientAddress`. + +To take advantage of the new change, integration authors must upgrade the version of Astro in their adapter `peerDependencies` to `4.9.0`.