#22529: fix: strip newlines from pasted setup-tokens
commands
size: S
Cluster:
Model Reasoning Fixes
## Summary
- `claude setup-token` outputs tokens that may wrap across terminal lines
- When pasted into the onboard wizard or `models auth` commands, embedded newlines silently truncate the token value, causing persistent 401 authentication failures
- Added `normalizeSetupToken()` that strips all whitespace (including `\n`, `\r\n`) before validation and storage
- Applied normalization in all three token-paste paths: onboard wizard, `models auth setup-token`, and `models auth paste-token`
## Root Cause
Long OAuth tokens (`sk-ant-oat01-*`) exceed typical terminal line width. When a user pastes a token that wraps across lines, the `@clack/prompts` text input captures only the first line, silently truncating the token. The truncated token passes prefix validation but fails API authentication with a 401.
## Related Issues
This fix addresses or partially addresses the following open issues:
- Fixes #8733 — **Claude token paste incomplete** (exact same root cause: newline in pasted token truncates the value)
- Fixes #9141 — **Configure wizard fails to save Anthropic setup token** (token truncation during onboard wizard)
- Related to #9938 — **Anthropic OAuth tokens stopped working** (many 401 reports likely caused by truncated tokens)
- Related to #6732 — **Anthropic token authentication misverification** (token validation issues)
- Related to #17274 — **Anthropic OAuth not saving refresh token** (auth profile appears saved but token is incomplete)
- Related to #19938 — **Setup-token auth broken after update** (401 errors with setup-token auth)
## Changes
| File | Change |
|------|--------|
| `src/commands/auth-token.ts` | Added `normalizeSetupToken()` — strips all whitespace; `validateAnthropicSetupToken()` now normalizes before validating |
| `src/commands/auth-choice.apply.anthropic.ts` | Use `normalizeSetupToken()` instead of `.trim()` when storing token |
| `src/commands/models/auth.ts` | Same fix in `modelsAuthSetupTokenCommand` and `modelsAuthPasteTokenCommand` |
| `src/commands/auth-token.test.ts` | 11 unit tests covering newline stripping, CR+LF, whitespace trim, passthrough, empty input, and full validation flow |
## Test plan
- [x] 11 unit tests pass (`vitest run src/commands/auth-token.test.ts`)
- [x] `pnpm check` passes (format, types, lint — zero warnings)
- [x] `pnpm build` succeeds
- [x] Manual: paste a token with embedded newline during `openclaw models auth setup-token` — token stored correctly with full value
- [x] Manual: verified stored token in `auth-profiles.json` contains no newline and matches expected full token
Most Similar PRs
#10492: fix(auth): store Anthropic setup-token as type:oauth for auto-refresh
by sparck75 · 2026-02-06
71.2%
#17297: fix(tui): copy-paste inserts spaces into long tokens at wrap points...
by yinghaosang · 2026-02-15
70.9%
#19885: test(gateway,browser): isolate tests from ambient OPENCLAW_GATEWAY_...
by NewdlDewdl · 2026-02-18
69.5%
#10108: fix: override stale Anthropic OAuth stealth headers for Opus 4.6
by CivilBooks · 2026-02-06
67.9%
#2123: fix(auth): sync from Claude CLI keychain before OAuth refresh
by jorge123255 · 2026-01-26
67.9%
#6400: fix: set correct mode for Anthropic OAuth auth (#2697)
by coupclawbot · 2026-02-01
67.8%
#18670: feat: add first-class Claude Code CLI auth path + CLI model UX hard...
by SmithLabsLLC · 2026-02-16
67.5%
#21518: feat(auth): add Anthropic OAuth token refresh and fix external CLI ...
by maxtongwang · 2026-02-20
67.2%
#9163: Fix: Save Anthropic setup token to config file
by vishaltandale00 · 2026-02-04
67.0%
#19937: fix(gateway): validate token/password auth modes and isolate gatewa...
by NewdlDewdl · 2026-02-18
66.9%