Repository navigation
feat(mothership): serve worker calls only for a user and chat sim granted #10192
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
Closed
+544
−104
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,103 @@ | ||
| import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock' | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { generateInternalToken } from '@/lib/auth/internal' | ||
| import { | ||
| acceptsRunGrant, | ||
| checkRunGrant, | ||
| issueRunGrant, | ||
| rememberRunGrant, | ||
| renewedRunGrant, | ||
| runGrantHeaders, | ||
| } from '@/lib/mothership/auth/run-grant' | ||
|
|
||
| const HOUR_MS = 60 * 60 * 1000 | ||
| const turn = { userId: 'actor', chatId: 'chat-1', workspaceId: 'ws-1', organizationId: null } | ||
| const claimed = { userId: 'actor', chatId: 'chat-1', workspaceId: 'ws-1' } | ||
|
|
||
| describe('run grants', () => { | ||
| afterEach(() => { | ||
| resetEnvMock() | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('admits a call naming exactly the identity its grant names, and no other', async () => { | ||
| const grant = issueRunGrant(turn) | ||
| expect(await checkRunGrant(grant, claimed)).toMatchObject({ ok: true, identity: turn }) | ||
| for (const other of [ | ||
| { ...claimed, userId: 'someone-else' }, | ||
| { ...claimed, chatId: 'chat-2' }, | ||
| { ...claimed, workspaceId: 'ws-2' }, | ||
| { userId: 'actor', chatId: 'chat-1', organizationId: 'org-1' }, | ||
| ]) { | ||
| expect(await checkRunGrant(grant, other)).toEqual({ ok: false, reason: 'mismatch' }) | ||
| } | ||
| }) | ||
|
|
||
| it('admits a grant for a turn with no chat only on calls that name no chat', async () => { | ||
| const chatless = issueRunGrant({ ...turn, chatId: undefined }) | ||
| expect(await checkRunGrant(chatless, claimed)).toEqual({ ok: false, reason: 'mismatch' }) | ||
| const { chatId: _chatId, ...noChat } = claimed | ||
| expect(await checkRunGrant(chatless, noChat)).toMatchObject({ ok: true }) | ||
| }) | ||
|
|
||
| it('issues no grants and serves grant-less calls where Sim runs without an auth secret', async () => { | ||
| const grant = issueRunGrant(turn) | ||
| setEnv({ BETTER_AUTH_SECRET: undefined }) | ||
| expect(runGrantHeaders({ userId: 'actor', chatId: 'chat-1', workspaceId: 'ws-1' })).toEqual({}) | ||
| const check = await checkRunGrant(grant, claimed) | ||
| expect(check).toEqual({ ok: false, reason: 'missing' }) | ||
| expect(acceptsRunGrant(check)).toBe(true) | ||
| }) | ||
|
|
||
| it('reads the enforcement switch as the string the environment holds', async () => { | ||
| const missing = await checkRunGrant(undefined, claimed) | ||
| for (const [value, accepted] of [ | ||
| ['false', true], | ||
| ['0', true], | ||
| ['true', false], | ||
| ['1', false], | ||
| ] as const) { | ||
| setEnv({ MOTHERSHIP_REQUIRE_RUN_GRANT: value }) | ||
| expect([value, acceptsRunGrant(missing)]).toEqual([value, accepted]) | ||
| } | ||
| }) | ||
|
|
||
| it('refuses a token the worker could mint itself with the secret it holds', async () => { | ||
| const forged = await generateInternalToken('actor') | ||
| const check = await checkRunGrant(forged, claimed) | ||
| expect(check).toEqual({ ok: false, reason: 'invalid' }) | ||
| expect(acceptsRunGrant(check)).toBe(false) | ||
| }) | ||
|
|
||
| it('serves a call with a missing or expired grant only until grants are required', async () => { | ||
| vi.useFakeTimers({ toFake: ['Date'] }) | ||
| vi.setSystemTime(Date.now()) | ||
| const grant = issueRunGrant(turn) | ||
| vi.setSystemTime(Date.now() + 25 * HOUR_MS) | ||
| const expired = await checkRunGrant(grant, claimed) | ||
| const missing = await checkRunGrant(undefined, claimed) | ||
| expect([expired, missing]).toEqual([ | ||
| { ok: false, reason: 'expired' }, | ||
| { ok: false, reason: 'missing' }, | ||
| ]) | ||
| expect([acceptsRunGrant(expired), acceptsRunGrant(missing)]).toEqual([true, true]) | ||
| setEnv({ MOTHERSHIP_REQUIRE_RUN_GRANT: true }) | ||
| expect([acceptsRunGrant(expired), acceptsRunGrant(missing)]).toEqual([false, false]) | ||
| }) | ||
|
|
||
| it('renews a verified grant once past half its life, for the same identity only', async () => { | ||
| vi.useFakeTimers({ toFake: ['Date'] }) | ||
| const issuedAt = Date.now() | ||
| vi.setSystemTime(issuedAt) | ||
| const verified = {} | ||
| rememberRunGrant(verified, await checkRunGrant(issueRunGrant(turn), claimed)) | ||
| vi.setSystemTime(issuedAt + 11 * HOUR_MS) | ||
| expect(renewedRunGrant(verified)).toBeUndefined() | ||
| vi.setSystemTime(issuedAt + 13 * HOUR_MS) | ||
| const renewed = renewedRunGrant(verified) | ||
| expect(await checkRunGrant(renewed, claimed)).toMatchObject({ ok: true, identity: turn }) | ||
| const unverified = {} | ||
| rememberRunGrant(unverified, await checkRunGrant(undefined, claimed)) | ||
| expect(renewedRunGrant(unverified)).toBeUndefined() | ||
| }) | ||
| }) |
Oops, something went wrong.
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.