Skip to content

Commit

Permalink
Set X-Forwarded-For in proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Apr 27, 2024
1 parent 0a2e154 commit 728d802
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _See the [full architecture guide](../README.md) first._
- Proxy allows only GET requests and HTTP/HTTPS protocols.
- Proxy do not allow requests to in-cloud IP addresses like `127.0.0.1`.
- Proxy removes cookie headers.
- Proxy set user’s IP in `X-Forwarded-For` header.

## Test Strategy

Expand Down
3 changes: 2 additions & 1 deletion proxy/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export function createProxyServer(config: {
let targetResponse = await fetch(url, {
headers: {
...(req.headers as HeadersInit),
host: new URL(url).host
'host': new URL(url).host,
'X-Forwarded-For': req.socket.remoteAddress!
},
method: req.method,
signal: AbortSignal.timeout(config.timeout)
Expand Down
14 changes: 14 additions & 0 deletions proxy/test/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,17 @@ test('checks Origin', async () => {
})
equal(response2.status, 400)
})

test('sends user IP to destination', async () => {
let response1 = await request(targetUrl)
equal(response1.status, 200)
let json1 = await response1.json()
equal(json1.request.headers['x-forwarded-for'], '::1')

let response2 = await request(targetUrl, {
headers: { 'X-Forwarded-For': '4.4.4.4' }
})
equal(response2.status, 200)
let json2 = await response2.json()
equal(json2.request.headers['x-forwarded-for'], '4.4.4.4, ::1')
})

0 comments on commit 728d802

Please sign in to comment.