disclaw/src/runtime/sanitize.ts
Nick Tabeling 57cd3d4a43 feat(DIS-102): sanitizeForDiscord strips paths and tokens before Discord send
Add sanitizeForDiscord() in src/runtime/sanitize.ts.
Router applies sanitizer to all channel.send() calls.
Prevents path/token leaks into Discord channels.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-10 09:50:37 +02:00

43 lines
1.3 KiB
TypeScript

export interface SanitizeOptions {
repoRoot?: string; // e.g. /home/user/.disclaw
home?: string; // os.homedir()
}
/**
* Sanitizes agent output before it is sent to a Discord channel.
* Replaces absolute paths and sensitive tokens so they are never leaked.
*
* Replacement order is intentional — longest/most-specific patterns first:
* 1. repoRoot → <disclaw>
* 2. home → ~
* 3. Discord bot token pattern → <redacted>
* 4. Anthropic API key pattern → <redacted>
*/
export function sanitizeForDiscord(
text: string,
opts: SanitizeOptions = {}
): string {
let result = text;
// 1. Replace repo root before home so that sub-paths are caught by the
// more specific substitution first (repoRoot is usually inside home).
if (opts.repoRoot) {
result = result.split(opts.repoRoot).join("<disclaw>");
}
// 2. Replace home directory with ~
if (opts.home) {
result = result.split(opts.home).join("~");
}
// 3. Discord bot token — format: [MN]<23 base64url>.<6 base64url>.<27 base64url>
result = result.replace(
/[MN][A-Za-z\d]{23}\.[\w-]{6}\.[\w-]{27}/g,
"<redacted>"
);
// 4. Anthropic API key — format: sk-ant-<20+ base64url chars>
result = result.replace(/sk-ant-[A-Za-z0-9\-_]{20,}/g, "<redacted>");
return result;
}