Commit d0024f0
authored
## Summary
Cached GET responses now live in per-identity namespaces so switching
accounts never serves another user's data. `buildCacheKey(method, url)`
internally mixes in a fingerprint of the active bearer identity,
producing distinct cache files per account for the same URL.
**Identity fingerprint** — a memoized 16-char MD5 hex of `kind|secret`,
derived (in order) from:
- **OAuth** — the stored `refresh_token`. Hourly access-token rotation
keeps the same refresh_token, so the cache stays hot across refreshes.
If the OAuth server DOES rotate the refresh_token on refresh (per spec,
allowed), the fingerprint changes and the cache naturally re-populates
under the new identity.
- **Env token** — the raw `SENTRY_AUTH_TOKEN` / `SENTRY_TOKEN` value.
Env tokens don't rotate, so hashing the token is equivalent to hashing a
stable identity.
- **No token** — sentinel `"<anon>"` (angle brackets can't appear in a
real hex fingerprint, so no collision with a computed hash is possible).
Memoization is reset on `setAuthToken` and `clearAuth`. OAuth refresh
goes through `setAuthToken` so the fingerprint rotates correctly. MD5 is
intentional: this is a cache namespace, not authentication — collisions
are benign (two colliding identities would share cache space, like the
anonymous case). CodeQL flagged this as a weak-crypto/insufficient-hash
finding; dismissed as "won't fix" with context.
This fixes the stale-data bugs reported in #785:
- **#3** `auth whoami` no longer returns the previous user after `auth
login` / env-token swap.
- **#2** Post-mutation cache staleness is now bounded to a single
identity rather than leaking across accounts.
**Mutation hooks call `invalidateCachedResponsesMatching` for
same-identity freshness:**
`project create`, `project delete`, `issue resolve`, `issue unresolve`,
`issue merge` all flush the relevant detail and list endpoints
immediately. The sweep gates on an `identity` field persisted in every
cache entry so one identity's mutation can't evict another identity's
cache on a shared cache directory.
**Cache-key / query-param correctness:** `getIssue` / `getIssueInOrg`
pass `?collapse=stats&collapse=lifetime&...` so cached URLs carry query
strings. Using exact-match `invalidateCachedResponse(<base url without
params>)` would silently miss them. Mutation paths now use prefix-sweep
(`invalidateCachedResponsesMatching`) on the trailing-slash base URL —
which catches every cached variant regardless of query params.
**URL-construction helper:** Introduced `buildApiUrl(regionUrl,
...segments)` in `api/infrastructure.ts` to replace repeated
`${base}/api/0/path/${encodeURIComponent(slug)}/` template-string
building. Per-segment encoding means a slug containing a literal `/`
(rare but possible) now produces a correctly-encoded URL instead of a
silently-broken one.
Cache-invalidation helpers in `api/issues.ts` and `api/projects.ts` are
contractually no-throw — a post-mutation DB/FS failure inside the helper
can't turn a successful mutation into a user-visible error.
**Circular import note:** `db/auth.ts` previously imported
`clearResponseCache` statically; now that `response-cache.ts` imports
`getIdentityFingerprint` from `db/auth.ts`, that's resolved via a
dynamic `await import()` inside the (already-async) `clearAuth()`.
## Test plan
- `bun test test/lib/db/auth.test.ts` — `getIdentityFingerprint`
coverage: stability across OAuth access-token rotation, distinct
fingerprints per env token / per OAuth session, `SENTRY_FORCE_ENV_TOKEN`
switching the source, `kind`-prefix isolation (env/oauth with same
secret produce distinct fingerprints), expired access-only token
fall-through to env.
- `bun test test/lib/response-cache.test.ts` — identity isolation (two
different auth states never share a cache key for the same URL;
`invalidateCachedResponsesMatching` leaves other identities' cached
entries alone) plus a regression test for the query-param-variant case.
- `bun test test/lib/api/infrastructure.test.ts` — `buildApiUrl`
composition, trailing-slash normalization, reserved-char encoding
(slash, space), and the zero-segments edge case.
- `bun run typecheck`, `bun run lint` — clean.
- Full unit suite: 5527 passing.
Part of #785 (addresses items #2, #3, and half of #7 — the other half is
fixed by a Sentry backend change).
1 parent 2359f96 commit d0024f0
11 files changed
Lines changed: 672 additions & 111 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
168 | 189 | | |
169 | 190 | | |
170 | 191 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
554 | 557 | | |
555 | 558 | | |
556 | 559 | | |
| 560 | + | |
557 | 561 | | |
558 | 562 | | |
559 | 563 | | |
| |||
562 | 566 | | |
563 | 567 | | |
564 | 568 | | |
| 569 | + | |
565 | 570 | | |
566 | 571 | | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
572 | 585 | | |
573 | 586 | | |
574 | 587 | | |
| |||
612 | 625 | | |
613 | 626 | | |
614 | 627 | | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
615 | 637 | | |
616 | 638 | | |
617 | 639 | | |
| |||
671 | 693 | | |
672 | 694 | | |
673 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
28 | 33 | | |
29 | 34 | | |
30 | 35 | | |
31 | 36 | | |
32 | 37 | | |
33 | 38 | | |
34 | 39 | | |
| 40 | + | |
35 | 41 | | |
36 | 42 | | |
37 | 43 | | |
| |||
166 | 172 | | |
167 | 173 | | |
168 | 174 | | |
| 175 | + | |
169 | 176 | | |
170 | 177 | | |
171 | 178 | | |
| |||
215 | 222 | | |
216 | 223 | | |
217 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
218 | 265 | | |
219 | 266 | | |
220 | 267 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | | - | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
224 | 227 | | |
225 | 228 | | |
226 | 229 | | |
| |||
235 | 238 | | |
236 | 239 | | |
237 | 240 | | |
| 241 | + | |
238 | 242 | | |
239 | | - | |
240 | | - | |
| 243 | + | |
241 | 244 | | |
| 245 | + | |
242 | 246 | | |
243 | 247 | | |
244 | 248 | | |
| |||
250 | 254 | | |
251 | 255 | | |
252 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
253 | 339 | | |
254 | 340 | | |
255 | 341 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
141 | 141 | | |
142 | 142 | | |
143 | 143 | | |
| 144 | + | |
| 145 | + | |
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
| |||
0 commit comments