Skip to content

Commit

Permalink
fix: use x-azure-clientip first for resolving client ip
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnderScorer committed Jun 13, 2024
1 parent 341094f commit 869c9f7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
3 changes: 2 additions & 1 deletion proxy/utils/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const mockReq = {
'x-azure-requestchain': 'hops=1',
'x-azure-socketip': '46.204.4.119',
'x-forwarded-for': '127.0.0.1',
'x-azure-clientip': '127.0.0.1',
'x-forwarded-host': 'fpjs.sh',
},
user: null,
Expand Down Expand Up @@ -178,7 +179,7 @@ describe('prepareHeadersForIngressAPI', () => {
it('should not set secret if it is undefined', () => {
const result = prepareHeadersForIngressAPI(mockReq, undefined)

expect(result['fpjs-proxy-client-ip']).toBe(mockReq.headers['x-forwarded-for'])
expect(result['fpjs-proxy-client-ip']).toBe(mockReq.headers['x-azure-clientip'])
expect(result['fpjs-proxy-secret']).toBe(undefined)
expect(result['fpjs-proxy-forwarded-host']).toBe(undefined)
})
Expand Down
17 changes: 2 additions & 15 deletions proxy/utils/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,9 @@ export function updateResponseHeaders(
}

function resolveClientIp(request: HttpRequest, logger?: Logger) {
const forwardedFor = request.headers['x-forwarded-for']
const clientIp = request.headers['x-azure-clientip'] || request.headers['x-client-ip'] || request.headers['x-real-ip']

Check warning on line 73 in proxy/utils/headers.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 73 in proxy/utils/headers.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

if (forwardedFor) {
const [clientIp] = forwardedFor.split(',')

logger?.verbose('Client IP resolved from x-forwarded-for', {
clientIp,
forwardedFor,
})

return clientIp
}

const clientIp = request.headers['x-client-ip'] || request.headers['x-real-ip']

logger?.verbose('Client IP resolved from x-client-ip or x-real-ip', {
logger?.verbose('Client IP resolved', {
clientIp,
})

Expand Down
4 changes: 4 additions & 0 deletions shared/test/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const mockRequestGet = (url: string, uri: string, query: HttpRequestQuery
'x-azure-requestchain': 'hops=1',
'x-azure-socketip': '46.204.4.119',
'x-forwarded-for': '127.0.0.1',
'x-client-ip': '128.0.0.1',
'x-azure-clientip': '127.0.0.1',
},
query,
params: {
Expand Down Expand Up @@ -66,6 +68,8 @@ export const mockRequestPost = (url: string, uri: string) => {
'x-azure-requestchain': 'hops=1',
'x-azure-socketip': '46.204.4.119',
'x-forwarded-for': '127.0.0.1',
'x-client-ip': '128.0.0.1',
'x-azure-clientip': '127.0.0.1',
},
query: {},
params: {
Expand Down

0 comments on commit 869c9f7

Please sign in to comment.