LibreChat/packages/api/src/utils/openid.ts
Danny Avila 67e2107d74
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
🔐 fix: Keep OpenID Metadata Out of Logs and Traces (#15470)
* 🔒 fix: Keep Secrets Out of Debug Logs and Spans

* 🧯 fix: Derive Safe Exception Types

* 🛡️ fix: Close Diagnostic Redaction Gaps

* 🧼 fix: Redact Escaped JSON Secrets
2026-09-01 13:22:12 -04:00

87 lines
2.8 KiB
TypeScript

const MASKED_VALUE = '***MASKED***';
const SAFE_OPENID_HEADER_VALUES = new Set(['content-length', 'content-type']);
function isSensitiveFieldName(key: string): boolean {
const normalized = key.toLowerCase().replace(/[^a-z0-9]/g, '');
return (
normalized === 'code' ||
normalized.includes('authorization') ||
normalized.includes('assertion') ||
normalized.includes('clientid') ||
normalized.includes('connectionstring') ||
normalized.includes('cookie') ||
normalized.includes('credential') ||
normalized.includes('password') ||
normalized.includes('signature') ||
normalized.includes('secret') ||
normalized.includes('token') ||
normalized.includes('verifier') ||
normalized.includes('apikey')
);
}
/**
* Helper function to safely log sensitive data when debug mode is enabled
* @param obj - Object to stringify
* @param maxLength - Maximum length of the stringified output
* @returns Stringified object with sensitive data masked
*/
export function safeStringify(obj: unknown, maxLength = 1000): string {
try {
const str = JSON.stringify(obj, (key, value) => {
if (isSensitiveFieldName(key)) {
return MASKED_VALUE;
}
return value;
});
const serialized = str ?? '[Unserializable value]';
if (serialized.length > maxLength) {
return `${serialized.substring(0, maxLength)}... (truncated)`;
}
return serialized;
} catch {
return '[Error stringifying object]';
}
}
/**
* Describes an OpenID request body without serializing any values. OAuth form
* bodies routinely contain authorization codes, refresh tokens, PKCE verifiers,
* and client secrets, so even partially masked values are unsafe to log.
*/
export function logOpenIdRequestBody(body: unknown): string {
try {
if (body instanceof URLSearchParams) {
return safeStringify({ type: 'URLSearchParams', fieldCount: body.size });
}
if (typeof body === 'string') {
return safeStringify({ type: 'string', length: body.length });
}
return safeStringify({ type: body == null ? typeof body : 'object' });
} catch {
return '[OpenID request body metadata unavailable]';
}
}
/**
* Helper to log headers without revealing sensitive information
* @param headers - Headers object to log
* @returns Stringified headers with sensitive data masked
*/
export function logHeaders(headers: Headers | undefined | null): string {
try {
const headerObj: Record<string, string> = {};
if (!headers || typeof headers.entries !== 'function') {
return 'No headers available';
}
for (const [key, value] of headers.entries()) {
headerObj[key] = SAFE_OPENID_HEADER_VALUES.has(key.toLowerCase()) ? value : MASKED_VALUE;
}
return safeStringify(headerObj);
} catch {
return '[OpenID request headers unavailable]';
}
}