From 2b04a8b3b6a9d2838c99967a4c5fc5f6f0b04640 Mon Sep 17 00:00:00 2001 From: Rui Chen Date: Sat, 24 Oct 2026 16:30:43 -0400 Subject: [PATCH] fix(auth): token validation and direct-download safeguards A direct-download URL is a bearer credential for the stored object, and the only gate in front of it is the token's `repository_id` and `ac` claims. With SKIP_TOKEN_VALIDATION those claims are never verified, so the server now refuses to start with ENABLE_DIRECT_DOWNLOADS and SKIP_TOKEN_VALIDATION together, unless UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION is set for local testing. - DIRECT_DOWNLOAD_URL_TTL_SECONDS (1-3600, default 600, the lifetime signed URLs had so far) sets the signed URL lifetime. The reader lease outlives the URL by a fixed margin, since storage authenticates a GET only when it starts. - Token verification requires `exp` (jose checks it only when present); the optional ACTIONS_TOKEN_AUDIENCE pins `aud`, and a value that lists no audience stops startup instead of disabling the check. - Token rejections report the jose error code and claim name instead of attaching the jose error, which carries the decoded claims, to the logged error. - `ac` scopes are schema-checked and treated as bit flags (read = 1, write = 2): lookups use only readable scopes, uploads and finalizes only writable ones. The tests run a second server against a local OIDC issuer (discovery and JWKS), covering key rotation, a JWKS outage, pull request scopes and the signed URL lifetime on S3, GCS and Azure Blob. Signed-off-by: Rui Chen --- README.md | 9 + ...-reader-leases-protect-active-downloads.md | 2 +- ...irect-downloads-require-verified-tokens.md | 7 + lib/env.ts | 13 +- lib/schemas.ts | 48 ++ lib/scope.ts | 57 +- lib/storage-leases.ts | 7 +- lib/storage.ts | 18 +- .../CreateCacheEntry.post.ts | 4 +- .../FinalizeCacheEntryUpload.ts | 4 +- .../GetCacheEntryDownloadURL.post.ts | 15 +- tests/cleanup-lifecycle.test.ts | 8 +- tests/direct-download-auth.test.ts | 573 ++++++++++++++++++ tests/setup.ts | 24 +- tests/write-authorization.test.ts | 93 +++ 15 files changed, 844 insertions(+), 38 deletions(-) create mode 100644 docs/adr/0010-direct-downloads-require-verified-tokens.md create mode 100644 tests/direct-download-auth.test.ts create mode 100644 tests/write-authorization.test.ts diff --git a/README.md b/README.md index 90307017..00b49cd8 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,15 @@ volumes: cache-data: ``` +Direct downloads (`ENABLE_DIRECT_DOWNLOADS=true`) hand the runner a signed storage URL for merged entries instead of proxying the bytes. +They require token validation: the server refuses to start with `SKIP_TOKEN_VALIDATION=true`, because unverified callers could then obtain signed URLs for any repository's cache. + +| Variable | Default | Effect | +| -------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------- | +| `DIRECT_DOWNLOAD_URL_TTL_SECONDS` | `600` | Lifetime of a signed download URL, `1` to `3600`. Lower it to limit replay of a leaked URL. | +| `ACTIONS_TOKEN_AUDIENCE` | unset | Comma-separated `aud` values a token must carry; unset skips the check, an empty list is refused. | +| `UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION` | `false` | Allows direct downloads with `SKIP_TOKEN_VALIDATION=true`. Local testing only. | + ## Documentation 👉 👈 diff --git a/docs/adr/0002-part-reader-leases-protect-active-downloads.md b/docs/adr/0002-part-reader-leases-protect-active-downloads.md index b4a93099..7640afee 100644 --- a/docs/adr/0002-part-reader-leases-protect-active-downloads.md +++ b/docs/adr/0002-part-reader-leases-protect-active-downloads.md @@ -1,3 +1,3 @@ # Storage Reader Leases protect active downloads -Concurrent downloads hold durable, expiring Storage Reader Leases, including direct downloads whose lease lasts no longer than the signed URL. A renewable, fenced Merge Lease grants a single worker authority to complete the Merge; Part Reader Leases protect concurrent readers of Parts. Both expiring lease types renew every 30 seconds while active and expire after two minutes, except direct-download leases, which expire with their signed URLs. Parts are deleted only after the merge completes and no active Part Reader Leases remain; a Storage Location is deleted only after no Storage Reader Leases remain. A reader acquires its lease in the same locked transaction that reads the Storage Location's merge state, so its scope is chosen atomically: a download arriving before the merge takes a Part Reader Lease that blocks parts deletion, and one arriving after takes a Storage Reader Lease over the merged representation. There is therefore no window in which a reader can fail to acquire a Part Reader Lease against parts that are being deleted. +Concurrent downloads hold durable, expiring Storage Reader Leases, including direct downloads, whose lease outlives the signed URL by a fixed margin. A renewable, fenced Merge Lease grants a single worker authority to complete the Merge; Part Reader Leases protect concurrent readers of Parts. Both expiring lease types renew every 30 seconds while active and expire after two minutes, except direct-download leases, which expire a fixed margin after their signed URLs: storage authenticates a GET when it starts, so a transfer begun just before the URL expires keeps reading after it. Parts are deleted only after the merge completes and no active Part Reader Leases remain; a Storage Location is deleted only after no Storage Reader Leases remain. A reader acquires its lease in the same locked transaction that reads the Storage Location's merge state, so its scope is chosen atomically: a download arriving before the merge takes a Part Reader Lease that blocks parts deletion, and one arriving after takes a Storage Reader Lease over the merged representation. There is therefore no window in which a reader can fail to acquire a Part Reader Lease against parts that are being deleted. diff --git a/docs/adr/0010-direct-downloads-require-verified-tokens.md b/docs/adr/0010-direct-downloads-require-verified-tokens.md new file mode 100644 index 00000000..81d94986 --- /dev/null +++ b/docs/adr/0010-direct-downloads-require-verified-tokens.md @@ -0,0 +1,7 @@ +# Direct downloads require verified tokens + +A direct-download URL is a bearer credential: whoever holds it can read the merged object from storage until it expires, without asking the server again. The only thing standing between a caller and that URL is `getCacheScope`, which authorizes a lookup from the token's `repository_id` and `ac` claims. With `SKIP_TOKEN_VALIDATION=true` those claims are decoded but not verified, so any client that can reach the server can name any repository and scope and walk away with a signed URL for its cache. The server therefore refuses to start with `ENABLE_DIRECT_DOWNLOADS=true` and `SKIP_TOKEN_VALIDATION=true` together. `UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION=true` overrides the check for local testing only; it is named so that it cannot be set by accident. This extends ADR-0006: skipping verification stays a dev/test escape hatch, and the one feature that turns an unverified claim into a storage credential is now unreachable with it. + +A signed URL lives `DIRECT_DOWNLOAD_URL_TTL_SECONDS` (default 600, the lifetime it had before the setting existed; at most 3600). Clients request the URL immediately before fetching it, and storage authenticates a GET when it starts rather than for its whole duration, so the lifetime only has to cover the start of the transfer and the client's retries, not a large restore. A shorter lifetime limits replay of a URL that leaks into job logs; with temporary AWS credentials, 300 or less also stays inside the window in which the AWS SDK refreshes them (5 minutes before they expire), so a URL is never signed by credentials that lapse before it does. Clients that cache a URL and fetch it later, such as BuildKit's `type=gha` cache importing a layer late in a build, need a longer lifetime. The Storage Reader Lease outlives the URL by a fixed margin (ADR-0002), so cleanup cannot delete an object while a transfer begun just before expiry is still reading it. + +Token verification requires an `exp` claim, because jose only checks expiry when the claim is present. The optional `ACTIONS_TOKEN_AUDIENCE` pins the expected `aud`, restricting the server to tokens issued for one installation; a value that lists no audience (blank, or only commas) stops startup rather than silently disabling the check. Token rejections return and log the jose error code and failing claim name, never the token or its decoded claims, which jose attaches to its errors. Cache scope permissions are bit flags (read = 1, write = 2): lookups use only scopes with the read bit, and uploads only scopes with the write bit. diff --git a/lib/env.ts b/lib/env.ts index 587caa4e..aace2fd3 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -1,6 +1,10 @@ import type { envDbDriverSchema, envStorageDriverSchema } from './schemas' import arkenv from 'arkenv' -import { envSchema } from './schemas' +import { + assertDirectDownloadsRequireTokenValidation, + envSchema, + parseActionsTokenAudience, +} from './schemas' export const env = arkenv(envSchema, { env: Object.assign( @@ -13,3 +17,10 @@ export const env = arkenv(envSchema, { process.env, ), }) + +assertDirectDownloadsRequireTokenValidation(env) + +// GitHub-issued runtime tokens carry a deployment-specific `aud`, so pinning it +// restricts the server to one installation's tokens. Parsed here so a bad value +// stops startup. +export const actionsTokenAudience = parseActionsTokenAudience(env.ACTIONS_TOKEN_AUDIENCE) diff --git a/lib/schemas.ts b/lib/schemas.ts index bbae5f53..2edfec3c 100644 --- a/lib/schemas.ts +++ b/lib/schemas.ts @@ -76,6 +76,7 @@ export const envBaseSchema = type({ "string.url = 'https://results-receiver.actions.githubusercontent.com'", 'ACTIONS_TOKEN_ISSUER': "string.url = 'https://token.actions.githubusercontent.com'", 'ACTIONS_TOKEN_JWKS_URL?': 'string.url', + 'ACTIONS_TOKEN_AUDIENCE?': 'string', 'CACHE_CLEANUP_OLDER_THAN_DAYS': 'number = 90', 'CACHE_MAX_SIZE_BYTES?': 'number.integer > 0', 'CACHE_FILESYSTEM_MAX_USAGE_PERCENT': 'number > 0 & number <= 100 = 90', @@ -83,6 +84,8 @@ export const envBaseSchema = type({ 'DISABLE_CLEANUP_JOBS?': 'boolean', 'DEBUG?': 'unknown', 'ENABLE_DIRECT_DOWNLOADS': 'boolean = false', + 'DIRECT_DOWNLOAD_URL_TTL_SECONDS': '1 <= number.integer <= 3600 = 600', + 'UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION': 'boolean = false', 'EAGER_MERGE': 'boolean = false', 'BENCHMARK': 'boolean = false', 'SKIP_TOKEN_VALIDATION': 'boolean = false', @@ -91,3 +94,48 @@ export const envBaseSchema = type({ export const envSchema = envBaseSchema.and(envStorageDriverSchema).and(envDbDriverSchema) export type Env = typeof envSchema.infer + +/** + * A direct-download URL is a bearer credential for the stored object. Handing + * one out to a caller whose token was never verified lets anyone who can reach + * the server read any repository's cache straight from storage, so the two + * settings are refused together unless explicitly overridden for local testing. + */ +export function assertDirectDownloadsRequireTokenValidation( + env: Pick< + Env, + | 'ENABLE_DIRECT_DOWNLOADS' + | 'SKIP_TOKEN_VALIDATION' + | 'UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION' + >, +) { + if ( + env.ENABLE_DIRECT_DOWNLOADS && + env.SKIP_TOKEN_VALIDATION && + !env.UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION + ) + throw new Error( + 'ENABLE_DIRECT_DOWNLOADS=true requires token validation, but SKIP_TOKEN_VALIDATION=true. ' + + 'Unverified callers would receive signed storage URLs for any cache entry. ' + + 'Unset SKIP_TOKEN_VALIDATION, or set UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION=true for local testing only.', + ) +} + +/** + * The `aud` values a token must carry. Unset means no audience check; set but + * listing no audience (blank, or only commas) is refused, so a typo cannot + * silently switch the check off. + */ +export function parseActionsTokenAudience(value: string | undefined) { + if (value === undefined) return + const audience = value + .split(',') + .map((entry) => entry.trim()) + .filter(Boolean) + if (audience.length === 0) + throw new Error( + 'ACTIONS_TOKEN_AUDIENCE is set but lists no audience. ' + + 'Set one or more comma-separated audiences, or unset it to skip the audience check.', + ) + return audience +} diff --git a/lib/scope.ts b/lib/scope.ts index bddbb61f..3b611edc 100644 --- a/lib/scope.ts +++ b/lib/scope.ts @@ -1,7 +1,8 @@ import type { H3Event } from 'h3' import * as jose from 'jose' import { hasAtLeast } from 'remeda' -import { env } from './env' +import { z } from 'zod' +import { actionsTokenAudience, env } from './env' import { logger } from './logger' const issuer = env.ACTIONS_TOKEN_ISSUER.replace(/\/$/, '') @@ -64,20 +65,50 @@ async function verifyGitHubActionsToken(token: string) { return jose.decodeJwt(token) } - const { payload } = await jose.jwtVerify(token, await getJwks(), { issuer }) + const { payload } = await jose.jwtVerify(token, await getJwks(), { + issuer, + audience: actionsTokenAudience, + // jose checks `exp` and `nbf` only when present; a token without `exp` + // would otherwise never expire. + requiredClaims: ['exp'], + }) return payload } +/** + * The reason a token was rejected, safe to log and return: never the token or + * its claims. jose errors carry the decoded payload, so they must not become + * the `cause` of a logged error. + */ +function describeTokenError(err: unknown) { + if (err instanceof jose.errors.JWTClaimValidationFailed || err instanceof jose.errors.JWTExpired) + return `${err.code} (${err.claim})` + if (err instanceof jose.errors.JOSEError) return err.code + return err instanceof Error ? err.name : 'unknown error' +} + +export const CACHE_PERMISSION_READ = 1 +export const CACHE_PERMISSION_WRITE = 2 + +const cacheScopesSchema = z.array( + z.object({ + Scope: z.string().min(1), + Permission: z.number().int().nonnegative(), + }), +) + function parseJsonScopes(json: string) { + let value: unknown try { - return JSON.parse(json) - } catch (err) { - throw createError({ - statusCode: 401, - message: 'Invalid JSON in cache scopes', - cause: err, - }) + value = JSON.parse(json) + } catch { + // no `cause`: the parser's message quotes the claim + throw createError({ statusCode: 401, message: 'Invalid JSON in cache scopes' }) } + + const parsed = cacheScopesSchema.safeParse(value) + if (!parsed.success) throw createError({ statusCode: 401, message: 'Invalid cache scopes' }) + return parsed.data } export async function getCacheScope(event: H3Event) { @@ -86,18 +117,14 @@ export async function getCacheScope(event: H3Event) { throw createError({ statusCode: 401, message: 'Authorization header missing or malformed' }) const decoded = await verifyGitHubActionsToken(token).catch((err) => { - throw createError({ - statusCode: 401, - message: 'Invalid token', - cause: err, - }) + throw createError({ statusCode: 401, message: `Invalid token: ${describeTokenError(err)}` }) }) const scopesJson = decoded.ac if (!scopesJson || typeof scopesJson !== 'string') throw createError({ statusCode: 401, message: 'Token does not contain cache scopes' }) - const scopes = parseJsonScopes(scopesJson) as Array<{ Scope: string; Permission: number }> + const scopes = parseJsonScopes(scopesJson) if (!hasAtLeast(scopes, 1)) throw createError({ statusCode: 401, message: 'Token does not contain any cache scopes' }) diff --git a/lib/storage-leases.ts b/lib/storage-leases.ts index c06dc8e2..5079f459 100644 --- a/lib/storage-leases.ts +++ b/lib/storage-leases.ts @@ -6,7 +6,12 @@ import { env } from './env' export const LEASE_DURATION_MS = 2 * 60 * 1000 export const LEASE_RENEWAL_MS = 30 * 1000 -export const DIRECT_DOWNLOAD_LEASE_DURATION_MS = 10 * 60 * 1000 +/** + * How long a direct-download reader lease outlives its signed URL. A download + * that starts just before the URL expires keeps streaming, so the lease must + * cover that transfer too. + */ +export const DIRECT_DOWNLOAD_LEASE_MARGIN_MS = 10 * 60 * 1000 export async function acquireMergeLease( db: Kysely, diff --git a/lib/storage.ts b/lib/storage.ts index d40ce618..b004f26e 100644 --- a/lib/storage.ts +++ b/lib/storage.ts @@ -46,7 +46,7 @@ import { logger } from './logger' import { acquireMergeLease, createReaderLease, - DIRECT_DOWNLOAD_LEASE_DURATION_MS, + DIRECT_DOWNLOAD_LEASE_MARGIN_MS, LEASE_RENEWAL_MS, releaseMergeLease, releaseReaderLease, @@ -774,9 +774,12 @@ export class Storage { cacheEntry: cacheEntry.match, } - // Merged entry with direct downloads enabled: take a reader lease bounded - // by the signed URL lifetime, then sign. - const directDownloadExpiresAt = Date.now() + DIRECT_DOWNLOAD_LEASE_DURATION_MS + // Merged entry with direct downloads enabled: take a reader lease that + // outlives the signed URL, then sign. Storage authenticates a GET when it + // starts, so a download begun just before the URL expires keeps reading + // after it; the margin keeps cleanup from deleting the object under it. + const urlExpiresAt = Date.now() + env.DIRECT_DOWNLOAD_URL_TTL_SECONDS * 1000 + const leaseExpiresAt = urlExpiresAt + DIRECT_DOWNLOAD_LEASE_MARGIN_MS const leased = await this.db.transaction().execute(async (tx) => { let query = tx .selectFrom('storage_locations') @@ -785,7 +788,7 @@ export class Storage { if (env.DB_DRIVER !== 'sqlite') query = query.forUpdate() const current = await query.executeTakeFirst() if (!current?.mergedAt) return current - await createReaderLease(tx, current.id, 'storage', directDownloadExpiresAt) + await createReaderLease(tx, current.id, 'storage', leaseExpiresAt) await tx .updateTable('storage_locations') .set({ lastDownloadedAt: Date.now() }) @@ -797,10 +800,7 @@ export class Storage { // The merge could have been undone between validation and the lease read. const downloadUrl = leased.mergedAt - ? await this.adapter.createDownloadUrl( - `${leased.folderName}/merged`, - directDownloadExpiresAt, - ) + ? await this.adapter.createDownloadUrl(`${leased.folderName}/merged`, urlExpiresAt) : defaultUrl return { diff --git a/routes/twirp/github.actions.results.api.v1.CacheService/CreateCacheEntry.post.ts b/routes/twirp/github.actions.results.api.v1.CacheService/CreateCacheEntry.post.ts index 077b7f46..ff51f393 100644 --- a/routes/twirp/github.actions.results.api.v1.CacheService/CreateCacheEntry.post.ts +++ b/routes/twirp/github.actions.results.api.v1.CacheService/CreateCacheEntry.post.ts @@ -1,6 +1,6 @@ import { z } from 'zod' import { env } from '~/lib/env' -import { getCacheScope } from '~/lib/scope' +import { CACHE_PERMISSION_WRITE, getCacheScope } from '~/lib/scope' import { AlreadyExistsError, getStorage } from '~/lib/storage' import { readTwirpRequest, sendTwirpResponse, TwirpMessage } from '~/lib/twirp' @@ -19,7 +19,7 @@ export default defineEventHandler(async (event) => { ) const storage = await getStorage() - const writeScope = scopes.find((s) => s.Permission >= 2) + const writeScope = scopes.find((s) => (s.Permission & CACHE_PERMISSION_WRITE) !== 0) if (!writeScope) throw createError({ statusCode: 403, message: 'No scope with write permission found' }) diff --git a/routes/twirp/github.actions.results.api.v1.CacheService/FinalizeCacheEntryUpload.ts b/routes/twirp/github.actions.results.api.v1.CacheService/FinalizeCacheEntryUpload.ts index fedca6c7..5dc20338 100644 --- a/routes/twirp/github.actions.results.api.v1.CacheService/FinalizeCacheEntryUpload.ts +++ b/routes/twirp/github.actions.results.api.v1.CacheService/FinalizeCacheEntryUpload.ts @@ -1,6 +1,6 @@ import { z } from 'zod' import { cacheUploadsTotal } from '~/lib/metrics' -import { getCacheScope } from '~/lib/scope' +import { CACHE_PERMISSION_WRITE, getCacheScope } from '~/lib/scope' import { getStorage } from '~/lib/storage' import { readTwirpRequest, sendTwirpResponse, TwirpMessage } from '~/lib/twirp' @@ -19,7 +19,7 @@ export default defineEventHandler(async (event) => { ) const storage = await getStorage() - const writeScope = scopes.find((s) => s.Permission >= 2) + const writeScope = scopes.find((s) => (s.Permission & CACHE_PERMISSION_WRITE) !== 0) if (!writeScope) throw createError({ statusCode: 403, message: 'No scope with write permission found' }) diff --git a/routes/twirp/github.actions.results.api.v1.CacheService/GetCacheEntryDownloadURL.post.ts b/routes/twirp/github.actions.results.api.v1.CacheService/GetCacheEntryDownloadURL.post.ts index feb91d06..faa13f80 100644 --- a/routes/twirp/github.actions.results.api.v1.CacheService/GetCacheEntryDownloadURL.post.ts +++ b/routes/twirp/github.actions.results.api.v1.CacheService/GetCacheEntryDownloadURL.post.ts @@ -1,7 +1,7 @@ -import { map, pipe, prop, sortBy } from 'remeda' +import { filter, map, pipe, prop, sortBy } from 'remeda' import { z } from 'zod' import { cacheRequestsTotal } from '~/lib/metrics' -import { getCacheScope } from '~/lib/scope' +import { CACHE_PERMISSION_READ, getCacheScope } from '~/lib/scope' import { getStorage } from '~/lib/storage' import { readTwirpRequest, sendTwirpResponse, TwirpMessage } from '~/lib/twirp' @@ -13,6 +13,15 @@ const bodySchema = z.object({ export default defineEventHandler(async (event) => { const { scopes, repoId } = await getCacheScope(event) + // Scopes are bit flags; a write-only scope must not be read from. + const readScopes = pipe( + scopes, + filter((s) => (s.Permission & CACHE_PERMISSION_READ) !== 0), + sortBy([prop('Permission'), 'desc']), + map(prop('Scope')), + ) + if (readScopes.length === 0) + throw createError({ statusCode: 403, message: 'No scope with read permission found' }) const { key, restore_keys, version } = await readTwirpRequest( event, @@ -24,7 +33,7 @@ export default defineEventHandler(async (event) => { const match = await storage.getCacheEntryWithDownloadUrl({ keys: [key, ...(restore_keys ?? [])], version, - scopes: pipe(scopes, sortBy([prop('Permission'), 'desc']), map(prop('Scope'))), + scopes: readScopes, repoId, }) diff --git a/tests/cleanup-lifecycle.test.ts b/tests/cleanup-lifecycle.test.ts index 49ddecae..ef9ea506 100644 --- a/tests/cleanup-lifecycle.test.ts +++ b/tests/cleanup-lifecycle.test.ts @@ -5,6 +5,7 @@ import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest' import { getDatabase } from '~/lib/db' import { env } from '~/lib/env' import { Storage } from '~/lib/storage' +import { DIRECT_DOWNLOAD_LEASE_MARGIN_MS } from '~/lib/storage-leases' describe('cleanup lifecycle', () => { beforeAll(() => { @@ -327,7 +328,7 @@ describe('cleanup lifecycle', () => { }, 30_000) test.skipIf(process.env.VITEST_STORAGE_DRIVER !== 's3')( - 'a direct-download reader lease covers the signed URL lifetime', + 'a direct-download reader lease outlives the signed URL, which honors the TTL', async () => { const db = await getDatabase() const storage = await Storage.fromEnv() @@ -387,7 +388,10 @@ describe('cleanup lifecycle', () => { ) expect(signedLifetimeSeconds).toBeGreaterThan(0) - expect(lease.expiresAt - startedAt).toBeGreaterThanOrEqual(signedLifetimeSeconds * 1000) + expect(signedLifetimeSeconds).toBeLessThanOrEqual(env.DIRECT_DOWNLOAD_URL_TTL_SECONDS) + expect(lease.expiresAt - startedAt).toBeGreaterThanOrEqual( + signedLifetimeSeconds * 1000 + DIRECT_DOWNLOAD_LEASE_MARGIN_MS, + ) expect(accessed.lastDownloadedAt).toBeGreaterThanOrEqual(startedAt) } finally { env.ENABLE_DIRECT_DOWNLOADS = directDownloadsEnabled diff --git a/tests/direct-download-auth.test.ts b/tests/direct-download-auth.test.ts new file mode 100644 index 00000000..6d8edd16 --- /dev/null +++ b/tests/direct-download-auth.test.ts @@ -0,0 +1,573 @@ +import type { ResultPromise } from 'execa' +import type { KeyObject } from 'node:crypto' +import type { Server } from 'node:http' +import type { AddressInfo } from 'node:net' + +import { generateKeyPairSync, randomBytes, randomUUID } from 'node:crypto' +import { createServer } from 'node:http' +import { setTimeout as sleep } from 'node:timers/promises' + +import { type } from 'arkenv' +import { execa } from 'execa' +import { exportJWK, SignJWT } from 'jose' +import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest' +import { + assertDirectDownloadsRequireTokenValidation, + envBaseSchema, + parseActionsTokenAudience, +} from '~/lib/schemas' + +// A second server with token validation on, verifying against a local issuer +// whose keys are generated here, so these tests never reach GitHub. +const PORT = 3011 +const BASE_URL = `http://localhost:${PORT}` +const TWIRP = `${BASE_URL}/twirp/github.actions.results.api.v1.CacheService` +const URL_TTL_SECONDS = 120 +const KID = 'test-key' +const AUDIENCE = 'test-audience' + +const trusted = generateKeyPairSync('rsa', { modulusLength: 2048 }) +const untrusted = generateKeyPairSync('rsa', { modulusLength: 2048 }) + +const repoA = `repo-a-${randomUUID()}` +const repoB = `repo-b-${randomUUID()}` +const scopeMarker = `refs/heads/marker-${randomUUID()}` +// Only ever inside a token, so finding it in the logs means decoded claims were logged. +const claimMarker = `claim-marker-${randomUUID()}` + +// holder object instead of bare `let`s (`unicorn/no-top-level-assignment-in-function`) +const issuer: { + url: string + server?: Server + keys: object[] + jwksFetchedAt: number[] +} = { url: '', keys: [], jwksFetchedAt: [] } + +async function publicJwk(key: KeyObject, kid: string) { + return { ...(await exportJWK(key)), kid, alg: 'RS256', use: 'sig' } +} + +async function startIssuer() { + issuer.keys.push(await publicJwk(trusted.publicKey, KID)) + const server = createServer((req, res) => { + res.setHeader('content-type', 'application/json') + if (req.url === '/.well-known/openid-configuration') + return res.end( + JSON.stringify({ issuer: issuer.url, jwks_uri: `${issuer.url}/.well-known/jwks` }), + ) + if (req.url === '/.well-known/jwks') { + issuer.jwksFetchedAt.push(Date.now()) + return res.end(JSON.stringify({ keys: issuer.keys })) + } + // a JWKS endpoint that is down + res.statusCode = req.url === '/broken-jwks' ? 503 : 404 + res.end('{}') + }) + const listening = Promise.withResolvers() + server.listen(0, '127.0.0.1', listening.resolve) + await listening.promise + issuer.server = server + issuer.url = `http://127.0.0.1:${(server.address() as AddressInfo).port}` +} + +function encodeSegment(value: object) { + return Buffer.from(JSON.stringify(value)).toString('base64url') +} + +// Same database and storage as the main test server, so whatever this server +// stores stays authorized for the other suites' orphaned-storage checks. +function serverEnv(overrides: Record) { + return { + ...process.env, + NITRO_PORT: String(PORT), + API_BASE_URL: BASE_URL, + DISABLE_CLEANUP_JOBS: 'true', + ...overrides, + } +} + +type ServerProcess = ResultPromise<{ node: true; ipc: true; all: true; reject: false }> + +function startServer(overrides: Record): ServerProcess { + return execa({ + node: true, + ipc: true, + all: true, + reject: false, + env: serverEnv(overrides), + })`.output/server/index.mjs` +} + +/** Resolves `ready` once the server signals readiness, or `exited` if it dies first. */ +async function readyOrExited(server: ServerProcess) { + return Promise.race([ + server + .getOneMessage({ filter: (message) => message === 'nitro:ready' }) + .then(() => 'ready' as const) + .catch(() => 'exited' as const), + server.then(() => 'exited' as const), + ]) +} + +type Claims = Record + +async function mint( + claims: Claims, + { + key = trusted.privateKey, + kid = KID, + iss = issuer.url, + aud = AUDIENCE, + exp = '10m', + nbf, + }: { + key?: KeyObject + kid?: string + iss?: string + aud?: string | null + exp?: string | number | null + nbf?: string | number + } = {}, +) { + const jwt = new SignJWT({ ...claims, run_id: claimMarker }) + .setProtectedHeader({ alg: 'RS256', kid }) + .setIssuedAt() + if (iss) jwt.setIssuer(iss) + if (aud !== null) jwt.setAudience(aud) + if (exp !== null) jwt.setExpirationTime(exp) + if (nbf !== undefined) jwt.setNotBefore(nbf) + return jwt.sign(key) +} + +function claimsFor(repoId: string, scopes = [{ Scope: scopeMarker, Permission: 3 }]): Claims { + return { ac: JSON.stringify(scopes), repository_id: repoId } +} + +async function twirp(method: string, token: string | undefined, body: object) { + const res = await fetch(`${TWIRP}/${method}`, { + method: 'POST', + headers: { + 'content-type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }), + }, + body: JSON.stringify(body), + }) + const text = await res.text() + let json: Record = {} + try { + json = JSON.parse(text) + } catch {} + return { status: res.status, json } +} + +async function save(token: string, key: string, data: Buffer) { + const created = await twirp('CreateCacheEntry', token, { key, version: 'v1' }) + expect(created.status).toBe(200) + const uploadUrl = created.json.signed_upload_url as string + const part = await fetch(uploadUrl, { method: 'PUT', body: new Uint8Array(data) }) + expect(part.status).toBe(201) + // Keep the signed URL's query and append to it, as Azure Blob clients do. + const blocklistUrl = new URL(uploadUrl) + blocklistUrl.searchParams.set('comp', 'blocklist') + const blocklist = await fetch(blocklistUrl, { method: 'PUT' }) + expect(blocklist.status).toBe(201) + const finalized = await twirp('FinalizeCacheEntryUpload', token, { + key, + version: 'v1', + size_bytes: data.length, + }) + expect(finalized.status).toBe(200) +} + +function lookup(token: string | undefined, key: string) { + return twirp('GetCacheEntryDownloadURL', token, { key, version: 'v1' }) +} + +function parseEnvBase(input: object) { + return envBaseSchema({ API_BASE_URL: 'http://localhost', ...input }) +} + +describe('direct downloads require token validation (config)', () => { + const base = { + ENABLE_DIRECT_DOWNLOADS: true, + SKIP_TOKEN_VALIDATION: true, + UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION: false, + } + + test('refuses direct downloads with token validation skipped', () => { + expect(() => assertDirectDownloadsRequireTokenValidation(base)).toThrow( + 'requires token validation', + ) + }) + + test('allows the combination only with the explicit unsafe override', () => { + expect(() => + assertDirectDownloadsRequireTokenValidation({ + ...base, + UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION: true, + }), + ).not.toThrow() + expect(() => + assertDirectDownloadsRequireTokenValidation({ ...base, SKIP_TOKEN_VALIDATION: false }), + ).not.toThrow() + }) + + test('direct downloads are off by default; the URL TTL defaults to 600 s within 1–3600', () => { + expect(parseEnvBase({})).toMatchObject({ + ENABLE_DIRECT_DOWNLOADS: false, + SKIP_TOKEN_VALIDATION: false, + UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION: false, + DIRECT_DOWNLOAD_URL_TTL_SECONDS: 600, + }) + for (const ttl of [1, 3600]) + expect(parseEnvBase({ DIRECT_DOWNLOAD_URL_TTL_SECONDS: ttl })).toMatchObject({ + DIRECT_DOWNLOAD_URL_TTL_SECONDS: ttl, + }) + for (const ttl of [0, 3601, 1.5]) + expect(parseEnvBase({ DIRECT_DOWNLOAD_URL_TTL_SECONDS: ttl })).toBeInstanceOf(type.errors) + }) + + test('the server refuses to start', { timeout: 30_000 }, async () => { + const server = startServer({ ENABLE_DIRECT_DOWNLOADS: 'true', SKIP_TOKEN_VALIDATION: 'true' }) + if ((await readyOrExited(server)) === 'ready') { + server.kill() + throw new Error('server started with direct downloads and skipped token validation') + } + const result = await server + expect(result.exitCode).not.toBe(0) + expect(result.all).toContain('ENABLE_DIRECT_DOWNLOADS=true requires token validation') + }) +}) + +describe('token audience (config)', () => { + test('unset skips the audience check; a list is trimmed', () => { + expect(parseActionsTokenAudience(undefined)).toBeUndefined() + expect(parseActionsTokenAudience(' a, b ,')).toEqual(['a', 'b']) + }) + + test.each(['', ' ', ',', ' , ,'])('refuses %j, which lists no audience', (value) => { + expect(() => parseActionsTokenAudience(value)).toThrow('lists no audience') + }) + + test('the server refuses to start with an empty audience list', { timeout: 30_000 }, async () => { + const server = startServer({ ACTIONS_TOKEN_AUDIENCE: ' , ' }) + if ((await readyOrExited(server)) === 'ready') { + server.kill() + throw new Error('server started with an empty ACTIONS_TOKEN_AUDIENCE') + } + const result = await server + expect(result.exitCode).not.toBe(0) + expect(result.all).toContain('ACTIONS_TOKEN_AUDIENCE is set but lists no audience') + }) +}) + +describe('token validation and direct downloads', () => { + let server: ServerProcess + let logs = '' + const issuedTokens: string[] = [] + const track = async (token: Promise) => { + const value = await token + issuedTokens.push(value) + return value + } + + beforeAll(async () => { + await startIssuer() + server = startServer({ + SKIP_TOKEN_VALIDATION: 'false', + ACTIONS_TOKEN_ISSUER: issuer.url, + ACTIONS_TOKEN_AUDIENCE: `other-audience, ${AUDIENCE}`, + ENABLE_DIRECT_DOWNLOADS: 'true', + EAGER_MERGE: 'true', + DIRECT_DOWNLOAD_URL_TTL_SECONDS: String(URL_TTL_SECONDS), + }) + server.all?.on('data', (chunk: Buffer) => { + logs += chunk.toString() + }) + if ((await readyOrExited(server)) !== 'ready') + throw new Error(`server failed to start:\n${logs}`) + }, 60_000) + + afterAll(async () => { + server?.kill() + await server + const closed = Promise.withResolvers() + issuer.server?.close(() => closed.resolve()) + await closed.promise + }) + + test('accepts a token signed by the issuer', async () => { + const token = await track(mint(claimsFor(repoA))) + const res = await lookup(token, randomUUID()) + expect(res.status).toBe(200) + expect(res.json.ok).toBe(false) + }) + + test.each([ + ['missing bearer', async () => undefined], + [ + 'signature from an untrusted key', + () => mint(claimsFor(repoA), { key: untrusted.privateKey }), + ], + ['expired', () => mint(claimsFor(repoA), { exp: Math.floor(Date.now() / 1000) - 60 })], + ['not yet valid', () => mint(claimsFor(repoA), { nbf: Math.floor(Date.now() / 1000) + 600 })], + ['wrong issuer', () => mint(claimsFor(repoA), { iss: 'https://issuer.invalid' })], + ['no expiry', () => mint(claimsFor(repoA), { exp: null })], + ['wrong audience', () => mint(claimsFor(repoA), { aud: 'someone-else' })], + ['no audience', () => mint(claimsFor(repoA), { aud: null })], + ['no cache scopes', () => mint({ repository_id: repoA })], + [ + 'no repository id', + () => mint({ ac: JSON.stringify([{ Scope: scopeMarker, Permission: 3 }]) }), + ], + ['malformed cache scopes', () => mint({ ac: '[{"Scope":1}]', repository_id: repoA })], + [ + 'unsigned token', + async () => { + const claims = { + ...claimsFor(repoA), + iss: issuer.url, + exp: Math.floor(Date.now() / 1000) + 600, + } + return `${encodeSegment({ alg: 'none' })}.${encodeSegment(claims)}.` + }, + ], + ])('rejects %s with 401 on lookup, create and finalize', async (_, makeToken) => { + const token = await makeToken() + if (token) issuedTokens.push(token) + const key = randomUUID() + const res = await lookup(token, key) + expect(res.status).toBe(401) + expect(res.json.signed_download_url).toBeUndefined() + + const create = await twirp('CreateCacheEntry', token, { key, version: 'v1' }) + expect(create.status).toBe(401) + expect(create.json.signed_upload_url).toBeUndefined() + const finalize = await twirp('FinalizeCacheEntryUpload', token, { key, version: 'v1' }) + expect(finalize.status).toBe(401) + }) + + test( + 'a forged token gets no URL for an existing entry, direct or proxied', + { timeout: 30_000 }, + async () => { + const key = `forged-${randomUUID()}` + const owner = await track(mint(claimsFor(repoA))) + await save(owner, key, randomBytes(1024)) + + const forged = await mint(claimsFor(repoA), { key: untrusted.privateKey }) + issuedTokens.push(forged) + const res = await lookup(forged, key) + expect(res.status).toBe(401) + expect(JSON.stringify(res.json)).not.toContain('/download/') + expect(JSON.stringify(res.json)).not.toContain('X-Amz-') + }, + ) + + test( + 'another repository cannot match the entry, even by prefix', + { timeout: 30_000 }, + async () => { + const key = `cross-repo-${randomUUID()}` + await save(await track(mint(claimsFor(repoA))), key, randomBytes(1024)) + + const other = await track(mint(claimsFor(repoB))) + const exact = await lookup(other, key) + expect(exact.json.ok).toBe(false) + const prefix = await twirp('GetCacheEntryDownloadURL', other, { + key: 'no-such-key', + restore_keys: [key.slice(0, 12)], + version: 'v1', + }) + expect(prefix.json.ok).toBe(false) + + const owner = await lookup(await track(mint(claimsFor(repoA))), key) + expect(owner.json.ok).toBe(true) + }, + ) + + test('scope permissions are bit flags', { timeout: 30_000 }, async () => { + const key = `scopes-${randomUUID()}` + await save(await track(mint(claimsFor(repoA))), key, randomBytes(1024)) + + const readOnly = await track(mint(claimsFor(repoA, [{ Scope: scopeMarker, Permission: 1 }]))) + const read = await lookup(readOnly, key) + expect(read.json.ok).toBe(true) + const write = await twirp('CreateCacheEntry', readOnly, { key: randomUUID(), version: 'v1' }) + expect(write.status).toBe(403) + + const writeOnly = await track(mint(claimsFor(repoA, [{ Scope: scopeMarker, Permission: 2 }]))) + const writeOnlyRead = await lookup(writeOnly, key) + expect(writeOnlyRead.status).toBe(403) + + const otherScope = await track( + mint(claimsFor(repoA, [{ Scope: `refs/heads/other-${randomUUID()}`, Permission: 3 }])), + ) + const otherScopeRead = await lookup(otherScope, key) + expect(otherScopeRead.json.ok).toBe(false) + }) + + test( + 'a fork pull request writes only to its own ref and reads the base scopes it lists', + { timeout: 30_000 }, + async () => { + const baseRef = `refs/heads/base-${randomUUID()}` + const prRef = `refs/pull/${randomUUID()}/merge` + const baseKey = `base-${randomUUID()}` + await save( + await track(mint(claimsFor(repoA, [{ Scope: baseRef, Permission: 3 }]))), + baseKey, + randomBytes(1024), + ) + + // the read-only base scope is listed first; writes must still land on the PR ref + const pr = await track( + mint( + claimsFor(repoA, [ + { Scope: baseRef, Permission: 1 }, + { Scope: prRef, Permission: 3 }, + ]), + ), + ) + const baseFromPr = await lookup(pr, baseKey) + expect(baseFromPr.json.ok).toBe(true) + + const prKey = `pr-${randomUUID()}` + await save(pr, prKey, randomBytes(1024)) + const prFromPr = await lookup(pr, prKey) + expect(prFromPr.json.ok).toBe(true) + const baseOnly = await track(mint(claimsFor(repoA, [{ Scope: baseRef, Permission: 3 }]))) + const prFromBase = await lookup(baseOnly, prKey) + expect(prFromBase.json.ok).toBe(false) + + // a read-only token cannot finalize an upload either + const readOnly = await track(mint(claimsFor(repoA, [{ Scope: prRef, Permission: 1 }]))) + const finalize = await twirp('FinalizeCacheEntryUpload', readOnly, { + key: prKey, + version: 'v1', + size_bytes: 1024, + }) + expect(finalize.status).toBe(403) + }, + ) + + test('accepts a key rotated in by the issuer (new kid)', { timeout: 60_000 }, async () => { + const rotated = generateKeyPairSync('rsa', { modulusLength: 2048 }) + const rotatedKid = 'rotated-key' + const token = await track(mint(claimsFor(repoA), { key: rotated.privateKey, kid: rotatedKid })) + issuer.keys.push(await publicJwk(rotated.publicKey, rotatedKid)) + + // A real wait: the server process refetches the JWKS for an unknown kid at + // most once per 30 s (jose's cooldown), and fake timers can't reach into it. + const fetches = issuer.jwksFetchedAt.length + expect(fetches).toBeGreaterThan(0) + await sleep(Math.max(0, issuer.jwksFetchedAt.at(-1)! + 31_000 - Date.now())) + + const res = await lookup(token, randomUUID()) + expect(res.status).toBe(200) + expect(issuer.jwksFetchedAt.length).toBeGreaterThan(fetches) + }) + + test('fails closed with 401 when the JWKS cannot be fetched', { timeout: 30_000 }, async () => { + const port = PORT + 1 + const broken = startServer({ + NITRO_PORT: String(port), + API_BASE_URL: `http://localhost:${port}`, + SKIP_TOKEN_VALIDATION: 'false', + ACTIONS_TOKEN_ISSUER: issuer.url, + ACTIONS_TOKEN_JWKS_URL: `${issuer.url}/broken-jwks`, + }) + let brokenLogs = '' + broken.all?.on('data', (chunk: Buffer) => { + brokenLogs += chunk.toString() + }) + try { + if ((await readyOrExited(broken)) !== 'ready') + throw new Error(`server failed to start:\n${brokenLogs}`) + const token = await track(mint(claimsFor(repoA))) + const res = await fetch( + `http://localhost:${port}/twirp/github.actions.results.api.v1.CacheService/GetCacheEntryDownloadURL`, + { + method: 'POST', + headers: { 'content-type': 'application/json', 'authorization': `Bearer ${token}` }, + body: JSON.stringify({ key: randomUUID(), version: 'v1' }), + }, + ) + expect(res.status).toBe(401) + expect(brokenLogs).not.toContain(token) + expect(brokenLogs).not.toContain(claimMarker) + } finally { + broken.kill() + await broken + } + }) + + test( + 'an authorized lookup of a merged entry gets a URL that expires within the TTL', + { timeout: 60_000 }, + async () => { + const key = `direct-${randomUUID()}` + const data = randomBytes(64 * 1024) + const token = await track(mint(claimsFor(repoA))) + await save(token, key, data) + + const signedUrl = async () => { + const res = await lookup(token, key) + expect(res.json.ok).toBe(true) + return new URL(res.json.signed_download_url as string) + } + + if (process.env.STORAGE_DRIVER === 'filesystem') { + // No signed URLs on the filesystem driver: always the proxied route. + const url = await signedUrl() + expect(url.origin).toBe(BASE_URL) + expect(url.pathname).toMatch(/^\/download\//) + return + } + + // Eager Merge runs after finalize; until it lands the lookup is proxied. + const url = await vi.waitFor( + async () => { + const candidate = await signedUrl() + expect(candidate.origin).not.toBe(BASE_URL) + return candidate + }, + { timeout: 30_000, interval: 200 }, + ) + // S3 signs a relative lifetime; GCS (V2 signing) an absolute epoch second, + // Azure (SAS) an absolute ISO time + const now = Date.now() + const gcsExpiresAt = url.searchParams.get('Expires') + const azureExpiresAt = url.searchParams.get('se') + const expires = gcsExpiresAt + ? Number(gcsExpiresAt) - Math.floor(now / 1000) + : azureExpiresAt + ? Math.floor((Date.parse(azureExpiresAt) - now) / 1000) + : Number(url.searchParams.get('X-Amz-Expires')) + expect(expires).toBeGreaterThan(0) + expect(expires).toBeLessThanOrEqual(URL_TTL_SECONDS) + + const download = await fetch(url) + const body = Buffer.from(await download.arrayBuffer()) + expect(body.equals(data)).toBe(true) + }, + ) + + test('logs contain neither tokens, claims nor signed URLs', () => { + expect(issuedTokens.length).toBeGreaterThan(0) + for (const token of issuedTokens) { + expect(logs).not.toContain(token) + // nor its signature on its own + const signature = token.slice(token.lastIndexOf('.') + 1) + if (signature) expect(logs).not.toContain(signature) + } + // Repository ids, scopes and keys do appear in DEBUG query logs; the claims + // jose decodes from a rejected token must not. + expect(logs).not.toContain(claimMarker) + expect(logs).not.toContain('X-Amz-Signature') + expect(logs).not.toContain('GoogleAccessId=') + expect(logs).not.toMatch(/[?&]sig=/) + // rejections are still diagnosable + expect(logs).toContain('ERR_JWT_EXPIRED') + }) +}) diff --git a/tests/setup.ts b/tests/setup.ts index 5ca471ec..c745f700 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -7,6 +7,7 @@ import type { Server } from 'node:http' import type { StartedTestContainer } from 'testcontainers' import type { Env, envBaseSchema, envDbDriverSchema, envStorageDriverSchema } from '~/lib/schemas' +import { generateKeyPairSync } from 'node:crypto' import fs from 'node:fs/promises' import path from 'node:path' @@ -43,6 +44,8 @@ const TESTING_ENV_BASE = { | 'CACHE_FILESYSTEM_MAX_USAGE_PERCENT' | 'ORPHANED_STORAGE_GRACE_PERIOD_HOURS' | 'ENABLE_DIRECT_DOWNLOADS' + | 'DIRECT_DOWNLOAD_URL_TTL_SECONDS' + | 'UNSAFE_ALLOW_DIRECT_DOWNLOADS_WITHOUT_TOKEN_VALIDATION' | 'EAGER_MERGE' | 'BENCHMARK' | 'SKIP_TOKEN_VALIDATION' @@ -93,7 +96,9 @@ const TESTING_ENV_BY_STORAGE_DRIVER = { STORAGE_DRIVER: 'gcs', STORAGE_GCS_BUCKET: 'vitest', STORAGE_GCS_ENDPOINT: 'http://localhost:9000', - STORAGE_GCS_SERVICE_ACCOUNT_KEY: 'tests/gcs-service-account-key.json', + // generated in `setup`: fake-gcs-server needs no credentials, but signing a + // direct-download URL does + STORAGE_GCS_SERVICE_ACCOUNT_KEY: `${TEST_TEMP_DIR}/gcs-service-account-key.json`, }, azblob: { STORAGE_DRIVER: 'azblob', @@ -132,6 +137,20 @@ export async function setup() { recursive: true, }) await fs.mkdir(TEST_TEMP_DIR, { recursive: true }) + if (env.VITEST_STORAGE_DRIVER === 'gcs') { + const { privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048 }) + await fs.writeFile( + TESTING_ENV_BY_STORAGE_DRIVER.gcs.STORAGE_GCS_SERVICE_ACCOUNT_KEY, + JSON.stringify({ + type: 'service_account', + project_id: 'vitest', + private_key_id: 'vitest', + private_key: privateKey.export({ type: 'pkcs8', format: 'pem' }), + client_email: 'vitest@vitest.iam.gserviceaccount.com', + token_uri: `${TESTING_ENV_BY_STORAGE_DRIVER.gcs.STORAGE_GCS_ENDPOINT}/token`, + }), + ) + } // eslint-disable-next-line no-console console.log('Starting test containers for', env.VITEST_DB_DRIVER, env.VITEST_STORAGE_DRIVER) @@ -210,7 +229,8 @@ export async function setup() { .withEntrypoint(['sh']) .withCommand([ `-c`, - `mkdir -p /data/${env.STORAGE_GCS_BUCKET} && /bin/fake-gcs-server -scheme http -port 9000 -data /data`, + // `-public-host` routes path-style object GETs, the shape of a signed URL + `mkdir -p /data/${env.STORAGE_GCS_BUCKET} && /bin/fake-gcs-server -scheme http -port 9000 -public-host ${new URL(env.STORAGE_GCS_ENDPOINT).host} -data /data`, ]) .withExposedPorts({ container: 9000, diff --git a/tests/write-authorization.test.ts b/tests/write-authorization.test.ts new file mode 100644 index 00000000..0f840b0b --- /dev/null +++ b/tests/write-authorization.test.ts @@ -0,0 +1,93 @@ +import { Buffer } from 'node:buffer' +import crypto from 'node:crypto' + +import { SignJWT } from 'jose' +import { describe, expect, test } from 'vitest' + +const TWIRP_BASE = 'http://localhost:3000/twirp/github.actions.results.api.v1.CacheService' + +function signToken({ + repoId = '123', + scopes = [{ Scope: 'refs/heads/main', Permission: 3 }], +}: { repoId?: string; scopes?: { Scope: string; Permission: number }[] } = {}) { + return new SignJWT({ ac: JSON.stringify(scopes), repository_id: repoId }) + .setProtectedHeader({ alg: 'HS256' }) + .sign(crypto.createSecretKey('mock-secret-key', 'ascii')) +} + +async function twirp(method: string, token: string, body: object) { + const response = await fetch(`${TWIRP_BASE}/${method}`, { + method: 'POST', + headers: { 'authorization': `Bearer ${token}`, 'content-type': 'application/json' }, + body: JSON.stringify(body), + }) + return { status: response.status, body: (await response.json()) as T } +} + +async function createUpload(token: string, key: string) { + const { status, body } = await twirp<{ ok: boolean; signed_upload_url: string }>( + 'CreateCacheEntry', + token, + { key, version: 'v1' }, + ) + expect(status).toBe(200) + expect(body.ok).toBe(true) + return body.signed_upload_url +} + +async function save(token: string, key: string, data: Uint8Array) { + const url = await createUpload(token, key) + const put = await fetch(url, { method: 'PUT', body: data as BodyInit }) + expect(put.status).toBe(201) + const { status } = await twirp('FinalizeCacheEntryUpload', token, { key, version: 'v1' }) + expect(status).toBe(200) +} + +async function restore(token: string, key: string) { + const { body } = await twirp<{ ok: boolean; signed_download_url?: string }>( + 'GetCacheEntryDownloadURL', + token, + { key, version: 'v1' }, + ) + if (!body.ok || !body.signed_download_url) return + const response = await fetch(body.signed_download_url) + expect(response.status).toBe(200) + return Buffer.from(await response.arrayBuffer()) +} + +describe('write path authorization', () => { + test('another repository cannot replace or read an entry with the same key and scope', async () => { + const tokenA = await signToken({ repoId: 'write-auth-owner' }) + const tokenB = await signToken({ repoId: 'write-auth-other' }) + const key = `write-auth-${crypto.randomUUID()}` + const original = crypto.randomBytes(64) + + await save(tokenA, key, original) + expect(await restore(tokenB, key)).toBeUndefined() + + await save(tokenB, key, crypto.randomBytes(64)) + const restored = await restore(tokenA, key) + expect(restored?.equals(original)).toBe(true) + }) + + test('a pull request ref write scope cannot replace a base branch entry', async () => { + const base = await signToken({ repoId: 'write-auth-fork' }) + const pullRequest = await signToken({ + repoId: 'write-auth-fork', + scopes: [ + { Scope: 'refs/pull/1/merge', Permission: 3 }, + { Scope: 'refs/heads/main', Permission: 1 }, + ], + }) + const key = `write-auth-${crypto.randomUUID()}` + const original = crypto.randomBytes(64) + await save(base, key, original) + + // the pull request restores the base entry but saves into its own ref + const restored = await restore(pullRequest, key) + expect(restored?.equals(original)).toBe(true) + await save(pullRequest, key, crypto.randomBytes(64)) + const baseRestored = await restore(base, key) + expect(baseRestored?.equals(original)).toBe(true) + }) +})