-
Notifications
You must be signed in to change notification settings - Fork 0
test(coverage): measure exchange success path #405
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
4 commits
Select commit
Hold shift + click to select a range
bb971bd
test(coverage): require exchange success path measurement
seonghobae 645f9d2
quality(coverage): measure exchange success path
seonghobae 818df0c
test(coverage): exercise alternate exchange success claims
seonghobae 5f531ee
fix(coverage): remove unreachable exchange workflow fallback
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
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,144 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import worker, { type Env } from "../src/index"; | ||
|
|
||
| const configuredRef = | ||
| "ContextualWisdomLab/.github/.github/workflows/noema-review.yml@refs/heads/main"; | ||
|
|
||
| const env: Env = { | ||
| ALLOWED_ISSUER: "https://token.actions.githubusercontent.com", | ||
| ALLOWED_AUDIENCE: "cwl-noema-review", | ||
| ALLOWED_REPOSITORY_OWNER: "ContextualWisdomLab", | ||
| ALLOWED_WORKFLOW_REPOSITORY: "ContextualWisdomLab/.github", | ||
| ALLOWED_WORKFLOW_REF_PREFIX: configuredRef, | ||
| GITHUB_API_BASE: "https://api.github.com", | ||
| GITHUB_APP_ID: "1", | ||
| GITHUB_APP_PRIVATE_KEY_PEM: "unused", | ||
| NOEMA_RATE_LIMIT_PER_MINUTE: "1000", | ||
| }; | ||
|
|
||
| function encodeSegment(value: unknown): string { | ||
| return Buffer.from(JSON.stringify(value)).toString("base64url"); | ||
| } | ||
|
|
||
| function encodeBytes(bytes: ArrayBuffer): string { | ||
| return Buffer.from(bytes).toString("base64url"); | ||
| } | ||
|
|
||
| function pemFromPkcs8(pkcs8: ArrayBuffer): string { | ||
| const base64 = Buffer.from(pkcs8).toString("base64"); | ||
| const lines = base64.match(/.{1,64}/g)?.join("\n") ?? base64; | ||
| return `-----BEGIN PRIVATE KEY-----\n${lines}\n-----END PRIVATE KEY-----`; | ||
| } | ||
|
|
||
| async function createSignedJwt(payload: Record<string, unknown>) { | ||
| const keyPair = await crypto.subtle.generateKey( | ||
| { | ||
| name: "RSASSA-PKCS1-v1_5", | ||
| modulusLength: 2048, | ||
| publicExponent: new Uint8Array([1, 0, 1]), | ||
| hash: "SHA-256", | ||
| }, | ||
| true, | ||
| ["sign", "verify"], | ||
| ); | ||
| const kid = `exchange-success-${crypto.randomUUID()}`; | ||
| const header = encodeSegment({ alg: "RS256", kid, typ: "JWT" }); | ||
| const body = encodeSegment(payload); | ||
| const signature = await crypto.subtle.sign( | ||
| "RSASSA-PKCS1-v1_5", | ||
| keyPair.privateKey, | ||
| new TextEncoder().encode(`${header}.${body}`), | ||
| ); | ||
| const publicJwk = await crypto.subtle.exportKey("jwk", keyPair.publicKey); | ||
| return { | ||
| token: `${header}.${body}.${encodeBytes(signature)}`, | ||
| jwk: { ...publicJwk, kid, kty: "RSA" }, | ||
| }; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("exchange success-path coverage through the public worker", () => { | ||
| it("accepts workflow_ref-only claims without inventing an OIDC subject", async () => { | ||
| const now = Math.floor(Date.now() / 1000); | ||
| const { token: oidcToken, jwk } = await createSignedJwt({ | ||
| iss: env.ALLOWED_ISSUER, | ||
| aud: env.ALLOWED_AUDIENCE, | ||
| repository_owner: env.ALLOWED_REPOSITORY_OWNER, | ||
| repository: "ContextualWisdomLab/.github", | ||
| workflow_ref: configuredRef, | ||
| exp: now + 300, | ||
| nbf: now - 30, | ||
| iat: now - 30, | ||
| }); | ||
| const appKeyPair = await crypto.subtle.generateKey( | ||
| { | ||
| name: "RSASSA-PKCS1-v1_5", | ||
| modulusLength: 2048, | ||
| publicExponent: new Uint8Array([1, 0, 1]), | ||
| hash: "SHA-256", | ||
| }, | ||
| true, | ||
| ["sign", "verify"], | ||
| ); | ||
| const appPrivateKey = pemFromPkcs8( | ||
| await crypto.subtle.exportKey("pkcs8", appKeyPair.privateKey), | ||
| ); | ||
| const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined); | ||
|
|
||
| vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => { | ||
| const url = String(input); | ||
| if (url === "https://token.actions.githubusercontent.com/.well-known/openid-configuration") { | ||
| return Response.json({ | ||
| jwks_uri: "https://token.actions.githubusercontent.com/.well-known/jwks", | ||
| }); | ||
| } | ||
| if (url === "https://token.actions.githubusercontent.com/.well-known/jwks") { | ||
| return Response.json({ keys: [jwk] }); | ||
| } | ||
| if (url === "https://api.github.com/repos/ContextualWisdomLab/noema/installation") { | ||
| return Response.json({ id: 12345 }); | ||
| } | ||
| if (url === "https://api.github.com/app/installations/12345/access_tokens") { | ||
| return Response.json({ | ||
| token: "ghs_exchange_success_token", | ||
| expires_at: "2030-01-01T00:00:00Z", | ||
| }); | ||
| } | ||
| return new Response("not found", { status: 404 }); | ||
| }); | ||
|
|
||
| const response = await worker.fetch( | ||
| new Request("https://noema.example/exchange", { | ||
| method: "POST", | ||
| headers: { | ||
| authorization: `Bearer ${oidcToken}`, | ||
| "content-type": "application/json", | ||
| "cf-connecting-ip": "203.0.113.205", | ||
| }, | ||
| body: JSON.stringify({ target_repository: "ContextualWisdomLab/noema" }), | ||
| }), | ||
| { | ||
| ...env, | ||
| GITHUB_APP_PRIVATE_KEY_PEM: appPrivateKey, | ||
| }, | ||
| ); | ||
|
|
||
| expect(response.status).toBe(200); | ||
| await expect(response.json()).resolves.toMatchObject({ | ||
| ok: true, | ||
| data: { | ||
| token: "ghs_exchange_success_token", | ||
| repository: "ContextualWisdomLab/noema", | ||
| workflow_ref: configuredRef, | ||
| token_expires_at: "2030-01-01T00:00:00Z", | ||
| }, | ||
| }); | ||
| const logOutput = logSpy.mock.calls.flat().join("\n"); | ||
| expect(logOutput).not.toContain("ghs_exchange_success_token"); | ||
| expect(logOutput).not.toContain(oidcToken); | ||
| expect(logOutput).not.toContain("oidc_sub"); | ||
| }); | ||
| }); | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
성공 응답의 프로토콜 계약과 로그 스키마를 검증하세요.
현재 테스트는 상태 코드와 일부 본문만 확인합니다.
trace_id,cache-control: no-store,x-content-type-options: nosniff,x-trace-id,x-latency-ms를 검증하세요.console.log호출이 정확히 한 번이고 유효한 HTTP/KPI JSON 레코드인지도 검증하세요. 이 계약이 회귀해도 현재 테스트는 통과합니다.As per coding guidelines,
test/**/*.ts는 “Add or update regression tests for security and API behavior changes; use Vitest and preserve assertions covering token non-disclosure and protocol contracts.”를 요구합니다.🤖 Prompt for AI Agents
Source: Coding guidelines