-
Notifications
You must be signed in to change notification settings - Fork 173
fix(cache): scope Actor definition cache to its owner #985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f086b1
fix(cache): scope Actor definition cache to its owner
MQ37 5c33d24
test(cache): cover MCP-URL gate + fail-closed paths; document invariant
MQ37 01fc479
test(cache): trim to the load-bearing isolation cases
MQ37 e8a07aa
Merge remote-tracking branch 'origin/master' into fix/cache-tenant-is…
MQ37 d43a8cd
test(cache): add isOrganization to getUserInfoCached mocks
MQ37 6bfa90a
Update src/utils/actor.ts
MQ37 bd5d485
Merge remote-tracking branch 'origin/master' into fix/cache-tenant-is…
MQ37 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import type { ApifyClient } from '../../src/apify_client.js'; | ||
| import type { ActorDefinitionWithInfo } from '../../src/types.js'; | ||
|
|
||
| vi.mock('../../src/tools/build.js', () => ({ getActorDefinition: vi.fn() })); | ||
| vi.mock('../../src/utils/userid_cache.js', () => ({ getUserInfoCached: vi.fn() })); | ||
|
|
||
| import { actorDefinitionCache } from '../../src/state.js'; | ||
| import { getActorDefinition } from '../../src/tools/build.js'; | ||
| import { getActorDefinitionCached, getActorMcpUrlCached } from '../../src/utils/actor.js'; | ||
| import { getUserInfoCached } from '../../src/utils/userid_cache.js'; | ||
|
|
||
| const getActorDefinitionMock = vi.mocked(getActorDefinition); | ||
| const getUserInfoCachedMock = vi.mocked(getUserInfoCached); | ||
|
|
||
| // Each test uses a unique Actor name, so the shared module-level cache never collides between cases. | ||
| function seedCache( | ||
| name: string, | ||
| isPublic: boolean, | ||
| ownerUserId: string, | ||
| opts: { id?: string; webServerMcpPath?: string } = {}, | ||
| ): ActorDefinitionWithInfo { | ||
| const id = opts.id ?? name; | ||
| const entry = { | ||
| definition: { | ||
| id, | ||
| actorFullName: name, | ||
| ...(opts.webServerMcpPath && { webServerMcpPath: opts.webServerMcpPath }), | ||
| }, | ||
| info: { id, isPublic, userId: ownerUserId }, | ||
| } as unknown as ActorDefinitionWithInfo; | ||
| actorDefinitionCache.set(name, entry); | ||
| return entry; | ||
| } | ||
|
|
||
| const client = { token: 'caller-token' } as unknown as ApifyClient; | ||
|
|
||
| beforeEach(() => { | ||
| getActorDefinitionMock.mockReset(); | ||
| getUserInfoCachedMock.mockReset(); | ||
| }); | ||
|
|
||
| describe('getActorDefinitionCached — tenant isolation', () => { | ||
| it('serves a cached public Actor to any caller without an ownership check', async () => { | ||
| const cached = seedCache('acme/public-1', true, 'owner-1'); | ||
|
|
||
| const result = await getActorDefinitionCached('acme/public-1', client); | ||
|
|
||
| expect(result).toBe(cached); | ||
| expect(getUserInfoCachedMock).not.toHaveBeenCalled(); | ||
| expect(getActorDefinitionMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('serves a cached private Actor to its owner', async () => { | ||
| const cached = seedCache('acme/private-owner', false, 'owner-2'); | ||
| getUserInfoCachedMock.mockResolvedValue({ userId: 'owner-2', userPlanTier: 'FREE', isOrganization: false }); | ||
|
|
||
| const result = await getActorDefinitionCached('acme/private-owner', client); | ||
|
|
||
| expect(result).toBe(cached); | ||
| expect(getActorDefinitionMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does NOT serve a cached private Actor to a non-owner — returns the re-fetched object, never the cached one', async () => { | ||
| const cached = seedCache('acme/private-other', false, 'owner-3'); | ||
| getUserInfoCachedMock.mockResolvedValue({ userId: 'intruder', userPlanTier: 'FREE', isOrganization: false }); | ||
| const refetched = { | ||
| definition: {}, | ||
| info: { isPublic: false, userId: 'owner-3' }, | ||
| } as unknown as ActorDefinitionWithInfo; | ||
| getActorDefinitionMock.mockResolvedValue(refetched); | ||
|
|
||
| const result = await getActorDefinitionCached('acme/private-other', client); | ||
|
|
||
| expect(result).toBe(refetched); | ||
| expect(result).not.toBe(cached); | ||
| expect(getActorDefinitionMock).toHaveBeenCalledWith('acme/private-other', client); | ||
| }); | ||
|
|
||
| it('does NOT serve a cached private Actor to an anonymous caller', async () => { | ||
| seedCache('acme/private-anon', false, 'owner-4'); | ||
| getUserInfoCachedMock.mockResolvedValue({ userId: null, userPlanTier: 'FREE', isOrganization: false }); | ||
| getActorDefinitionMock.mockResolvedValue(null); | ||
|
|
||
| const result = await getActorDefinitionCached('acme/private-anon', client); | ||
|
|
||
| expect(result).toBeNull(); | ||
| expect(getActorDefinitionMock).toHaveBeenCalledWith('acme/private-anon', client); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getActorMcpUrlCached — tenant isolation', () => { | ||
| it('derives the MCP URL from a cached Actor the caller may see', async () => { | ||
| seedCache('acme/mcp-public', true, 'owner-6', { id: 'actorpub', webServerMcpPath: '/mcp' }); | ||
|
|
||
| const result = await getActorMcpUrlCached('acme/mcp-public', client); | ||
|
|
||
| expect(result).toBe('https://actorpub.apify.actor/mcp'); | ||
| expect(getActorDefinitionMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does NOT leak a cached private Actor MCP URL to a non-owner — re-fetches and returns false', async () => { | ||
| seedCache('acme/mcp-private', false, 'owner-7', { id: 'actorpriv', webServerMcpPath: '/mcp' }); | ||
| getUserInfoCachedMock.mockResolvedValue({ userId: 'intruder', userPlanTier: 'FREE', isOrganization: false }); | ||
| getActorDefinitionMock.mockResolvedValue(null); // intruder's own fetch is unauthorized | ||
|
|
||
| const result = await getActorMcpUrlCached('acme/mcp-private', client); | ||
|
|
||
| expect(result).toBe(false); | ||
| expect(getActorDefinitionMock).toHaveBeenCalledWith('acme/mcp-private', client); | ||
| }); | ||
|
|
||
| it('returns false for a non-existent Actor without throwing', async () => { | ||
| getActorDefinitionMock.mockResolvedValue(null); | ||
|
|
||
| await expect(getActorMcpUrlCached('acme/missing', client)).resolves.toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.