#16708: fix(security): OC-17 add token redaction to error formatting, deprecate plaintext botToken
channel: telegram
size: XS
trusted-contributor
Cluster:
Config Redaction Improvements
## Summary
- Wrap `formatErrorMessage()` and `formatUncaughtError()` with `redactSensitiveText()` to prevent credential leakage in all error paths
- Deprecate plaintext `botToken` config field with `@deprecated` JSDoc guiding users to `tokenFile`
- Enhanced `tokenFile` documentation with secret manager examples
## Security Impact
**OC-17 HIGH (CWE-522, Insufficiently Protected Credentials)** — Attack vectors:
1. Telegram bot tokens stored plaintext in `~/.openclaw/openclaw.json` readable via `/tools/invoke exec`
2. Error messages containing tokens logged without redaction via `formatErrorMessage()`
3. Stack traces from `formatUncaughtError()` may expose tokens
## Changes
| File | Change |
|------|--------|
| `src/infra/errors.ts` | Import `redactSensitiveText`, wrap both `formatErrorMessage()` and `formatUncaughtError()` returns with redaction |
| `src/config/types.telegram.ts` | Add `@deprecated` JSDoc to `botToken` field, enhance `tokenFile` docs with secret manager examples |
## Defense-in-Depth Approach
- Existing pattern `\d{6,}:[A-Za-z0-9_-]{20,}` already matches Telegram bot tokens
- This fix applies redaction **before** error messages reach any logging output (25+ call sites protected)
- No breaking changes — `botToken` still works but IDEs show deprecation warning
## Test plan
- [x] Full test suite: 5,641 tests passed, 0 OC-17-related failures
- [x] Redaction pattern tests: 9 passing (covers Telegram token format)
- [x] Config validation tests: 53 passing
- [x] Telegram bot tests: 426 passing
- [x] TypeScript compilation: no new errors
- [x] Backward compatibility: existing configs unaffected
---
*Created by [Aether AI Agent](https://tryaether.ai) — AI security research and remediation agent.*
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR adds defense-in-depth token redaction to the two central error formatting functions (`formatErrorMessage` and `formatUncaughtError` in `src/infra/errors.ts`), ensuring Telegram bot tokens and other credential patterns are masked before error messages reach any logging output. It also adds a `@deprecated` JSDoc annotation to the plaintext `botToken` config field, guiding users toward the more secure `tokenFile` alternative.
- `formatErrorMessage` and `formatUncaughtError` now call `redactSensitiveText()` on their return values, protecting ~25 call sites across the codebase from credential leakage
- The redaction uses the existing pattern set in `src/logging/redact.ts` (including `\d{6,}:[A-Za-z0-9_-]{20,}` which matches Telegram bot token format) — no new patterns were needed
- Error classification logic in retry policies and network error detection remains unaffected, as the redacted patterns target token-shaped strings, not keyword strings like "429" or "timeout"
- One existing call site in `src/telegram/send.ts:86` now double-redacts (outer `redactSensitiveText` wrapping the already-redacted `formatUncaughtError` output) — harmless but wasteful
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge — it applies a well-scoped security improvement with no breaking changes to existing behavior.
- The changes are minimal and focused: two functions get an additional redaction call, and one type field gets JSDoc updates. The redaction function already exists and is battle-tested. Error classification and retry logic remain unaffected because redaction targets token-shaped strings, not error keywords. The only concern is a minor double-redaction inefficiency at one call site, which is a style issue rather than a correctness issue.
- Pay attention to `src/infra/errors.ts` as it's a widely-used utility — the redaction overhead now applies to all ~25 call sites. The existing `src/telegram/send.ts:86` has a redundant double-redaction that could be cleaned up.
<sub>Last reviewed commit: ea342d7</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#11208: fix(config): prevent __OPENCLAW_REDACTED__ corruption on config writes
by janckerchen · 2026-02-07
80.6%
#16928: fix(security): OC-07 redact session history credentials and enforce...
by aether-ai-agent · 2026-02-15
79.2%
#23654: security(cli): redact sensitive values in config get output
by SleuthCo · 2026-02-22
78.9%
#12792: fix: exclude 'tokens' (plural) fields from config redaction
by jpaine · 2026-02-09
78.8%
#11347: fix: scope Telegram update offset to bot token
by anooprdawar · 2026-02-07
78.1%
#22231: fix(security): redact sensitive data in session transcripts
by novalis133 · 2026-02-20
77.4%
#23175: feat(security): runtime safety — transcript retention, tool call bu...
by ihsanmokhlisse · 2026-02-22
77.4%
#23174: feat(security): credential leak prevention — exfiltration patterns,...
by ihsanmokhlisse · 2026-02-22
76.5%
#7611: fix: migrate channels.telegram.token to botToken on config load
by luiginotmario · 2026-02-03
76.4%
#16321: Fix #12767: suppress HEARTBEAT_OK leakage in Telegram DM replies
by tdjackey · 2026-02-14
76.3%