Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🦫 fix: Bound Error Text Before URL Redaction (#16532)

lia-by-librechat[bot]f4f36774e17b
diff --git a/packages/api/src/utils/errors.spec.ts b/packages/api/src/utils/errors.spec.ts
index 181eabf73f..960786a16c 100644
--- a/packages/api/src/utils/errors.spec.ts
+++ b/packages/api/src/utils/errors.spec.ts
@@ -82,11 +82,11 @@ describe('getSafeErrorText', () => {
expect(text).toContain('at send (/app/api/s3.js:1:1)');
});
- it('keeps file:// stack frames readable', () => {
+ it.each(['file', 'FILE'])('keeps %s:// stack frames readable', (scheme) => {
const error = new Error('boom');
- error.stack = 'Error: boom\n at run (file:///app/api/server.js:10:5)';
+ error.stack = `Error: boom\n at run (${scheme}:///app/api/server.js:10:5)`;
- expect(getSafeErrorText(error)).toContain('file:///app/api/server.js:10:5');
+ expect(getSafeErrorText(error)).toContain(`${scheme}:///app/api/server.js:10:5`);
});
it('redacts bearer credentials echoed into a message', () => {
@@ -128,6 +128,106 @@ describe('getSafeErrorText', () => {
it('bounds the text a very long error can write into the log', () => {
expect(getSafeErrorText(new Error('x'.repeat(9000))).length).toBeLessThanOrEqual(2000);
});
+
+ it.each([
+ { kind: 'string', createError: (text: string) => text, prefix: '' },
+ { kind: 'stack', createError: (text: string) => ({ stack: text }), prefix: '' },
+ {
+ kind: 'message',
+ createError: (text: string) => ({ name: 'RestError', message: text }),
+ prefix: 'RestError: ',
+ },
+ {
+ kind: 'name',
+ createError: (text: string) => ({ name: text, message: 'outside the input limit' }),
+ prefix: '',
+ },
+ ])('truncates oversized $kind input before URL redaction', ({ createError, prefix }) => {
+ const text = `https://minio.example.com/${'x'.repeat(3 * 1024 * 1024)} after the input limit`;
+
+ expect(getSafeErrorText(createError(text))).toBe(
+ `${prefix}https://minio.example.com/[redacted]`,
+ );
+ });
+
+ it('bounds repeated scheme-like prefixes before matching URLs', () => {
+ const text = 'model-'.repeat(512 * 1024);
+
+ expect(getSafeErrorText(text)).toBe(text.slice(0, 2000));
+ });
+
+ it.each([1, 32])('redacts a URL with a %i-character scheme', (length) => {
+ const scheme = 'a'.repeat(length);
+
+ expect(getSafeErrorText(`${scheme}://example.com/private?signature=secret`)).toBe(
+ `${scheme}://example.com/[redacted]`,
+ );
+ });
+
+ it.each([33, 64, 1900])('redacts the entire URL tail for a %i-character scheme', (length) => {
+ const scheme = 'a'.repeat(length);
+ const input = `${scheme}://user:[email protected]/private?signature=secret`;
+
+ expect(getSafeErrorText(input)).toBe(`${scheme}[url]`);
+ });
+
+ it.each(['1+.-', '+file', '.FILE', '-file'])(
+ 'does not mistake an overlength scheme suffix (%s) for a separate URL',
+ (suffix) => {
+ const scheme = `${'a'.repeat(33)}${suffix}`;
+ const input = `${scheme}://example.com/private?signature=secret`;
+
+ expect(getSafeErrorText(input)).toBe(`${scheme}[url]`);
+ },
+ );
+
+ it.each([
+ { kind: 'stack', createError: (text: string) => ({ stack: text }), prefix: '' },
+ { kind: 'name', createError: (text: string) => ({ name: text }), prefix: '' },
+ {
+ kind: 'message',
+ createError: (text: string) => ({ message: text }),
+ prefix: 'UnknownError: ',
+ },
+ ])('redacts overlength URL schemes in $kind input', ({ createError, prefix }) => {
+ const scheme = 'a'.repeat(33);
+ const input = `${scheme}://example.com/private?signature=secret`;
+
+ expect(getSafeErrorText(createError(input))).toBe(`${prefix}${scheme}[url]`);
+ });
+
+ it('redacts an overlength URL scheme that crosses the input limit', () => {
+ const prefix = `${'x'.repeat(1940)} `;
+ const scheme = 'a'.repeat(33);
+
+ expect(getSafeErrorText(`${prefix}${scheme}://example.com/private?signature=secret`)).toBe(
+ `${prefix}${scheme}[url]`,
+ );
+ });
+
+ it('redacts a signed URL that crosses the input limit', () => {
+ const prefix = `${'x'.repeat(1950)} `;
+ const text = getSafeErrorText(`${prefix}${signedUrl}`);
+
+ expect(text).toBe(`${prefix}https://minio.example.com/[redacted]`);
+ expect(text).not.toContain('user123');
+ });
+
+ it.each(['Bearer', 'Basic'])('redacts a truncated %s credential', (scheme) => {
+ const prefix = `${'x'.repeat(1980)} `;
+ const text = getSafeErrorText(`${prefix}${scheme} private-credential-crossing-the-limit`);
+
+ expect(text).toBe(`${prefix}${scheme} [redacted]`);
+ expect(text).not.toContain('private');
+ });
+
+ it('keeps the output bounded when redaction expands the truncated input', () => {
+ const text = getSafeErrorText('Bearer x '.repeat(1000));
+
+ expect(text.length).toBe(2000);
+ expect(text).toContain('Bearer [redacted]');
+ expect(text).not.toContain('Bearer x');
+ });
});
describe('isAbortError', () => {
diff --git a/packages/api/src/utils/errors.ts b/packages/api/src/utils/errors.ts
index e8dcc95c72..cf77c43769 100644
--- a/packages/api/src/utils/errors.ts
+++ b/packages/api/src/utils/errors.ts
@@ -41,7 +41,14 @@ export function getSafeErrorMetadata(error: unknown): SafeErrorMetadata {
const MAX_SAFE_ERROR_TEXT = 2000;
-function redactUrl(match: string): string {
+function redactUrl(match: string, scheme?: string): string {
+ if (scheme == null) {
+ return '[url]';
+ }
+ if (scheme.toLowerCase() === 'file') {
+ return match;
+ }
+
try {
const url = new URL(match);
return `${url.protocol}//${url.host}/[redacted]`;
@@ -52,15 +59,18 @@ function redactUrl(match: string): string {
function redactSecrets(value: string): string {
return value
- .replace(/\b(?!file:)[a-z][a-z0-9+.-]*:\/\/\S+/gi, redactUrl)
- .replace(/\b(bearer|basic)\s+\S+/gi, '$1 [redacted]');
+ .slice(0, MAX_SAFE_ERROR_TEXT)
+ .replace(/\b(?<![a-z0-9+.-])([a-z][a-z0-9+.-]{0,31}):\/\/\S+|:\/\/\S+/gi, redactUrl)
+ .replace(/\b(bearer|basic)\s+\S+/gi, '$1 [redacted]')
+ .slice(0, MAX_SAFE_ERROR_TEXT);
}
/**
* The request-boundary counterpart to {@link getSafeErrorMetadata}: the error's own
- * description and stack, with every URL reduced to its origin and bearer credentials
- * removed. A signed storage URL carries the object path and signature in the parts
- * that are dropped, while the origin is what an operator needs to place the failure.
+ * description and stack, with recognized URLs reduced to their origins, unrecognized
+ * URL tails removed, and bearer credentials removed. A signed storage URL carries its
+ * object path and signature in the parts that are dropped, while the origin is what
+ * an operator needs to place the failure.
*
* Returned as text because the caller must log it inside the message and pass no
* winston metadata: metadata arms `format.splat()` and promotes an SDK error's own
@@ -73,7 +83,7 @@ function redactSecrets(value: string): string {
*/
export function getSafeErrorText(error: unknown): string {
if (typeof error === 'string') {
- return redactSecrets(error).slice(0, MAX_SAFE_ERROR_TEXT);
+ return redactSecrets(error);
}
if (error == null || typeof error !== 'object') {
@@ -82,19 +92,23 @@ export function getSafeErrorText(error: unknown): string {
const stack = readProperty(error, 'stack');
if (typeof stack === 'string' && stack.length > 0) {
- return redactSecrets(stack).slice(0, MAX_SAFE_ERROR_TEXT);
+ return redactSecrets(stack);
}
const name = readProperty(error, 'name');
const message = readProperty(error, 'message');
const described = [
- typeof name === 'string' && name.length > 0 ? name : 'UnknownError',
- typeof message === 'string' && message.length > 0 ? message : undefined,
+ typeof name === 'string' && name.length > 0
+ ? name.slice(0, MAX_SAFE_ERROR_TEXT)
+ : 'UnknownError',
+ typeof message === 'string' && message.length > 0
+ ? message.slice(0, MAX_SAFE_ERROR_TEXT)
+ : undefined,
]
.filter(Boolean)
.join(': ');
- return redactSecrets(described).slice(0, MAX_SAFE_ERROR_TEXT);
+ return redactSecrets(described);
}
/**