Skip to content

Commit dccc2cd

Browse files
authored
Merge pull request #43 from fingerprintjs/fix/always-send-all-proxy-headers
fix: send other proxy headers even if proxy secret is undefined INTER-1158
2 parents 03d26d3 + 5597be5 commit dccc2cd

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/utils/addProxyIntegrationHeaders.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export function addProxyIntegrationHeaders(headers: Headers, url: string, env: I
55
const proxySecret = getProxySecret(env)
66
if (proxySecret) {
77
headers.set('FPJS-Proxy-Secret', proxySecret)
8-
headers.set('FPJS-Proxy-Client-IP', getClientIp())
9-
headers.set('FPJS-Proxy-Forwarded-Host', new URL(url).hostname)
108
}
9+
10+
headers.set('FPJS-Proxy-Forwarded-Host', new URL(url).hostname)
11+
headers.set('FPJS-Proxy-Client-IP', getClientIp())
1112
}

test/handlers/ingressAPI.test.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,46 @@ describe('Ingress', () => {
105105
expect(cookieValue['_iidt']).toBe('test')
106106
})
107107

108-
it('should not add proxy integration headers if PROXY_SECRET env not set', async () => {
109-
const request = makeRequest(new URL('https://test/result'), { method: 'POST' })
108+
it('should add all proxy integration headers if PROXY_SECRET is present, preserving existing headers', async () => {
109+
const existingHeaderValue = 'testValue'
110+
const secret = '42'
111+
const secretStore = new SecretStore('Fingerprint')
112+
// @ts-ignore
113+
secretStore.set('PROXY_SECRET', secret)
114+
115+
const request = makeRequest(new URL('https://test/result'), {
116+
method: 'POST',
117+
headers: { 'X-Test': existingHeaderValue },
118+
})
110119
await handleRequest(request)
111120

112-
expect(requestHeaders.has('FPJS-Proxy-Secret')).toBe(false)
113-
expect(requestHeaders.has('FPJS-Proxy-Client-IP')).toBe(false)
114-
expect(requestHeaders.has('FPJS-Proxy-Forwarded-Host')).toBe(false)
121+
expect(requestHeaders.has('FPJS-Proxy-Secret')).toBe(true)
122+
expect(requestHeaders.has('FPJS-Proxy-Client-IP')).toBe(true)
123+
expect(requestHeaders.has('FPJS-Proxy-Forwarded-Host')).toBe(true)
124+
125+
expect(requestHeaders.get('X-Test')).toBe(existingHeaderValue)
126+
expect(requestHeaders.get('FPJS-Proxy-Secret')).toBe(secret)
115127
})
116128

117-
it('should add proxy integration headers if PROXY_SECRET is present', async () => {
129+
// So Fingerprint server can know the request is coming from a proxy integration, even if the proxy secret is missing
130+
it('should add the other proxy integration headers even if PROXY_SECRET is not set, preserving existing headers', async () => {
131+
const existingHeaderValue = 'testValue'
118132
const secretStore = new SecretStore('Fingerprint')
119133
// @ts-ignore
120-
secretStore.set('PROXY_SECRET', 'secret')
134+
// Reset the secret to undefined in case a previous test defined it
135+
secretStore.set('PROXY_SECRET', undefined)
121136

122-
const request = makeRequest(new URL('https://test/result'), { method: 'POST' })
137+
const request = makeRequest(new URL('https://test/result'), {
138+
method: 'POST',
139+
headers: { 'X-Test': existingHeaderValue },
140+
})
123141
await handleRequest(request)
124142

125-
expect(requestHeaders.has('FPJS-Proxy-Secret')).toBe(true)
143+
expect(requestHeaders.has('FPJS-Proxy-Secret')).toBe(false)
126144
expect(requestHeaders.has('FPJS-Proxy-Client-IP')).toBe(true)
127145
expect(requestHeaders.has('FPJS-Proxy-Forwarded-Host')).toBe(true)
146+
147+
expect(requestHeaders.get('X-Test')).toBe(existingHeaderValue)
128148
})
129149

130150
it('should set client ip if request has header Fastly-Client-IP', async () => {

0 commit comments

Comments
 (0)