Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prefer using x-forwarded-for as clientAddress #11101

Merged
merged 7 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/healthy-planets-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": major
linguofeng marked this conversation as resolved.
Show resolved Hide resolved
---

prefer using x-forwarded-for as clientAddress
linguofeng marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions packages/astro/test/client-address-node.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
Original file line number Diff line number Diff line change
@@ -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' }),
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/client-address-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/client-address-node",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/node": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
export const prerender = false;
const address = Astro.clientAddress;
---
<html>
<head>
<title>Astro.clientAddress</title>
</head>
<body>
<h1>Astro.clientAddress</h1>
<div id="address">{ address }</div>
</body>
</html>
8 changes: 8 additions & 0 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +42,7 @@ process.env.ASTRO_TELEMETRY_DISABLED = true;
* @property {typeof preview} preview
* @property {() => Promise<void>} clean
* @property {() => Promise<App>} loadTestAdapterApp
* @property {() => Promise<(req: NodeRequest, res: NodeResponse) => void>} loadNodeAdapterHandler
* @property {() => Promise<void>} onNextChange
* @property {typeof check} check
* @property {typeof sync} sync
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.