-
Notifications
You must be signed in to change notification settings - Fork 0
feat(calendar): require trusted workspace context #139
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
11 commits
Select commit
Hold shift + click to select a range
1d32fdf
test(calendar): define trusted workspace context boundary
seonghobae 1e3d877
feat(calendar): verify trusted workspace context
seonghobae 02bf725
test(calendar): reject legacy workspace authority
seonghobae 0365f95
refactor(calendar): classify trusted context failures
seonghobae 78024d6
fix(calendar): derive workspace from trusted gateway context
seonghobae b1a8654
test(calendar): avoid hardcoded context key fixtures
seonghobae e7d7240
test(calendar): generate integration context key
seonghobae a097837
Merge branch 'main' into feat/calendar-trusted-context
opencode-agent[bot] 909cc02
test(calendar): pin trusted-context error classes
seonghobae f540ad7
test(calendar): prove unavailable context maps to 503
seonghobae 6077364
test(calendar): prove signed context outranks legacy workspace header
seonghobae 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
104 changes: 104 additions & 0 deletions
104
apps/integration-calendar-service/src/calendar-service-context.test.ts
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,104 @@ | ||
| import { createHmac, randomBytes } from 'node:crypto'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| CalendarContextInvalidError, | ||
| CalendarContextUnavailableError, | ||
| } from './calendar-service-context'; | ||
|
|
||
| const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111'; | ||
| const TEST_CONTEXT_KEY = randomBytes(32).toString('base64url'); | ||
| const NOW_SECONDS = 1_786_291_200; | ||
|
|
||
| interface CalendarContextModule { | ||
| requireTrustedCalendarWorkspaceContext( | ||
| headers: Readonly<{ | ||
| workspaceId: unknown; | ||
| issuedAt: unknown; | ||
| signature: unknown; | ||
| }>, | ||
| secret: unknown, | ||
| nowSeconds?: number, | ||
| ): string; | ||
| } | ||
|
|
||
| async function contextModule(): Promise<CalendarContextModule> { | ||
| const modulePath = './calendar-service-context'; | ||
| const module = (await import(modulePath).catch(() => ({}))) as Readonly< | ||
| Record<string, unknown> | ||
| >; | ||
| expect(typeof module.requireTrustedCalendarWorkspaceContext).toBe('function'); | ||
| return module as unknown as CalendarContextModule; | ||
| } | ||
|
|
||
| function signature(workspaceId: string, issuedAt: string): string { | ||
| return createHmac('sha256', TEST_CONTEXT_KEY) | ||
| .update(`life-os.calendar-workspace.v1\n${workspaceId}\n${issuedAt}`, 'utf8') | ||
| .digest('base64url'); | ||
| } | ||
|
|
||
| describe('trusted calendar workspace context', () => { | ||
| it('accepts one fresh signed UUIDv4 workspace context', async () => { | ||
| const { requireTrustedCalendarWorkspaceContext } = await contextModule(); | ||
| const issuedAt = String(NOW_SECONDS); | ||
|
|
||
| expect( | ||
| requireTrustedCalendarWorkspaceContext( | ||
| { | ||
| workspaceId: WORKSPACE_ID, | ||
| issuedAt, | ||
| signature: signature(WORKSPACE_ID, issuedAt), | ||
| }, | ||
| TEST_CONTEXT_KEY, | ||
| NOW_SECONDS, | ||
| ), | ||
| ).toBe(WORKSPACE_ID); | ||
| }); | ||
|
|
||
| it('classifies untrusted contexts as invalid and unusable server configuration as unavailable', async () => { | ||
| const { requireTrustedCalendarWorkspaceContext } = await contextModule(); | ||
| const issuedAt = String(NOW_SECONDS); | ||
| const valid = { | ||
| workspaceId: WORKSPACE_ID, | ||
| issuedAt, | ||
| signature: signature(WORKSPACE_ID, issuedAt), | ||
| }; | ||
| const invalid = [ | ||
| { ...valid, signature: undefined }, | ||
| { | ||
| ...valid, | ||
| signature: signature( | ||
| '22222222-2222-4222-8222-222222222222', | ||
| issuedAt, | ||
| ), | ||
| }, | ||
| { | ||
| ...valid, | ||
| issuedAt: String(NOW_SECONDS - 61), | ||
| signature: signature(WORKSPACE_ID, String(NOW_SECONDS - 61)), | ||
| }, | ||
| { | ||
| ...valid, | ||
| issuedAt: String(NOW_SECONDS + 6), | ||
| signature: signature(WORKSPACE_ID, String(NOW_SECONDS + 6)), | ||
| }, | ||
| { ...valid, workspaceId: 'attacker-selected-workspace' }, | ||
| ]; | ||
|
|
||
| for (const candidate of invalid) { | ||
| expect(() => | ||
| requireTrustedCalendarWorkspaceContext( | ||
| candidate, | ||
| TEST_CONTEXT_KEY, | ||
| NOW_SECONDS, | ||
| ), | ||
| ).toThrow(CalendarContextInvalidError); | ||
| } | ||
| expect(() => | ||
| requireTrustedCalendarWorkspaceContext( | ||
| valid, | ||
| 'short-secret', | ||
| NOW_SECONDS, | ||
| ), | ||
| ).toThrow(CalendarContextUnavailableError); | ||
| }); | ||
| }); | ||
103 changes: 103 additions & 0 deletions
103
apps/integration-calendar-service/src/calendar-service-context.ts
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 { createHmac, timingSafeEqual } from 'node:crypto'; | ||
|
|
||
| const UUID_V4_PATTERN = | ||
| /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu; | ||
| const ISSUED_AT_PATTERN = /^\d{10}$/u; | ||
| const SIGNATURE_PATTERN = /^[A-Za-z0-9_-]{43}$/u; | ||
| const MINIMUM_SECRET_BYTES = 32; | ||
| const MAXIMUM_CONTEXT_AGE_SECONDS = 60; | ||
| const MAXIMUM_FUTURE_SKEW_SECONDS = 5; | ||
| const CONTEXT_VERSION = 'life-os.calendar-workspace.v1'; | ||
|
|
||
| /** Headers accepted from the trusted LifeOS gateway boundary. */ | ||
| export interface CalendarWorkspaceContextHeaders { | ||
| readonly workspaceId: unknown; | ||
| readonly issuedAt: unknown; | ||
| readonly signature: unknown; | ||
| } | ||
|
|
||
| /** Marks a missing or unusable server-side verification configuration. */ | ||
| export class CalendarContextUnavailableError extends Error { | ||
| constructor() { | ||
| super('trusted calendar context is unavailable'); | ||
| this.name = 'CalendarContextUnavailableError'; | ||
| } | ||
| } | ||
|
|
||
| /** Marks an untrusted, malformed, stale, or forged calendar context. */ | ||
| export class CalendarContextInvalidError extends Error { | ||
| constructor() { | ||
| super('trusted calendar context is invalid'); | ||
| this.name = 'CalendarContextInvalidError'; | ||
| } | ||
| } | ||
|
|
||
| function requireSecret(secret: unknown): string { | ||
| if ( | ||
| typeof secret !== 'string' || | ||
| Buffer.byteLength(secret, 'utf8') < MINIMUM_SECRET_BYTES | ||
| ) { | ||
| throw new CalendarContextUnavailableError(); | ||
| } | ||
| return secret; | ||
| } | ||
|
|
||
| function requireIssuedAt(value: unknown, nowSeconds: number): string { | ||
| if (typeof value !== 'string' || !ISSUED_AT_PATTERN.test(value)) { | ||
| throw new CalendarContextInvalidError(); | ||
| } | ||
| const issuedAt = Number(value); | ||
| if ( | ||
| !Number.isSafeInteger(nowSeconds) || | ||
| !Number.isSafeInteger(issuedAt) || | ||
| issuedAt < nowSeconds - MAXIMUM_CONTEXT_AGE_SECONDS || | ||
| issuedAt > nowSeconds + MAXIMUM_FUTURE_SKEW_SECONDS | ||
| ) { | ||
| throw new CalendarContextInvalidError(); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| function expectedSignature( | ||
| workspaceId: string, | ||
| issuedAt: string, | ||
| secret: string, | ||
| ): Buffer { | ||
| return Buffer.from( | ||
| createHmac('sha256', secret) | ||
| .update(`${CONTEXT_VERSION}\n${workspaceId}\n${issuedAt}`, 'utf8') | ||
| .digest('base64url'), | ||
| 'ascii', | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies one short-lived server-derived workspace context and returns only | ||
| * the authenticated UUIDv4 workspace identifier. | ||
| */ | ||
| export function requireTrustedCalendarWorkspaceContext( | ||
| headers: CalendarWorkspaceContextHeaders, | ||
| secret: unknown, | ||
| nowSeconds = Math.floor(Date.now() / 1000), | ||
| ): string { | ||
| const safeSecret = requireSecret(secret); | ||
| if ( | ||
| typeof headers.workspaceId !== 'string' || | ||
| !UUID_V4_PATTERN.test(headers.workspaceId) || | ||
| typeof headers.signature !== 'string' || | ||
| !SIGNATURE_PATTERN.test(headers.signature) | ||
| ) { | ||
| throw new CalendarContextInvalidError(); | ||
| } | ||
| const workspaceId = headers.workspaceId.toLowerCase(); | ||
| const issuedAt = requireIssuedAt(headers.issuedAt, nowSeconds); | ||
| const providedSignature = Buffer.from(headers.signature, 'ascii'); | ||
| const canonicalSignature = expectedSignature(workspaceId, issuedAt, safeSecret); | ||
| if ( | ||
| providedSignature.length !== canonicalSignature.length || | ||
| !timingSafeEqual(providedSignature, canonicalSignature) | ||
| ) { | ||
| throw new CalendarContextInvalidError(); | ||
| } | ||
| return workspaceId; | ||
| } |
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
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.