From afa0183ba4b668023ce21501f30002a2ea120be3 Mon Sep 17 00:00:00 2001 From: prdai Date: Thu, 19 Nov 2026 08:34:36 +0530 Subject: [PATCH 1/3] feat(verifier): allow each run to attest only once Co-authored-by: Codex --- docs/e2e-secure-rads.txt | 2 +- secure/verifier-service/README.md | 2 +- secure/verifier-service/serverless.yml | 20 +++++++++ .../verifier-service/src/handlers/attest.ts | 2 + secure/verifier-service/src/lib/attestFlow.ts | 6 +++ .../src/lib/runAttestation.ts | 42 +++++++++++++++++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 secure/verifier-service/src/lib/runAttestation.ts diff --git a/docs/e2e-secure-rads.txt b/docs/e2e-secure-rads.txt index d88fe76..7525373 100644 --- a/docs/e2e-secure-rads.txt +++ b/docs/e2e-secure-rads.txt @@ -71,7 +71,7 @@ Input: 4 SEED-ENTROPY from NSM // no AWS cost 5 emit unit-ready // no token yet, auth none 6 challenge ← REQUEST-CHALLENGE(Verifier, a, runId) // single-use nonce, verifier API GW/Lambda and DynamoDB metered -7 token ← ATTEST(Verifier, runId, challenge) // NSM nonce ← challenge; verifier API GW/Lambda and DynamoDB metered +7 token ← ATTEST(Verifier, runId, challenge) // NSM nonce ← challenge; claims the run so only one attestation succeeds; verifier API GW/Lambda and DynamoDB metered 8 emit attested 9 records ← FETCH-PDM(a, token, limit ← n) 10 cost apigateway:request + lambda:invocation + lambda:compute + dynamodb:read-capacity // PDM /records diff --git a/secure/verifier-service/README.md b/secure/verifier-service/README.md index b66baf2..7bcf245 100644 --- a/secure/verifier-service/README.md +++ b/secure/verifier-service/README.md @@ -6,7 +6,7 @@ Serverless Framework v4, TypeScript. Receives `AlgorithmId` + PCR values from re - `POST /algorithms` (mTLS domain) - registry-service registers `AlgorithmId` -> PCR0/1/2 at build time. - `POST /challenge` (public domain) - sce-unit asks for a single-use nonce before attesting. The row is stored hashed in `AttestationChallengeTable` with a 60 s TTL and is bound to the `algorithmId` and `runId` in the request. -- `POST /attest` (public domain) - sce-unit submits a real Nitro attestation document (base64 COSE_Sign1 bytes) + `algorithmId` + `runId` + the `challenge` it was issued. The document's `nonce` must equal the challenge, and the challenge is consumed atomically before the PCR check, so a captured document cannot be replayed. Verified against the pinned AWS Nitro root cert chain and the registered PCRs; on success, issues a signed access JWT + opaque refresh token (persisted in `AttestationSessionTable`, including `RunId`). +- `POST /attest` (public domain) - sce-unit submits a real Nitro attestation document (base64 COSE_Sign1 bytes) + `algorithmId` + `runId` + the `challenge` it was issued. The document's `nonce` must equal the challenge, and the challenge is consumed atomically before the PCR check, so a captured document cannot be replayed. After the PCR check, the run is claimed in `RunAttestationTable` with a conditional put, so only the first attestation for a `runId` succeeds; later ones get 409. Verified against the pinned AWS Nitro root cert chain and the registered PCRs; on success, issues a signed access JWT + opaque refresh token (persisted in `AttestationSessionTable`, including `RunId`). - `POST /refresh` (public domain) - exchanges a still-valid refresh token plus its `runId` for a new access JWT, without requiring a fresh attestation document. The `runId` must match the one stored on the session. - `GET /.well-known/jwks.json` (public domain) - the ML-DSA-87 public key downstream services use to verify access JWTs locally. Public by definition, no auth. diff --git a/secure/verifier-service/serverless.yml b/secure/verifier-service/serverless.yml index d599a35..447d8d4 100644 --- a/secure/verifier-service/serverless.yml +++ b/secure/verifier-service/serverless.yml @@ -37,6 +37,9 @@ provider: - dynamodb:PutItem - dynamodb:UpdateItem Resource: !GetAtt AttestationChallengeTable.Arn + - Effect: Allow + Action: dynamodb:PutItem + Resource: !GetAtt RunAttestationTable.Arn - Effect: Allow Action: ssm:GetParameter Resource: !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/secure-rads/verifier-service/jwt-signing-key" @@ -44,6 +47,7 @@ provider: ALGORITHM_PCR_TABLE: !Ref AlgorithmPCRTable ATTESTATION_SESSION_TABLE: !Ref AttestationSessionTable ATTESTATION_CHALLENGE_TABLE: !Ref AttestationChallengeTable + RUN_ATTESTATION_TABLE: !Ref RunAttestationTable JWT_SIGNING_KEY_PARAM: /secure-rads/verifier-service/jwt-signing-key COST_HOST: ${env:COST_HOST_SECURE_RADS} @@ -128,6 +132,22 @@ resources: AttributeName: ExpiresAt Enabled: true + # One row per run that has attested. The conditional put is what makes a run single-use: concurrency-safe + # first writer wins, and every later /attest for the same run id is rejected. + RunAttestationTable: + Type: AWS::DynamoDB::Table + Properties: + BillingMode: PAY_PER_REQUEST + AttributeDefinitions: + - AttributeName: RunId + AttributeType: S + KeySchema: + - AttributeName: RunId + KeyType: HASH + TimeToLiveSpecification: + AttributeName: ExpiresAt + Enabled: true + # PEM bundle containing registry-service's client cert public cert # (csav/registry-service/tls/client.crt) - upload it here before deploy. TruststoreBucket: diff --git a/secure/verifier-service/src/handlers/attest.ts b/secure/verifier-service/src/handlers/attest.ts index 1a9b9a3..19bee71 100644 --- a/secure/verifier-service/src/handlers/attest.ts +++ b/secure/verifier-service/src/handlers/attest.ts @@ -6,6 +6,7 @@ import type { APIGatewayProxyHandlerV2 } from "aws-lambda"; import { verifyAttestationDocument } from "../lib/attestation.js"; import { type AttestBody, attestRequest } from "../lib/attestFlow.js"; import { consumeChallenge } from "../lib/challenge.js"; +import { claimRunAttestation } from "../lib/runAttestation.js"; import { issueTokenPair } from "../lib/tokens.js"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); @@ -66,6 +67,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { registered: registered.Item as { PCR0: string; PCR1: string; PCR2: string }, verifyDocument: verifyAttestationDocument, consumeChallenge, + claimRunAttestation, issueTokens: issueTokenPair, }); } finally { diff --git a/secure/verifier-service/src/lib/attestFlow.ts b/secure/verifier-service/src/lib/attestFlow.ts index f5e08e1..bbb97d9 100644 --- a/secure/verifier-service/src/lib/attestFlow.ts +++ b/secure/verifier-service/src/lib/attestFlow.ts @@ -18,6 +18,7 @@ export interface AttestDeps { registered: RegisteredPcrs; verifyDocument(raw: Buffer): AttestationDocument; consumeChallenge(challenge: string, algorithmId: string, runId: string): Promise; + claimRunAttestation(algorithmId: string, runId: string): Promise; issueTokens(algorithmId: string, runId: string): Promise; } @@ -62,6 +63,11 @@ export async function attestRequest(body: AttestBody, deps: AttestDeps): Promise return { statusCode: 401, body: "PCR mismatch: enclave measurements do not match registered algorithm" }; } + // A run attests once. Claiming after the measurement check keeps a wrong-image request from burning a run. + if (!(await deps.claimRunAttestation(body.algorithmId, body.runId))) { + return { statusCode: 409, body: "run already attested" }; + } + const tokens = await deps.issueTokens(body.algorithmId, body.runId); return { statusCode: 200, body: JSON.stringify(tokens) }; } diff --git a/secure/verifier-service/src/lib/runAttestation.ts b/secure/verifier-service/src/lib/runAttestation.ts new file mode 100644 index 0000000..53de988 --- /dev/null +++ b/secure/verifier-service/src/lib/runAttestation.ts @@ -0,0 +1,42 @@ +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; +import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; +import { CostPhase, costEvent } from "@srads/common/cost"; +import { requiredEnv } from "@srads/common/env"; + +const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); +const tableName = requiredEnv("RUN_ATTESTATION_TABLE"); + +// A run attests within minutes of launch; a day bounds the table without reopening a completed run. +const RUN_ATTESTATION_TTL_SECONDS = 24 * 60 * 60; + +// First writer wins. The conditional put makes two concurrent attestations for one run race safely: one mints +// tokens, the other gets ConditionalCheckFailedException and is rejected. +export async function claimRunAttestation(algorithmId: string, runId: string): Promise { + try { + const write = await ddb.send( + new PutCommand({ + TableName: tableName, + Item: { + RunId: runId, + AlgorithmId: algorithmId, + AttestedAt: new Date().toISOString(), + ExpiresAt: Math.floor(Date.now() / 1000) + RUN_ATTESTATION_TTL_SECONDS, + }, + ConditionExpression: "attribute_not_exists(RunId)", + ReturnConsumedCapacity: "TOTAL", + }), + ); + await costEvent( + algorithmId, + runId, + CostPhase.Execution, + "dynamodb", + "write-capacity", + write.ConsumedCapacity?.CapacityUnits ?? 0, + ); + return true; + } catch (err) { + if ((err as Error).name === "ConditionalCheckFailedException") return false; + throw err; + } +} From 0759f1edae37c963b9107c793edfcd9822ec5274 Mon Sep 17 00:00:00 2001 From: prdai Date: Thu, 19 Nov 2026 09:38:17 +0530 Subject: [PATCH 2/3] fix(verifier): keep run attestation claims permanently Co-authored-by: Codex --- secure/verifier-service/serverless.yml | 3 --- secure/verifier-service/src/lib/runAttestation.ts | 5 +---- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/secure/verifier-service/serverless.yml b/secure/verifier-service/serverless.yml index 447d8d4..8fb3796 100644 --- a/secure/verifier-service/serverless.yml +++ b/secure/verifier-service/serverless.yml @@ -144,9 +144,6 @@ resources: KeySchema: - AttributeName: RunId KeyType: HASH - TimeToLiveSpecification: - AttributeName: ExpiresAt - Enabled: true # PEM bundle containing registry-service's client cert public cert # (csav/registry-service/tls/client.crt) - upload it here before deploy. diff --git a/secure/verifier-service/src/lib/runAttestation.ts b/secure/verifier-service/src/lib/runAttestation.ts index 53de988..8bbe5e6 100644 --- a/secure/verifier-service/src/lib/runAttestation.ts +++ b/secure/verifier-service/src/lib/runAttestation.ts @@ -6,9 +6,7 @@ import { requiredEnv } from "@srads/common/env"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); const tableName = requiredEnv("RUN_ATTESTATION_TABLE"); -// A run attests within minutes of launch; a day bounds the table without reopening a completed run. -const RUN_ATTESTATION_TTL_SECONDS = 24 * 60 * 60; - +// Rows are kept permanently: a TTL would reopen the run id once it expired, and run ids are never reused. // First writer wins. The conditional put makes two concurrent attestations for one run race safely: one mints // tokens, the other gets ConditionalCheckFailedException and is rejected. export async function claimRunAttestation(algorithmId: string, runId: string): Promise { @@ -20,7 +18,6 @@ export async function claimRunAttestation(algorithmId: string, runId: string): P RunId: runId, AlgorithmId: algorithmId, AttestedAt: new Date().toISOString(), - ExpiresAt: Math.floor(Date.now() / 1000) + RUN_ATTESTATION_TTL_SECONDS, }, ConditionExpression: "attribute_not_exists(RunId)", ReturnConsumedCapacity: "TOTAL", From cf8c90ca3c234386ac630d52f480a9e7364f4c54 Mon Sep 17 00:00:00 2001 From: prdai Date: Thu, 19 Nov 2026 09:39:24 +0530 Subject: [PATCH 3/3] chore(verifier): drop run attestation comment Co-authored-by: Codex --- secure/verifier-service/src/lib/runAttestation.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/secure/verifier-service/src/lib/runAttestation.ts b/secure/verifier-service/src/lib/runAttestation.ts index 8bbe5e6..bd3a832 100644 --- a/secure/verifier-service/src/lib/runAttestation.ts +++ b/secure/verifier-service/src/lib/runAttestation.ts @@ -6,9 +6,6 @@ import { requiredEnv } from "@srads/common/env"; const ddb = DynamoDBDocumentClient.from(new DynamoDBClient({})); const tableName = requiredEnv("RUN_ATTESTATION_TABLE"); -// Rows are kept permanently: a TTL would reopen the run id once it expired, and run ids are never reused. -// First writer wins. The conditional put makes two concurrent attestations for one run race safely: one mints -// tokens, the other gets ConditionalCheckFailedException and is rejected. export async function claimRunAttestation(algorithmId: string, runId: string): Promise { try { const write = await ddb.send(