Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

redact url logs

Thiago Araujo da Silva67752cc4e955
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 7091c25add..6ad6a38f95 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -337,7 +337,7 @@ logger.info(`getPlan: Process done for [orgId=${orgId}] [projectId=${projectId}]
logger.error({ sessionId, err }, "Failed to get connection details");
```
-**Never log an outbound URL verbatim — a URL is often itself a credential.** Incoming-webhook providers put the bearer secret in the path (`https://hooks.slack.com/services/T…/B…/<secret>`, Discord, Teams, Telegram) and many APIs accept a token as a query param, so a raw URL in a log line ships a working credential to the log sink. Pass it through `sanitizeUrlForLog` from `@app/lib/logger` first (`src/lib/logger/sanitize-url.ts`): it strips userinfo and the fragment, redacts every query value, drops the path for known credential-in-URL hosts, and redacts token-shaped path segments on hosts we can't enumerate (users can point a webhook channel at any host). The global axios response interceptor (`src/lib/config/request.ts`) and `safeRequest`'s dispatch log already do this — when adding a new provider whose path is a secret, add its host to `CREDENTIAL_PATH_HOSTS`.
+**Never log an outbound URL verbatim — a URL is often itself a credential.** Incoming-webhook providers put the bearer secret in the path (`https://hooks.slack.com/services/T…/B…/<secret>`, Discord, Teams, Telegram) and many APIs accept a token as a query param, so a raw URL in a log line ships a working credential to the log sink. Pass it through `sanitizeUrlForLog` from `@app/lib/logger` first (`src/lib/logger/sanitize-url.ts`): it keeps only the origin, strips userinfo and the fragment, redacts the entire path, and redacts every query value. Token formats can't be recognised reliably, so the path is redacted by default for every host rather than sniffed with heuristics. The global axios response interceptor (`src/lib/config/request.ts`) and `safeRequest`'s dispatch log already do this.
Note that `logger.ts` also has a `redactedKeys` list applied to structured-object fields up to depth three. It only matches by key name, so it does **not** help with a secret embedded in a `url` field.
diff --git a/backend/src/lib/logger/sanitize-url.test.ts b/backend/src/lib/logger/sanitize-url.test.ts
index 4cc792b5c2..596845d85e 100644
--- a/backend/src/lib/logger/sanitize-url.test.ts
+++ b/backend/src/lib/logger/sanitize-url.test.ts
@@ -1,13 +1,8 @@
import { sanitizeUrlForLog } from "./sanitize-url";
describe("sanitizeUrlForLog", () => {
- test("drops the path of an incoming-webhook provider", () => {
- expect(sanitizeUrlForLog("https://hooks.slack.com/services/T0000000/B0000000/aBcDeFgHiJkLmNoPqRsTuVwX")).toBe(
- "https://hooks.slack.com/[REDACTED]"
- );
- });
-
test.each([
+ "https://hooks.slack.com/services/T0000000/B0000000/aBcDeFgHiJkLmNoPqRsTuVwX",
"https://discord.com/api/webhooks/1234567890/tokentokentokentoken",
"https://outlook.webhook.office.com/webhookb2/abc-def@ghi/IncomingWebhook/jkl/mno",
"https://api.telegram.org/bot123456:AAHrandomsecret/sendMessage"
@@ -16,40 +11,40 @@ describe("sanitizeUrlForLog", () => {
expect(sanitized).toMatch(/\/\[REDACTED\]$/);
});
- test("keeps query param names but redacts their values", () => {
- expect(sanitizeUrlForLog("https://api.example.com/v1/items?page=2&token=supersecret")).toBe(
- "https://api.example.com/v1/items?page=[REDACTED]&token=[REDACTED]"
- );
+ test("drops the path on unknown hosts even when it looks harmless", () => {
+ expect(sanitizeUrlForLog("https://api.example.com/v1/secrets/my-app")).toBe("https://api.example.com/[REDACTED]");
+ expect(sanitizeUrlForLog("https://example.com/hook/abcdefghijklmnopqrstuv")).toBe("https://example.com/[REDACTED]");
});
- test("keeps readable paths on unknown hosts", () => {
- expect(sanitizeUrlForLog("https://api.example.com/v1/secrets/my-app")).toBe(
- "https://api.example.com/v1/secrets/my-app"
+ test("keeps query param names but redacts their values", () => {
+ expect(sanitizeUrlForLog("https://api.example.com/v1/items?page=2&token=supersecret")).toBe(
+ "https://api.example.com/[REDACTED]?page=[REDACTED]&token=[REDACTED]"
);
});
- test("redacts token-shaped path segments on unknown hosts", () => {
- expect(sanitizeUrlForLog("https://webhooks.acme.internal/hook/9f8a7b6c5d4e3f2a1b0c9d8e7f6a5b4c")).toBe(
- "https://webhooks.acme.internal/hook/[REDACTED]"
- );
+ test("keeps a root path as is", () => {
+ expect(sanitizeUrlForLog("https://api.example.com")).toBe("https://api.example.com/");
+ expect(sanitizeUrlForLog("https://api.example.com/")).toBe("https://api.example.com/");
});
test("strips embedded credentials and the fragment", () => {
- expect(sanitizeUrlForLog("https://user:[email protected]/path#frag")).toBe("https://api.example.com/path");
+ expect(sanitizeUrlForLog("https://user:[email protected]/path#frag")).toBe("https://api.example.com/[REDACTED]");
});
test("resolves a relative url against the axios baseURL", () => {
- expect(sanitizeUrlForLog("/v1/items", "https://api.example.com/base")).toBe(
- "https://api.example.com/base/v1/items"
- );
+ expect(sanitizeUrlForLog("/v1/items", "https://api.example.com/base")).toBe("https://api.example.com/[REDACTED]");
});
- test("keeps an absolute url even when a baseURL is set", () => {
+ test("keeps the origin of an absolute url even when a baseURL is set", () => {
expect(sanitizeUrlForLog("https://other.example.com/x", "https://api.example.com")).toBe(
- "https://other.example.com/x"
+ "https://other.example.com/[REDACTED]"
);
});
+ test("redacts the path of a relative url with no base", () => {
+ expect(sanitizeUrlForLog("/v1/items")).toBe("/[REDACTED]");
+ });
+
test("returns a placeholder instead of echoing an unparseable url", () => {
expect(sanitizeUrlForLog("https://")).toBe("[REDACTED]");
});
diff --git a/backend/src/lib/logger/sanitize-url.ts b/backend/src/lib/logger/sanitize-url.ts
index 106111d6bb..e032d3d8bc 100644
--- a/backend/src/lib/logger/sanitize-url.ts
+++ b/backend/src/lib/logger/sanitize-url.ts
@@ -4,39 +4,10 @@ const PLACEHOLDER = "[REDACTED]";
const NO_URL = "NO_URL";
const RELATIVE_BASE = "http://relative.invalid";
-const CREDENTIAL_PATH_HOSTS = new Set([
- "hooks.slack.com",
- "discord.com",
- "discordapp.com",
- "chat.googleapis.com",
- "hooks.zapier.com",
- "api.telegram.org",
- "maker.ifttt.com"
-]);
-
-const CREDENTIAL_PATH_HOST_SUFFIXES = [".webhook.office.com", ".logic.azure.com", ".slack.com"];
-
const hasScheme = new RE2(/^[a-z][a-z\d+\-.]*:\/\//i);
const trailingSlashes = new RE2(/\/+$/);
const leadingSlashes = new RE2(/^\/+/);
-const MIN_TOKEN_LENGTH = 20;
-const tokenCharset = new RE2(/^[A-Za-z0-9._~=+-]+$/);
-const hasDigit = new RE2(/\d/);
-const hasLetter = new RE2(/[A-Za-z]/);
-
-const isCredentialPathHost = (hostname: string): boolean =>
- CREDENTIAL_PATH_HOSTS.has(hostname) || CREDENTIAL_PATH_HOST_SUFFIXES.some((suffix) => hostname.endsWith(suffix));
-
-const isTokenLikeSegment = (segment: string): boolean =>
- segment.length >= MIN_TOKEN_LENGTH && tokenCharset.test(segment) && hasDigit.test(segment) && hasLetter.test(segment);
-
-const redactPath = (pathname: string): string =>
- pathname
- .split("/")
- .map((segment) => (isTokenLikeSegment(segment) ? PLACEHOLDER : segment))
- .join("/");
-
const redactQuery = (searchParams: URLSearchParams): string => {
const keys = [...searchParams.keys()];
if (!keys.length) return "";
@@ -50,9 +21,11 @@ const resolveUrl = (rawUrl?: string, baseUrl?: string): string => {
};
/**
- * Renders a URL safe to log: strips userinfo and fragment, redacts every query
- * value, and drops path segments that are credentials. Never returns the raw
- * input, so an unparseable URL is redacted rather than echoed.
+ * Renders a URL safe to log: keeps only the origin, strips userinfo and the
+ * fragment, redacts the whole path, and redacts every query value. Credentials
+ * in a URL path cannot be recognised reliably, so the path is always dropped
+ * rather than inspected. Never returns the raw input, so an unparseable URL is
+ * redacted rather than echoed.
*/
export const sanitizeUrlForLog = (rawUrl?: string, baseUrl?: string): string => {
const target = resolveUrl(rawUrl, baseUrl);
@@ -67,8 +40,7 @@ export const sanitizeUrlForLog = (rawUrl?: string, baseUrl?: string): string =>
return PLACEHOLDER;
}
- const hostname = url.hostname.toLowerCase();
- const path = isAbsolute && isCredentialPathHost(hostname) ? `/${PLACEHOLDER}` : redactPath(url.pathname);
+ const path = url.pathname === "/" ? url.pathname : `/${PLACEHOLDER}`;
const origin = isAbsolute ? `${url.protocol}//${url.host}` : "";
return `${origin}${path}${redactQuery(url.searchParams)}`;