Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/facts-credential-sanitization.md
Original file line number Diff line number Diff line change
@@ -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`).
73 changes: 73 additions & 0 deletions packages/axios/src/http-client.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
61 changes: 27 additions & 34 deletions packages/axios/src/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
HttpClientResponseFacts,
HttpClientErrorFacts,
FailureMode,
sanitizeHeadersForFacts,
sanitizeUrlForFacts,
} from "@sapiom/core";

import type { TransactionPollingConfig } from "@sapiom/core";
Expand Down Expand Up @@ -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<string, string> = {};
if (axiosConfig.headers) {
Object.entries(axiosConfig.headers as Record<string, any>).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<string, unknown>)
: 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,
Expand Down Expand Up @@ -789,21 +792,11 @@ export function addCompletionInterceptor(
const startTime = (axiosConfig as any).__sapiomStartTime || Date.now();
const durationMs = Date.now() - startTime;

const sanitizedHeaders: Record<string, string> = {};
const sensitiveHeaders = new Set([
"set-cookie",
"authorization",
"x-api-key",
]);
if (response.headers) {
Object.entries(response.headers as Record<string, any>).forEach(
([key, value]) => {
if (!sensitiveHeaders.has(key.toLowerCase())) {
sanitizedHeaders[key] = String(value);
}
},
);
}
const sanitizedHeaders = sanitizeHeadersForFacts(
response.headers
? Object.entries(response.headers as Record<string, unknown>)
: undefined,
);

const facts: HttpClientResponseFacts = {
status: response.status,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
export type { CallSiteInfo, RuntimeInfo } from "./types/telemetry.js";

// Types
Expand Down
152 changes: 152 additions & 0 deletions packages/core/src/utils/facts-sanitization.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>([
["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({});
});
});
});
Loading
Loading