From 49eb9c7fd1ed51baae67ac652fc085fbc3c7e7bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 13:30:33 +0900 Subject: [PATCH 1/9] fix: preserve query parameters for agent request --- proxy/handlers/agent.test.ts | 17 +++++++++++++++++ proxy/handlers/agent.ts | 14 +++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/proxy/handlers/agent.test.ts b/proxy/handlers/agent.test.ts index 7da233d2..dc1966c2 100644 --- a/proxy/handlers/agent.test.ts +++ b/proxy/handlers/agent.test.ts @@ -64,6 +64,23 @@ describe('Agent Endpoint', () => { }) }) + test('Call with custom query parameters', async () => { + const req = mockRequestGet('https://fp.domain.com', 'fpjs/agent', { + apiKey: 'ujKG34hUYKLJKJ1F', + version: '5', + customQuery: '123', + }) + const ctx = mockContext(req) + + await proxy(ctx, req) + + const [url] = requestSpy.mock.calls[0] + + expect(url.toString()).toEqual( + `https://${origin}/v5/ujKG34hUYKLJKJ1F?customQuery=123&ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` + ) + }) + test('Call with version', async () => { const req = mockRequestGet('https://fp.domain.com', 'fpjs/agent', { apiKey: 'ujKG34hUYKLJKJ1F', diff --git a/proxy/handlers/agent.ts b/proxy/handlers/agent.ts index 63cfd5aa..d356a29a 100644 --- a/proxy/handlers/agent.ts +++ b/proxy/handlers/agent.ts @@ -2,7 +2,7 @@ import { HttpRequest, Logger } from '@azure/functions' import { config } from '../utils/config' import * as https from 'https' import { filterRequestHeaders, updateResponseHeadersForAgentDownload } from '../utils/headers' -import { HttpResponseSimple } from '@azure/functions/types/http' +import { HttpRequestQuery, HttpResponseSimple } from '@azure/functions/types/http' import { addTrafficMonitoringSearchParamsForProCDN } from '../utils/traffic' import { IntegrationError } from '../errors/IntegrationError' @@ -14,6 +14,16 @@ export interface DownloadAgentParams { const DEFAULT_VERSION = '3' +function copySearchParams(query: HttpRequestQuery, newURL: URL): void { + const params = new URLSearchParams(query) + + params.delete('apiKey') + params.delete('version') + params.delete('loaderVersion') + + newURL.search = params.toString() +} + export async function downloadAgent({ httpRequest, logger, path }: DownloadAgentParams): Promise { const apiKey = httpRequest.query.apiKey const version = httpRequest.query.version ?? DEFAULT_VERSION @@ -30,6 +40,8 @@ export async function downloadAgent({ httpRequest, logger, path }: DownloadAgent } const url = new URL(`https://${config.fpcdn}`) + copySearchParams(httpRequest.query, url) + url.pathname = getEndpoint(apiKey, version, loaderVersion) addTrafficMonitoringSearchParamsForProCDN(url) From c7f3783280ed4c87d33e938bc19446c3c408f14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 13:32:18 +0900 Subject: [PATCH 2/9] fix: omit cookies when sending request to CDN --- proxy/handlers/agent.test.ts | 4 ++-- proxy/handlers/agent.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/proxy/handlers/agent.test.ts b/proxy/handlers/agent.test.ts index dc1966c2..c499bb19 100644 --- a/proxy/handlers/agent.test.ts +++ b/proxy/handlers/agent.test.ts @@ -231,7 +231,7 @@ describe('Agent Endpoint', () => { }) }) - test('Req body and headers are the same, expect cookies, which should include only _iidt cookie', async () => { + test('Req body and headers are the same, expect cookies, which should be omitted', async () => { const req = mockRequestGet('https://fp.domain.com', 'fpjs/agent', { apiKey: 'ujKG34hUYKLJKJ1F', version: '5', @@ -259,7 +259,7 @@ describe('Agent Endpoint', () => { expect(options.headers).toEqual({ ...req.headers, - cookie: '_iidt=GlMQaHMfzYvomxCuA7Uymy7ArmjH04jPkT+enN7j/Xk8tJG+UYcQV+Qw60Ry4huw9bmDoO/smyjQp5vLCuSf8t4Jow==', + cookie: undefined, }) }) diff --git a/proxy/handlers/agent.ts b/proxy/handlers/agent.ts index d356a29a..ce02f166 100644 --- a/proxy/handlers/agent.ts +++ b/proxy/handlers/agent.ts @@ -49,6 +49,8 @@ export async function downloadAgent({ httpRequest, logger, path }: DownloadAgent const headers = filterRequestHeaders(httpRequest.headers) + delete headers['cookie'] + return new Promise((resolve) => { const data: any[] = [] From 554b1e6876bf0bb8fb1f0fe445fc5875a27126d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 13:39:21 +0900 Subject: [PATCH 3/9] fix: set cookies to undefined if _iidt cookie is not present in ingress request --- proxy/handlers/ingress.test.ts | 17 +++++++++++++++++ proxy/utils/headers.ts | 9 +++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/proxy/handlers/ingress.test.ts b/proxy/handlers/ingress.test.ts index c28a1396..1031b62a 100644 --- a/proxy/handlers/ingress.test.ts +++ b/proxy/handlers/ingress.test.ts @@ -114,6 +114,23 @@ describe('Result Endpoint', function () { expect(options.headers.cookie).toBe('_iidt=7A03Gwg') }) + test('Cookies are undefined if _iidt is not est', async () => { + const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId') + + req.headers.cookie = '_vid_t=gEFRuIQlzYmv692/UL4GLA==' + + mockSuccessfulResponse({ + checkRequestUrl: (url) => { + expect(url.toString()).toBe(`${defaultOrigin}/${search}`) + }, + }) + + await proxy(mockContext(req), req) + + const [, options] = requestSpy.mock.calls[0] + expect(options.headers.cookie).toBeUndefined() + }) + test('Request body and headers are not modified, expect strict-transport-security and transfer-encoding', async () => { const req = mockRequestGet('https://fp.domain.com', 'fpjs/resultId') const resHeaders = { diff --git a/proxy/utils/headers.ts b/proxy/utils/headers.ts index c51e0baa..d7f30fac 100644 --- a/proxy/utils/headers.ts +++ b/proxy/utils/headers.ts @@ -24,9 +24,14 @@ export function filterRequestHeaders(headers: HttpRequestHeaders) { headerValue = headerValue.split(/; */).join('; ') headerValue = filterCookie(headerValue, (key) => key === FPJS_COOKIE_NAME) - } - result[headerName] = headerValue + // Only set cookie header if there are relevant cookies + if (headerValue) { + result[headerName] = headerValue + } + } else { + result[headerName] = headerValue + } } return result From 25901fd970217e31989470e06a5355df88bb65ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 13:45:54 +0900 Subject: [PATCH 4/9] test: add new mock tests parameters --- e2e/scripts/mockTests.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/e2e/scripts/mockTests.ts b/e2e/scripts/mockTests.ts index cc3f69e6..9f2ca70a 100644 --- a/e2e/scripts/mockTests.ts +++ b/e2e/scripts/mockTests.ts @@ -1,5 +1,6 @@ import { execSync } from 'child_process' import { readTestInfo } from '../shared/testInfo' +import pkg from '../../package.json' async function main() { let hasError = false @@ -22,7 +23,7 @@ async function main() { try { execSync( - `npm exec -y "git+https://github.com/fingerprintjs/dx-team-mock-for-proxy-integrations-e2e-tests.git" -- --api-url="https://${apiUrl}" --host="${host}" --cdn-proxy-path="${agentPath}" --ingress-proxy-path="${resultPath}"`, + `npm exec -y "git+https://github.com/fingerprintjs/dx-team-mock-for-proxy-integrations-e2e-tests.git" -- --api-url="https://${apiUrl}" --host="${host}" --cdn-proxy-path="${agentPath}" --ingress-proxy-path="${resultPath}" --traffic-name="fingerprint-pro-azure" --integration-version=${pkg.version}`, { stdio: 'inherit', } From 79490c63c3c5b673bc96f699a98808d9b3564068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 19:44:20 +0900 Subject: [PATCH 5/9] chore: don't omit any query parameters --- proxy/handlers/agent.test.ts | 6 +++--- proxy/handlers/agent.ts | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/proxy/handlers/agent.test.ts b/proxy/handlers/agent.test.ts index c499bb19..d105050a 100644 --- a/proxy/handlers/agent.test.ts +++ b/proxy/handlers/agent.test.ts @@ -77,7 +77,7 @@ describe('Agent Endpoint', () => { const [url] = requestSpy.mock.calls[0] expect(url.toString()).toEqual( - `https://${origin}/v5/ujKG34hUYKLJKJ1F?customQuery=123&ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` + `https://${origin}/v5/ujKG34hUYKLJKJ1F?apiKey=ujKG34hUYKLJKJ1F&version=5&customQuery=123&ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` ) }) @@ -93,7 +93,7 @@ describe('Agent Endpoint', () => { const [url] = requestSpy.mock.calls[0] expect(url.toString()).toEqual( - `https://${origin}/v5/ujKG34hUYKLJKJ1F?ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` + `https://${origin}/v5/ujKG34hUYKLJKJ1F?apiKey=ujKG34hUYKLJKJ1F&version=5&ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` ) }) @@ -110,7 +110,7 @@ describe('Agent Endpoint', () => { const [url] = requestSpy.mock.calls[0] expect(url.toString()).toEqual( - `https://${origin}/v5/ujKG34hUYKLJKJ1F/loader_v3.6.5.js?ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` + `https://${origin}/v5/ujKG34hUYKLJKJ1F/loader_v3.6.5.js?apiKey=ujKG34hUYKLJKJ1F&version=5&loaderVersion=3.6.5&ii=fingerprint-pro-azure%2F__azure_function_version__%2Fprocdn` ) }) diff --git a/proxy/handlers/agent.ts b/proxy/handlers/agent.ts index ce02f166..2f6e290c 100644 --- a/proxy/handlers/agent.ts +++ b/proxy/handlers/agent.ts @@ -17,10 +17,6 @@ const DEFAULT_VERSION = '3' function copySearchParams(query: HttpRequestQuery, newURL: URL): void { const params = new URLSearchParams(query) - params.delete('apiKey') - params.delete('version') - params.delete('loaderVersion') - newURL.search = params.toString() } From ab99c539d3f2bb11c5fae2582e2b8499fbda303a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 19:45:37 +0900 Subject: [PATCH 6/9] test: change assertion --- proxy/handlers/ingress.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/handlers/ingress.test.ts b/proxy/handlers/ingress.test.ts index 1031b62a..d56c4834 100644 --- a/proxy/handlers/ingress.test.ts +++ b/proxy/handlers/ingress.test.ts @@ -128,7 +128,7 @@ describe('Result Endpoint', function () { await proxy(mockContext(req), req) const [, options] = requestSpy.mock.calls[0] - expect(options.headers.cookie).toBeUndefined() + expect(options.headers.cookie).toBeFalsy() }) test('Request body and headers are not modified, expect strict-transport-security and transfer-encoding', async () => { From 37852fd1a27f685413730fa5f6b92d71a8207fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 19:47:40 +0900 Subject: [PATCH 7/9] fix: simplify cookie parsing --- proxy/utils/headers.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/proxy/utils/headers.ts b/proxy/utils/headers.ts index d7f30fac..05b3e5a6 100644 --- a/proxy/utils/headers.ts +++ b/proxy/utils/headers.ts @@ -21,8 +21,6 @@ export function filterRequestHeaders(headers: HttpRequestHeaders) { let headerValue = value if (headerName === 'cookie') { - headerValue = headerValue.split(/; */).join('; ') - headerValue = filterCookie(headerValue, (key) => key === FPJS_COOKIE_NAME) // Only set cookie header if there are relevant cookies From 4329da66fdba1b152f7195b8eaca259c2c34860b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Thu, 18 Apr 2024 19:55:12 +0900 Subject: [PATCH 8/9] fix: remove cookies for browser cache requests --- proxy/handlers/ingress.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/proxy/handlers/ingress.ts b/proxy/handlers/ingress.ts index c1200c81..430c7699 100644 --- a/proxy/handlers/ingress.ts +++ b/proxy/handlers/ingress.ts @@ -42,6 +42,11 @@ export function handleIngress({ const headers = prepareHeadersForIngressAPI(httpRequest, preSharedSecret, logger) + // No need to send cookies for browser cache request + if (suffix) { + delete headers['cookie'] + } + return new Promise((resolve) => { const data: any[] = [] From 1fd204a7c3380d3eecefb2281ab3299bf83e6c0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20=C5=BBydek?= Date: Tue, 23 Apr 2024 15:25:21 +0900 Subject: [PATCH 9/9] ci: use new mock app parameters --- e2e/scripts/mockTests.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/e2e/scripts/mockTests.ts b/e2e/scripts/mockTests.ts index 9f2ca70a..0fb4e654 100644 --- a/e2e/scripts/mockTests.ts +++ b/e2e/scripts/mockTests.ts @@ -17,13 +17,19 @@ async function main() { const resultPath = `${info.routePrefix}/${info.getResultPath}` const host = info.functionAppUrl + const agentUrl = new URL(host) + agentUrl.pathname = agentPath + + const resultUrl = new URL(host) + resultUrl.pathname = resultPath + console.info('Running mock server for', host) console.info('Agent download path:', agentPath) console.info('Get result path:', resultPath) try { execSync( - `npm exec -y "git+https://github.com/fingerprintjs/dx-team-mock-for-proxy-integrations-e2e-tests.git" -- --api-url="https://${apiUrl}" --host="${host}" --cdn-proxy-path="${agentPath}" --ingress-proxy-path="${resultPath}" --traffic-name="fingerprint-pro-azure" --integration-version=${pkg.version}`, + `npm exec -y "git+https://github.com/fingerprintjs/dx-team-mock-for-proxy-integrations-e2e-tests.git" -- --api-url="https://${apiUrl}" --cdn-proxy-url="${agentUrl.toString()}" --ingress-proxy-url="${resultUrl.toString()}" --traffic-name="fingerprint-pro-azure" --integration-version=${pkg.version}`, { stdio: 'inherit', }