-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: prefer using x-forwarded-for as clientAddress (#11101)
* feat: change node clientAddress use x-forwarded-for when ``` adapter: node({ mode: 'standalone', }) ``` * feat: prefer using x-forwarded-for as clientAddress * Update .changeset/healthy-planets-dream.md Co-authored-by: Emanuele Stoppa <[email protected]> * Update .changeset/healthy-planets-dream.md Co-authored-by: Emanuele Stoppa <[email protected]> * Apply suggestions from code review --------- Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
2d4c8fa
commit a6916e4
Showing
8 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"astro": minor | ||
--- | ||
|
||
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/client-address-node/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
9
packages/astro/test/fixtures/client-address-node/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:*" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/client-address-node/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.