mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 00:20:22 +00:00
fix(security): redact Telegram bot tokens in errors
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { redactSensitiveText } from "../logging/redact.js";
|
||||
|
||||
export function extractErrorCode(err: unknown): string | undefined {
|
||||
if (!err || typeof err !== "object") {
|
||||
return undefined;
|
||||
@@ -27,20 +29,22 @@ export function hasErrnoCode(err: unknown, code: string): boolean {
|
||||
}
|
||||
|
||||
export function formatErrorMessage(err: unknown): string {
|
||||
let formatted: string;
|
||||
if (err instanceof Error) {
|
||||
return err.message || err.name || "Error";
|
||||
}
|
||||
if (typeof err === "string") {
|
||||
return err;
|
||||
}
|
||||
if (typeof err === "number" || typeof err === "boolean" || typeof err === "bigint") {
|
||||
return String(err);
|
||||
}
|
||||
try {
|
||||
return JSON.stringify(err);
|
||||
} catch {
|
||||
return Object.prototype.toString.call(err);
|
||||
formatted = err.message || err.name || "Error";
|
||||
} else if (typeof err === "string") {
|
||||
formatted = err;
|
||||
} else if (typeof err === "number" || typeof err === "boolean" || typeof err === "bigint") {
|
||||
formatted = String(err);
|
||||
} else {
|
||||
try {
|
||||
formatted = JSON.stringify(err);
|
||||
} catch {
|
||||
formatted = Object.prototype.toString.call(err);
|
||||
}
|
||||
}
|
||||
// Security: best-effort token redaction before returning/logging.
|
||||
return redactSensitiveText(formatted);
|
||||
}
|
||||
|
||||
export function formatUncaughtError(err: unknown): string {
|
||||
@@ -48,7 +52,8 @@ export function formatUncaughtError(err: unknown): string {
|
||||
return formatErrorMessage(err);
|
||||
}
|
||||
if (err instanceof Error) {
|
||||
return err.stack ?? err.message ?? err.name;
|
||||
const stack = err.stack ?? err.message ?? err.name;
|
||||
return redactSensitiveText(stack);
|
||||
}
|
||||
return formatErrorMessage(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user