diff --git a/.changeset/facts-credential-sanitization.md b/.changeset/facts-credential-sanitization.md new file mode 100644 index 000000000..db4c65f63 --- /dev/null +++ b/.changeset/facts-credential-sanitization.md @@ -0,0 +1,8 @@ +--- +"@sapiom/core": minor +"@sapiom/fetch": patch +"@sapiom/axios": patch +"@sapiom/node-http": patch +--- + +Request and response facts now share one header and URL sanitizer. `@sapiom/fetch`, `@sapiom/axios` and `@sapiom/node-http` leave out the same credential headers (`authorization`, cookies, `x-api-key`, `api-key`, `bt-api-key` and other `*-api-key` / `*token*` / `*secret*` / `*password*` headers, payment signatures, `x-sapiom-*` key and token headers), and mask credential query values (`key`, `api_key`, `token`, `access_token`, `signature`, `sig`, `password`, …) and `user:pass@` in the reported `url` and `urlParsed.search`. Only the facts change: the outgoing request is sent exactly as before. `@sapiom/core` exports the helpers (`sanitizeHeadersForFacts`, `sanitizeUrlForFacts`, `isSensitiveHeaderName`, `isSensitiveQueryParam`). diff --git a/packages/axios/src/http-client.integration.test.ts b/packages/axios/src/http-client.integration.test.ts index 3330653d4..cc6255241 100644 --- a/packages/axios/src/http-client.integration.test.ts +++ b/packages/axios/src/http-client.integration.test.ts @@ -979,3 +979,76 @@ describe("Axios HTTP Client Integration Tests", () => { }); }); }); + +describe("Facts sanitization", () => { + it("keeps a relative URL's query out of urlParsed.pathname when there is no baseURL", async () => { + const axiosInstance = axios.create(); + const mockAxios = new MockAdapter(axiosInstance); + const { client: sapiomClient, mocks } = createMockSapiomClient(); + mocks.create.mockResolvedValue({ + id: "tx-rel", + status: TransactionStatus.AUTHORIZED, + } as any); + mocks.complete.mockResolvedValue({ + transaction: { id: "tx-rel", status: "completed" }, + } as any); + mockAxios.onGet(/\/geocode/).reply(200, { ok: true }); + + const client = withSapiom(axiosInstance, { sapiomClient }); + await client.get("/geocode?key=FAKEKEY&address=Paris#top"); + await flushPromises(); + + const request = mocks.create.mock.calls[0][0].requestFacts!.request as any; + expect(request.urlParsed.pathname).toBe("/geocode"); + expect(request.urlParsed.search).toBe("?key=[REDACTED]&address=Paris"); + expect(JSON.stringify(request)).not.toContain("FAKEKEY"); + expect(mockAxios.history.get[0].url).toBe( + "/geocode?key=FAKEKEY&address=Paris#top", + ); + mockAxios.restore(); + }); + + it("leaves credential headers out of facts and masks credential query values, without changing the request", async () => { + const axiosInstance = axios.create({ baseURL: "https://api.example.com" }); + const mockAxios = new MockAdapter(axiosInstance); + const { client: sapiomClient, mocks } = createMockSapiomClient(); + mocks.create.mockResolvedValue({ + id: "tx-facts", + status: TransactionStatus.AUTHORIZED, + } as any); + mocks.complete.mockResolvedValue({ + transaction: { id: "tx-facts", status: "completed" }, + } as any); + mockAxios + .onGet(/\/geocode/) + .reply( + 200, + { ok: true }, + { "set-cookie": "sid=FAKE", "x-request-id": "r-1" }, + ); + + const client = withSapiom(axiosInstance, { sapiomClient }); + await client.get("/geocode?key=FAKEKEY&address=Paris", { + headers: { "bt-api-key": "FAKEBT", accept: "application/json" }, + }); + await flushPromises(); + + const request = mocks.create.mock.calls[0][0].requestFacts!.request as any; + expect(request.url).toBe( + "https://api.example.com/geocode?key=[REDACTED]&address=Paris", + ); + expect(request.urlParsed.search).toBe("?key=[REDACTED]&address=Paris"); + // axios merges the header into its default `Accept`. + expect(request.headers).toEqual({ Accept: "application/json" }); + const response = (mocks.complete.mock.calls[0][1].responseFacts as any) + .facts; + expect(response.headers["set-cookie"]).toBeUndefined(); + expect(response.headers["x-request-id"]).toBe("r-1"); + + // The request itself still carries the real URL and headers. + const sent = mockAxios.history.get[0]; + expect(sent.url).toBe("/geocode?key=FAKEKEY&address=Paris"); + expect(sent.headers?.["bt-api-key"]).toBe("FAKEBT"); + mockAxios.restore(); + }); +}); diff --git a/packages/axios/src/interceptors.ts b/packages/axios/src/interceptors.ts index ee95ac892..8765c72b4 100644 --- a/packages/axios/src/interceptors.ts +++ b/packages/axios/src/interceptors.ts @@ -17,6 +17,8 @@ import { HttpClientResponseFacts, HttpClientErrorFacts, FailureMode, + sanitizeHeadersForFacts, + sanitizeUrlForFacts, } from "@sapiom/core"; import type { TransactionPollingConfig } from "@sapiom/core"; @@ -394,38 +396,39 @@ export function addAuthorizationInterceptor( protocol: parsed.protocol, hostname: parsed.hostname, pathname: parsed.pathname, - search: parsed.search, + search: sanitizeUrlForFacts(parsed.search), port: parsed.port ? parseInt(parsed.port) : null, }; } catch { + // A relative endpoint with no baseURL: keep the query out of the pathname. + const queryAt = endpoint.search(/[?#]/); + const pathname = queryAt === -1 ? endpoint : endpoint.slice(0, queryAt); + const query = + queryAt === -1 ? "" : endpoint.slice(queryAt).split("#")[0]; urlParsed = { protocol: "", hostname: "", - pathname: endpoint, - search: "", + pathname, + search: query.startsWith("?") ? sanitizeUrlForFacts(query) : "", port: null, }; } - const sanitizedHeaders: Record = {}; - if (axiosConfig.headers) { - Object.entries(axiosConfig.headers as Record).forEach( - ([key, value]) => { - const lowerKey = key.toLowerCase(); - if ( - !lowerKey.includes("auth") && - !lowerKey.includes("key") && - !lowerKey.includes("token") - ) { - sanitizedHeaders[key] = String(value); - } - }, - ); - } + // Shared list, plus this integration's broader substring rule (kept so no + // header it left out before is reported now). + const sanitizedHeaders = sanitizeHeadersForFacts( + axiosConfig.headers + ? Object.entries(axiosConfig.headers as Record) + : undefined, + (lowerKey) => + lowerKey.includes("auth") || + lowerKey.includes("key") || + lowerKey.includes("token"), + ); const requestFacts: HttpClientRequestFacts = { method, - url: fullUrl, + url: sanitizeUrlForFacts(fullUrl), urlParsed, headers: sanitizedHeaders, hasBody: !!axiosConfig.data, @@ -789,21 +792,11 @@ export function addCompletionInterceptor( const startTime = (axiosConfig as any).__sapiomStartTime || Date.now(); const durationMs = Date.now() - startTime; - const sanitizedHeaders: Record = {}; - const sensitiveHeaders = new Set([ - "set-cookie", - "authorization", - "x-api-key", - ]); - if (response.headers) { - Object.entries(response.headers as Record).forEach( - ([key, value]) => { - if (!sensitiveHeaders.has(key.toLowerCase())) { - sanitizedHeaders[key] = String(value); - } - }, - ); - } + const sanitizedHeaders = sanitizeHeadersForFacts( + response.headers + ? Object.entries(response.headers as Record) + : undefined, + ); const facts: HttpClientResponseFacts = { status: response.status, diff --git a/packages/core/README.md b/packages/core/README.md index 9c1a22048..dc2ca7d25 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -231,6 +231,15 @@ All HTTP integrations accept a config object: } ``` +### What request facts contain + +The HTTP integrations report each call's method, URL, headers and status as transaction facts. They never report the credentials the call was made with: + +- Headers left out: `Authorization`, `Proxy-Authorization`, cookies, `Sapiom-Identity`, payment signatures (`payment-signature`, `x-payment`), any name containing `api-key`, `apikey`, `token`, `secret`, `password`, `credential`, `signature` or `auth`, any `*-key`, every `bt-*`, and `x-sapiom-*` key, token and signature headers. +- URL (`url` and `urlParsed.search`): values of credential query parameters (`key`, `api_key`, `apikey`, `token`, `access_token`, `secret`, `signature`, `sig`, `password`, `X-Amz-Signature`, …) and any `user:pass@` are reported as `[REDACTED]`. The rest of the URL is reported as it is. + +Only the facts change; the request goes out with its real URL and headers. The same rules are exported for custom integrations: `isSensitiveHeaderName`, `isSensitiveQueryParam`, `sanitizeHeadersForFacts` and `sanitizeUrlForFacts`. + ### Environment Variables All integrations automatically read from environment: diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d29c9d008..9a1001413 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -52,6 +52,13 @@ export type { // Telemetry export { captureUserCallSite, getRuntimeInfo } from "./utils/telemetry.js"; +export { + REDACTED_FACT_VALUE, + isSensitiveHeaderName, + isSensitiveQueryParam, + sanitizeHeadersForFacts, + sanitizeUrlForFacts, +} from "./utils/facts-sanitization.js"; export type { CallSiteInfo, RuntimeInfo } from "./types/telemetry.js"; // Types diff --git a/packages/core/src/utils/facts-sanitization.test.ts b/packages/core/src/utils/facts-sanitization.test.ts new file mode 100644 index 000000000..c516325e5 --- /dev/null +++ b/packages/core/src/utils/facts-sanitization.test.ts @@ -0,0 +1,152 @@ +import { + REDACTED_FACT_VALUE, + isSensitiveHeaderName, + isSensitiveQueryParam, + sanitizeHeadersForFacts, + sanitizeUrlForFacts, +} from "./facts-sanitization"; + +describe("facts sanitization", () => { + describe("isSensitiveHeaderName", () => { + it.each([ + "authorization", + "Authorization", + "proxy-authorization", + "cookie", + "set-cookie", + "payment-signature", + "x-payment", + "x-api-key", + "api-key", + "apikey", + "bt-api-key", + "BT-Trace-Id", + "x-goog-api-key", + "x-auth-token", + "x-client-secret", + "x-db-password", + "x-license-key", + "x-sapiom-api-key", + "x-sapiom-service-key", + "x-sapiom-signature", + "x-sapiom-workflow-token", + "Sapiom-Identity", + ])("leaves out %s", (name) => { + expect(isSensitiveHeaderName(name)).toBe(true); + }); + + it.each([ + "content-type", + "accept", + "user-agent", + "x-request-id", + "x-sapiom-transaction-id", + "x-sapiom-trace-id", + ])("keeps %s", (name) => { + expect(isSensitiveHeaderName(name)).toBe(false); + }); + }); + + describe("isSensitiveQueryParam", () => { + it.each([ + "key", + "KEY", + "api_key", + "apikey", + "token", + "access_token", + "secret", + "signature", + "sig", + "password", + "X-Amz-Signature", + "api%5Fkey", + ])("masks %s", (name) => { + expect(isSensitiveQueryParam(name)).toBe(true); + }); + + it.each(["q", "limit", "keyword", "monkey", "author"])( + "keeps %s", + (name) => { + expect(isSensitiveQueryParam(name)).toBe(false); + }, + ); + }); + + describe("sanitizeUrlForFacts", () => { + it("masks credential query values and keeps every other part as it was", () => { + expect( + sanitizeUrlForFacts( + "https://api.example.com/v1/geocode?key=abc123&address=Paris&access_token=t#top", + ), + ).toBe( + `https://api.example.com/v1/geocode?key=${REDACTED_FACT_VALUE}&address=Paris&access_token=${REDACTED_FACT_VALUE}#top`, + ); + }); + + it("masks userinfo", () => { + expect( + sanitizeUrlForFacts("https://alice:hunter2@api.example.com/x"), + ).toBe(`https://${REDACTED_FACT_VALUE}@api.example.com/x`); + }); + + it("masks a password that itself contains @, and not an @ in the query", () => { + expect( + sanitizeUrlForFacts("https://alice:p@ss@api.example.com/x?q=a@b"), + ).toBe(`https://${REDACTED_FACT_VALUE}@api.example.com/x?q=a@b`); + expect(sanitizeUrlForFacts("https://api.example.com/x?q=a@b")).toBe( + "https://api.example.com/x?q=a@b", + ); + }); + + it("works on a bare search string", () => { + expect(sanitizeUrlForFacts("?token=abc&q=1")).toBe( + `?token=${REDACTED_FACT_VALUE}&q=1`, + ); + expect(sanitizeUrlForFacts("")).toBe(""); + }); + + it("returns a URL with nothing to mask unchanged", () => { + const url = "https://api.example.com/search?q=a%20b&limit=10&keyword=k"; + expect(sanitizeUrlForFacts(url)).toBe(url); + }); + }); + + describe("sanitizeHeadersForFacts", () => { + it("drops sensitive headers from a record and stringifies the rest", () => { + expect( + sanitizeHeadersForFacts({ + "bt-api-key": "k", + "api-key": "k", + Authorization: "Bearer t", + "content-type": "application/json", + "content-length": 42, + "x-empty": undefined, + }), + ).toEqual({ "content-type": "application/json", "content-length": "42" }); + }); + + it("accepts a Headers entries iterable", () => { + const headers = new Map([ + ["set-cookie", "sid=1"], + ["accept", "*/*"], + ]); + expect(sanitizeHeadersForFacts(headers.entries())).toEqual({ + accept: "*/*", + }); + }); + + it("applies an extra exclusion on top of the shared list", () => { + expect( + sanitizeHeadersForFacts( + { "x-idempotency-key-id": "1", accept: "*/*" }, + (name) => name.includes("key"), + ), + ).toEqual({ accept: "*/*" }); + }); + + it("returns an empty record for no headers", () => { + expect(sanitizeHeadersForFacts(undefined)).toEqual({}); + }); + }); +}); diff --git a/packages/core/src/utils/facts-sanitization.ts b/packages/core/src/utils/facts-sanitization.ts new file mode 100644 index 000000000..75211fd93 --- /dev/null +++ b/packages/core/src/utils/facts-sanitization.ts @@ -0,0 +1,139 @@ +/** + * Header and URL sanitization for the request and response facts the SDK + * reports with a transaction. + * + * Facts describe a call (method, host, path, status, content type). They + * should never carry the credentials the call was made with. The HTTP + * integrations (`@sapiom/fetch`, `@sapiom/axios`, `@sapiom/node-http`) share + * this one list, so they leave out the same headers and mask the same URL + * parts. The Sapiom API masks the same names again when it stores facts. + */ + +/** The value a masked URL query parameter or userinfo is replaced with. */ +export const REDACTED_FACT_VALUE = "[REDACTED]"; + +/** Header names that always carry a credential. */ +const SENSITIVE_HEADER_EXACT = new Set([ + "authorization", + "proxy-authorization", + "cookie", + "set-cookie", + "payment-signature", + "x-payment", + "x-payment-response", + "sapiom-identity", +]); + +/** A name that names a credential: `x-api-key`, `bt-api-key`, `client_secret`, … */ +const CREDENTIAL_NAME = + /api[-_]?key|apikey|token|secret|password|passwd|credential|signature|private[-_]?key/i; + +/** Header names only: anything with `auth` in it, and any `*-key` / `*_key`. */ +const SENSITIVE_HEADER_EXTRA = /auth|(?:^|[-_])key$/i; + +/** Every `bt-*` header, and `x-sapiom-*` headers that carry a key, token or signature. */ +const SENSITIVE_HEADER_PREFIX = + /^bt-|^x-sapiom-.*(?:key|token|secret|signature|auth)/i; + +/** Query parameters whose value is a credential (matched case-insensitively). */ +const SENSITIVE_QUERY_EXACT = new Set(["key", "sig", "auth", "pwd", "pass"]); + +// Greedy up to the last `@` before the path, query or fragment: a password may itself contain `@`. +const USERINFO = /^([a-z][a-z0-9+.-]*:\/\/)([^/?#\s]*)@/i; + +/** + * Whether a header must be left out of facts: `Authorization`, cookies, + * API-key, token, secret and password headers (`x-api-key`, `bt-api-key`, + * `api-key`, `x-auth-token`, …), payment signatures, the `Sapiom-Identity` + * token, and `x-sapiom-*` credential headers. Case-insensitive. + */ +export function isSensitiveHeaderName(name: string): boolean { + const lower = name.trim().toLowerCase(); + return ( + SENSITIVE_HEADER_EXACT.has(lower) || + CREDENTIAL_NAME.test(lower) || + SENSITIVE_HEADER_EXTRA.test(lower) || + SENSITIVE_HEADER_PREFIX.test(lower) + ); +} + +/** + * Whether a URL query parameter carries a credential: `key`, `api_key`, + * `apikey`, `token`, `access_token`, `secret`, `signature`, `sig`, + * `password`, `X-Amz-Signature`, … Case-insensitive, on the decoded name. + */ +export function isSensitiveQueryParam(name: string): boolean { + let decoded = name; + try { + decoded = decodeURIComponent(name.replace(/\+/g, " ")); + } catch { + // A malformed escape: match on the raw name. + } + const lower = decoded.trim().toLowerCase(); + return ( + SENSITIVE_QUERY_EXACT.has(lower) || + CREDENTIAL_NAME.test(lower) || + /(?:^|[-_])sig$/.test(lower) + ); +} + +/** + * The headers to report in facts: every header except the sensitive ones, + * values as strings (`String(value)`). Accepts a `Headers`-style entries + * iterable or a plain record. An `undefined` or `null` value is skipped. + * + * @param exclude An extra test for headers to leave out, on top of + * {@link isSensitiveHeaderName}. + */ +export function sanitizeHeadersForFacts( + headers: + | Iterable<[string, unknown]> + | Record + | null + | undefined, + exclude?: (lowerName: string) => boolean, +): Record { + const sanitized: Record = {}; + if (!headers) return sanitized; + const entries: Iterable<[string, unknown]> = + Symbol.iterator in Object(headers) && + typeof (headers as Iterable)[Symbol.iterator] === "function" + ? (headers as Iterable<[string, unknown]>) + : Object.entries(headers as Record); + for (const [key, value] of entries) { + if (value === undefined || value === null) continue; + if (isSensitiveHeaderName(key)) continue; + if (exclude?.(key.toLowerCase())) continue; + sanitized[key] = String(value); + } + return sanitized; +} + +/** + * `url` with credential query-parameter values and `user:pass@` userinfo + * replaced by `[REDACTED]`. Works on an absolute or relative URL or a bare + * query string (`?a=1`, as in `URL.search`); everything else is kept exactly + * as it was. + */ +export function sanitizeUrlForFacts(url: string): string { + const withoutUserinfo = url.replace( + USERINFO, + (_match, scheme: string) => `${scheme}${REDACTED_FACT_VALUE}@`, + ); + const queryStart = withoutUserinfo.indexOf("?"); + if (queryStart === -1) return withoutUserinfo; + const hashStart = withoutUserinfo.indexOf("#", queryStart); + const queryEnd = hashStart === -1 ? withoutUserinfo.length : hashStart; + const query = withoutUserinfo + .slice(queryStart + 1, queryEnd) + .split("&") + .map((pair) => { + const eq = pair.indexOf("="); + if (eq === -1 || eq === pair.length - 1) return pair; + return isSensitiveQueryParam(pair.slice(0, eq)) + ? `${pair.slice(0, eq + 1)}${REDACTED_FACT_VALUE}` + : pair; + }) + .join("&"); + return `${withoutUserinfo.slice(0, queryStart + 1)}${query}${withoutUserinfo.slice(queryEnd)}`; +} diff --git a/packages/fetch/src/http-client.integration.test.ts b/packages/fetch/src/http-client.integration.test.ts index c91abb52c..38e725c3f 100644 --- a/packages/fetch/src/http-client.integration.test.ts +++ b/packages/fetch/src/http-client.integration.test.ts @@ -809,3 +809,67 @@ describe("HTTP Client Integration Tests", () => { }); }); }); + +describe("Facts sanitization", () => { + let mocks: jest.Mocked; + let mockSapiomClient: SapiomClient; + + beforeAll(() => { + fetchMock.mockGlobal(); + }); + + beforeEach(() => { + const setup = createMockSapiomClient(); + mockSapiomClient = setup.client; + mocks = setup.mocks; + fetchMock.removeRoutes(); + fetchMock.clearHistory(); + }); + + afterAll(() => { + fetchMock.unmockGlobal(); + }); + + it("leaves credential headers out of facts and masks credential query values, without changing the request", async () => { + mocks.create.mockResolvedValue({ + id: "tx-facts", + status: TransactionStatus.AUTHORIZED, + } as any); + mocks.complete.mockResolvedValue({ + transaction: { id: "tx-facts", status: "completed" }, + } as any); + fetchMock.get("begin:https://api.example.com/geocode", { + status: 200, + body: { ok: true }, + headers: { "set-cookie": "sid=FAKE", "x-request-id": "r-1" }, + }); + + const fetch = createFetch({ sapiomClient: mockSapiomClient }); + await fetch("https://api.example.com/geocode?key=FAKEKEY&address=Paris", { + headers: { + "bt-api-key": "FAKEBT", + "api-key": "FAKEAPI", + accept: "application/json", + }, + }); + await flushPromises(); + + const request = mocks.create.mock.calls[0][0].requestFacts!.request as any; + expect(request.url).toBe( + "https://api.example.com/geocode?key=[REDACTED]&address=Paris", + ); + expect(request.urlParsed.search).toBe("?key=[REDACTED]&address=Paris"); + expect(request.headers).toEqual({ accept: "application/json" }); + const response = (mocks.complete.mock.calls[0][1].responseFacts as any) + .facts; + expect(response.headers["set-cookie"]).toBeUndefined(); + expect(response.headers["x-request-id"]).toBe("r-1"); + + // The request itself still carries the real URL and headers. + const sent = fetchMock.callHistory.lastCall()!; + expect(sent.url).toBe( + "https://api.example.com/geocode?key=FAKEKEY&address=Paris", + ); + expect(JSON.stringify(sent.options.headers)).toContain("FAKEBT"); + }); +}); diff --git a/packages/fetch/src/interceptors.ts b/packages/fetch/src/interceptors.ts index 1d8b352cd..f9a5b39a4 100644 --- a/packages/fetch/src/interceptors.ts +++ b/packages/fetch/src/interceptors.ts @@ -8,6 +8,8 @@ import { HttpClientRequestFacts, HttpClientResponseFacts, HttpClientErrorFacts, + sanitizeHeadersForFacts, + sanitizeUrlForFacts, } from "@sapiom/core"; import type { FailureMode, TransactionPollingConfig } from "@sapiom/core"; @@ -191,27 +193,15 @@ export async function handleAuthorization( protocol: parsedUrl.protocol.replace(":", ""), hostname: parsedUrl.hostname, pathname: parsedUrl.pathname, - search: parsedUrl.search, + search: sanitizeUrlForFacts(parsedUrl.search), port: parsedUrl.port ? parseInt(parsedUrl.port) : null, }; - const sanitizedHeaders: Record = {}; - const sensitiveHeaders = new Set([ - "authorization", - "cookie", - "x-api-key", - "x-auth-token", - ]); - - for (const [key, value] of request.headers.entries()) { - if (!sensitiveHeaders.has(key.toLowerCase())) { - sanitizedHeaders[key] = value; - } - } + const sanitizedHeaders = sanitizeHeadersForFacts(request.headers.entries()); const requestFacts: HttpClientRequestFacts = { method, - url, + url: sanitizeUrlForFacts(url), urlParsed, headers: sanitizedHeaders, hasBody: request.body !== null, @@ -388,12 +378,12 @@ export async function handlePayment( }), request: { method: request.method.toUpperCase(), - url: request.url, + url: sanitizeUrlForFacts(request.url), urlParsed: { protocol: parsedUrl.protocol.replace(":", ""), hostname: parsedUrl.hostname, pathname: parsedUrl.pathname, - search: parsedUrl.search, + search: sanitizeUrlForFacts(parsedUrl.search), port: parsedUrl.port ? parseInt(parsedUrl.port) : null, }, headers: {}, @@ -537,19 +527,9 @@ export function handleCompletion( const durationMs = Date.now() - startTime; const isSuccess = response !== null && response.ok; - const sanitizedHeaders: Record = {}; - if (response) { - const sensitiveHeaders = new Set([ - "set-cookie", - "authorization", - "x-api-key", - ]); - for (const [key, value] of response.headers.entries()) { - if (!sensitiveHeaders.has(key.toLowerCase())) { - sanitizedHeaders[key] = value; - } - } - } + const sanitizedHeaders = response + ? sanitizeHeadersForFacts(response.headers.entries()) + : {}; let responseFacts: | { source: string; version: string; facts: Record } diff --git a/packages/node-http/src/http-client.integration.test.ts b/packages/node-http/src/http-client.integration.test.ts index 36c60a812..de0125cf4 100644 --- a/packages/node-http/src/http-client.integration.test.ts +++ b/packages/node-http/src/http-client.integration.test.ts @@ -640,3 +640,55 @@ describe("Node-HTTP Client Integration Tests", () => { }); }); }); + +describe("Facts sanitization", () => { + afterEach(() => { + nock.cleanAll(); + }); + + it("leaves credential headers out of facts and masks credential query values, without changing the request", async () => { + const { client: sapiomClient, mocks } = createMockSapiomClient(); + mocks.create.mockResolvedValue({ + id: "tx-facts", + status: TransactionStatus.AUTHORIZED, + } as any); + mocks.complete.mockResolvedValue({ + transaction: { id: "tx-facts", status: "completed" }, + } as any); + const scope = nock("https://api.example.com", { + reqheaders: { "bt-api-key": "FAKEBT" }, + }) + .get("/geocode") + .query({ key: "FAKEKEY", address: "Paris" }) + .reply( + 200, + { ok: true }, + { "set-cookie": "sid=FAKE", "x-request-id": "r-1" }, + ); + + const client = createClient({ sapiomClient }); + await client.request({ + method: "GET", + url: "https://api.example.com/geocode?key=FAKEKEY&address=Paris", + headers: { + "bt-api-key": "FAKEBT", + "api-key": "FAKEAPI", + accept: "application/json", + }, + }); + await flushPromises(); + + // nock matched the real key and header: the request itself is unchanged. + expect(scope.isDone()).toBe(true); + const request = mocks.create.mock.calls[0][0].requestFacts!.request as any; + expect(request.url).toBe( + "https://api.example.com/geocode?key=[REDACTED]&address=Paris", + ); + expect(request.urlParsed.search).toBe("?key=[REDACTED]&address=Paris"); + expect(request.headers).toEqual({ accept: "application/json" }); + const response = (mocks.complete.mock.calls[0][1].responseFacts as any) + .facts; + expect(response.headers["set-cookie"]).toBeUndefined(); + expect(response.headers["x-request-id"]).toBe("r-1"); + }); +}); diff --git a/packages/node-http/src/interceptors.ts b/packages/node-http/src/interceptors.ts index 3cf168e9d..cf924b248 100644 --- a/packages/node-http/src/interceptors.ts +++ b/packages/node-http/src/interceptors.ts @@ -12,6 +12,8 @@ import { HttpClientResponseFacts, HttpClientErrorFacts, FailureMode, + sanitizeHeadersForFacts, + sanitizeUrlForFacts, } from "@sapiom/core"; import type { TransactionPollingConfig } from "@sapiom/core"; @@ -196,27 +198,17 @@ export async function handleAuthorization( protocol: parsedUrl.protocol.replace(":", ""), hostname: parsedUrl.hostname, pathname: parsedUrl.pathname, - search: parsedUrl.search, + search: sanitizeUrlForFacts(parsedUrl.search), port: parsedUrl.port ? parseInt(parsedUrl.port) : null, }; - const sanitizedHeaders: Record = {}; - const sensitiveHeaders = new Set([ - "authorization", - "cookie", - "x-api-key", - "x-auth-token", - ]); - - for (const [key, value] of Object.entries(request.headers)) { - if (!sensitiveHeaders.has(key.toLowerCase())) { - sanitizedHeaders[key] = value; - } - } + const sanitizedHeaders = sanitizeHeadersForFacts( + Object.entries(request.headers), + ); const requestFacts: HttpClientRequestFacts = { method, - url, + url: sanitizeUrlForFacts(url), urlParsed, headers: sanitizedHeaders, hasBody: !!request.body, @@ -459,19 +451,9 @@ export function handleCompletion( const isSuccess = response !== null && response.status >= 200 && response.status < 300; - const sanitizedHeaders: Record = {}; - if (response?.headers) { - const sensitiveHeaders = new Set([ - "set-cookie", - "authorization", - "x-api-key", - ]); - Object.entries(response.headers).forEach(([key, value]) => { - if (!sensitiveHeaders.has(key.toLowerCase())) { - sanitizedHeaders[key] = String(value); - } - }); - } + const sanitizedHeaders = sanitizeHeadersForFacts( + response?.headers ? Object.entries(response.headers) : undefined, + ); let responseFacts: | { source: string; version: string; facts: Record }