Commit c3bb8e3
authored
feat(billing): add cch attestation for OAuth requests (#8)
## Summary
Add billing attribution header computation with cch request integrity
hash for OAuth-authenticated API requests. This matches Claude Code's
native client attestation protocol, enabling subscription-tier rate
limits and fast mode access.
The cch mechanism was reverse-engineered from Anthropic's custom Bun
binary ([a10k.co
writeup](https://a10k.co/b/reverse-engineering-claude-code-cch.html)) —
it's xxHash64 with a fixed seed, masked to 5 hex chars. The fingerprint
suffix is SHA-256 of salt + 3 characters from the first user message +
version.
- Add `billing.rs` module: `compute_fingerprint` (SHA-256 suffix),
`build_billing_header` (header assembly with placeholder), `inject_cch`
(xxHash64 body hash + single-occurrence replacement).
- Integrate into `anthropic.rs`: for OAuth, prepend billing header as
the first system block, serialize to string, compute and inject cch
before sending.
- Reorder `CreateMessageRequest` fields so `system` serializes before
`messages`, ensuring the placeholder in the billing header is found
first by `str::replacen` even when tool results contain the literal
`cch=00000`.
- Switch `stream_sse` from `serde_json::Value` + `.json()` to `String` +
`.body()` to support post-serialization cch injection.
- API key auth is unaffected — billing header is only injected for
OAuth.
### Avoiding Claude Code's prompt cache bug
Claude Code's Bun runtime performs a global in-place string mutation of
`cch=` values across the entire request body, including historical tool
results
([anthropics/claude-code#40652](anthropics/claude-code#40652)).
This permanently invalidates prompt cache and wastes 30-50K+ tokens per
turn. Our implementation is structurally immune:
- `inject_cch` takes `&str` (immutable) and returns a new `String` —
Rust's ownership model prevents in-place mutation.
- `str::replacen(..., 1)` replaces only the first occurrence (the
billing header's placeholder).
- The conversation `messages` slice is never modified — it's serialized
fresh each turn.
## Changes
| File | Description |
| ---- | ----------- |
| `Cargo.toml` | Add `sha2 = "0.10"` and `xxhash-rust = { version =
"0.8", features = ["xxh64"] }` to workspace dependencies |
| `crates/oxide-code/Cargo.toml` | Wire up `sha2` and `xxhash-rust` |
| `crates/oxide-code/src/client.rs` | Add `mod billing` |
| `crates/oxide-code/src/client/billing.rs` | New module:
`compute_fingerprint`, `build_billing_header`, `inject_cch` with 8 tests
|
| `crates/oxide-code/src/client/anthropic.rs` | Import `billing`,
`ContentBlock`, `Role`; reorder `CreateMessageRequest` fields (`system`
before `messages`); compute billing header for OAuth in
`stream_message`; switch `stream_sse` body from `Value` to `String`; add
`first_user_text` helper with 4 tests |
| `CLAUDE.md` | Add `billing.rs` to crate structure diagram |
| `docs/research/anthropic-api.md` | Replace "tamper-proof /
unreplicable" characterization with full reverse-engineered algorithm,
constants, known bugs, and oxide-code's approach |
## Test plan
- [x] `cargo fmt --all --check` — clean
- [x] `cargo build` compiles cleanly
- [x] `cargo clippy --all-targets -- -D warnings` — zero warnings
- [x] `cargo test` — 208 tests pass (12 new)
- [x] `cargo llvm-cov --ignore-filename-regex 'main\.rs'` — 87% line
coverage (`billing.rs` at 100%)1 parent 57d1b7b commit c3bb8e3
9 files changed
Lines changed: 398 additions & 36 deletions
File tree
- .cspell
- crates/oxide-code
- src
- client
- docs/research
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
| 28 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
30 | 31 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
35 | 36 | | |
36 | | - | |
37 | | - | |
38 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
39 | 40 | | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| 31 | + | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
0 commit comments