LibreChat/packages/api/src/utils/errors.ts
Hao bd08a363c4
🔣 fix: Encode Images From Storage Keys (#15851)
* fix: Prefer storage keys for image encoding

* fix: Sanitize assistant image errors

* fix: Sanitize agent image storage errors

* fix: Move blob image encoding into API package

* fix: Complete image encoder TypeScript migration

* fix: Keep request-boundary error logs diagnosable

getSafeErrorMetadata reduces an error to { type: 'Error' } and an optional
status. That is the right bound where an error may echo submitted content, and
it stays in place at getS3FileStream, getAzureFileStream and
getFirebaseFileStream, where a signed URL reaches the log.

Applied at ResumableAgentController's initialization catch and the chatV1
generic branch it blinded the whole route: a missing key, an agent that fails to
load and a database error all logged as { type: 'Error' }, with no message,
name, code or stack.

getSafeErrorText keeps the error's own description and stack, reduces every URL
to its origin so an object path and signature cannot ride along, and strips
bearer credentials. It returns text so the two handlers log it inside the
message and pass no winston metadata, which is what stops an SDK error's own
enumerable properties (request.url, config, response.headers) from being
promoted onto the record.

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
2026-09-12 12:39:26 -04:00

135 lines
4.5 KiB
TypeScript

export interface SafeErrorMetadata {
readonly type: string;
readonly status?: number;
}
function readProperty(value: object, property: string): unknown {
try {
return (value as Record<string, unknown>)[property];
} catch {
return undefined;
}
}
/**
* Returns bounded diagnostic metadata without error messages, stacks, headers,
* request bodies, or provider response data that may echo submitted content.
*/
export function getSafeErrorMetadata(error: unknown): SafeErrorMetadata {
if (error == null || typeof error !== 'object') {
return { type: 'UnknownError' };
}
const directStatus = readProperty(error, 'status') ?? readProperty(error, 'statusCode');
const response = readProperty(error, 'response');
const responseStatus =
response != null && typeof response === 'object' ? readProperty(response, 'status') : undefined;
const statusCandidate = directStatus ?? responseStatus;
const status =
typeof statusCandidate === 'number' &&
Number.isInteger(statusCandidate) &&
statusCandidate >= 100 &&
statusCandidate <= 599
? statusCandidate
: undefined;
return {
type: error instanceof Error ? 'Error' : 'UnknownError',
...(status !== undefined && { status }),
};
}
const MAX_SAFE_ERROR_TEXT = 2000;
function redactUrl(match: string): string {
try {
const url = new URL(match);
return `${url.protocol}//${url.host}/[redacted]`;
} catch {
return '[url]';
}
}
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]');
}
/**
* 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.
*
* 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
* enumerable properties — `request.url`, `config`, `response.headers` — onto the
* record, which is the wider leak an error object causes at a log site.
*
* Use this where a handler catches every failure on a route and the class of error is
* not known ahead of time. Prefer `getSafeErrorMetadata` where the error may echo
* submitted content, such as a provider response body.
*/
export function getSafeErrorText(error: unknown): string {
if (typeof error === 'string') {
return redactSecrets(error).slice(0, MAX_SAFE_ERROR_TEXT);
}
if (error == null || typeof error !== 'object') {
return 'UnknownError';
}
const stack = readProperty(error, 'stack');
if (typeof stack === 'string' && stack.length > 0) {
return redactSecrets(stack).slice(0, MAX_SAFE_ERROR_TEXT);
}
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,
]
.filter(Boolean)
.join(': ');
return redactSecrets(described).slice(0, MAX_SAFE_ERROR_TEXT);
}
/**
* Whether a caught error is a cancellation rather than a genuine failure.
*
* An aborted signal on its own proves only that the run is over: a permission,
* OAuth, or upstream error can reject in the same tick a user presses Stop, and
* treating those as cancellations hides real faults from operational alerts.
* Callers that want to quiet a cancellation should require this as well as the
* signal state. Walks `cause` because intermediate layers rewrap the rejection.
*/
export function isAbortError(error: unknown): boolean {
const visited = new Set<unknown>();
let current: unknown = error;
while (current != null && typeof current === 'object' && !visited.has(current)) {
visited.add(current);
const name = readProperty(current, 'name');
const code = readProperty(current, 'code');
const messageValue = readProperty(current, 'message');
const message = typeof messageValue === 'string' ? messageValue : '';
if (
name === 'AbortError' ||
code === 'ABORT_ERR' ||
code === 'ERR_CANCELED' ||
message.includes('AbortError') ||
/(?:operation|request|stream) was aborted/i.test(message)
) {
return true;
}
current = readProperty(current, 'cause');
}
return false;
}