Commit 2dc6c21
fix(cache): scope Actor definition cache to its owner (#985)
> Mostly tests + comments — the actual logic change is small: a ~10-line
ownership gate in `getActorDefinitionCached`, a simplified
`getActorMcpUrlCached`, and deleting the `mcpServerCache` global.
## What
Gate `actorDefinitionCache` hits by ownership: public Actors are served
to any caller, private Actors only to their owner; everyone else falls
through to a fresh fetch under their own token. Drop the separate
`mcpServerCache` and derive the MCP URL from the now-gated definition
cache.
## Why
The cache is process-wide and was keyed by Actor name only. On a
multi-tenant HTTP deployment, token B requesting an Actor that token A
already cached got a cache **hit that bypassed B's authorization check**
— leaking a private Actor's definition (input schema, README, run
options) across tenants. `getActorMCPServerURL` does no I/O, so
`mcpServerCache` guarded nothing expensive and re-introduced the same
leak for the MCP URL; removing it closes that vector and deletes a
global. Owner check reuses the already-cached `getUserInfoCached`, so no
extra API call on the hot path. Found in an internal security review of
the public package.
The gate hinges on two invariants (documented in
`callerMaySeeCachedActor`): `info.userId` is the platform-set **owner**,
and the caller's identity is resolved with the same `user('me')`
identity the platform authorizes with. Verified against the **live Apify
API**: a private Actor returns `isPublic: false` and `userId` equal to
its owner's `user('me').id`; a public Actor returns `isPublic: true`. So
a non-owner token resolves to a different `user('me').id`, the predicate
denies, and the cache can never grant more than a bare re-fetch would.
## Testing
- New `tests/unit/utils.actor.cache.test.ts` (7 cases) pinning the
authorization truth table: public→served to all (asserts no `user('me')`
lookup), private→owner served, private→non-owner returns the
**re-fetched** object (never the cached one), private→anonymous denied,
and the `getActorMcpUrlCached` gate (owner→URL, non-owner
private→`false`/re-fetch, missing→`false` no throw).
- `pnpm type-check`, `pnpm lint`, `pnpm format:check`, `pnpm test:unit`
(870 pass / 2 skipped) all green.
> **Residual risk (accepted):** an Actor cached while public, then
flipped private by its owner, is still served from the public fast-path
until the 30-min TTL expires. The data was world-readable at cache time
and the window is bounded; closing it would require per-request
visibility re-checks that defeat the cache.
>
> **Note:** a private Actor legitimately shared with another user
re-fetches each call (cache miss) rather than serving from cache — safe,
slightly less efficient for that uncommon case.
---------
Co-authored-by: Jiří Spilka <jiri.spilka@apify.com>1 parent 138a349 commit 2dc6c21
3 files changed
Lines changed: 149 additions & 47 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
| |||
18 | 16 | | |
19 | 17 | | |
20 | 18 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
7 | 28 | | |
8 | 29 | | |
9 | 30 | | |
| |||
17 | 38 | | |
18 | 39 | | |
19 | 40 | | |
20 | | - | |
| 41 | + | |
21 | 42 | | |
22 | 43 | | |
23 | 44 | | |
24 | 45 | | |
25 | 46 | | |
26 | 47 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
31 | 51 | | |
32 | 52 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
66 | 57 | | |
67 | 58 | | |
68 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
0 commit comments