Appearance
ADR-2442: MSW Mock Gateway Simulator and Browser HMAC Verification Engine
Also known as: ADR-2185 (former number)
- Status: Accepted
- Date: 2026-09-12
- Authors: @jules
- Deciders: @fderuiter, @cadence-arch-board
- Requirements: PRD-GWY-001
1. Context & Problem Statement
The Cadence platform enforces strict GxP 21 CFR Part 11 electronic signature verification and API Gateway security controls across microservice boundaries. During local frontend development, end-to-end component testing, and MSW (Mock Service Worker) offline simulation, web components need to emulate Gateway path prefix stripping, 403 Subject-role route access restrictions, and dual-signature header validation (specifically X-Sig-Token step-up signature tokens and X-Gateway-Signature HMAC-SHA256 headers).
Prior to this change, client-side MSW mock handlers lacked centralized browser-compatible HMAC-SHA256 signing and path-prefix validation routines, causing mock responses to diverge from real Gateway security behavior.
2. Decision Drivers & Constraints
- Driver 1: Parity with API Gateway security controls (
X-Sig-Token,X-Gateway-SignatureVersion 2, Subject 403 route blocking, path prefix stripping). - Driver 2: Browser Web Crypto API compatibility (
crypto.subtle) for client-side MSW handlers without Node-only crypto module dependencies. - Driver 3: Zero regression in existing frontend unit and integration test suites.
3. Options Considered
Option 1: Ad-hoc Mock Signature Checking in Individual Handler Files
- Overview: Implement custom validation logic inside individual MSW route handlers.
- Pros:
- ✅ Quick to throw together for single tests.
- Cons:
- ❌ Duplicate code and inconsistent signature enforcement across endpoints.
- ❌ Hard to maintain when Gateway signature rules evolve.
Option 2: Shared mockGateway Simulator in @cadence/ui (Selected)
- Overview: Create a centralized
mockGateway.jsmodule inpackages/uiexporting path-prefix validators, Subject-role whitelist checkers, andvalidateGatewayRequestutilizing browser Crypto APIs viasigning.js. - Pros:
- ✅ Centralized, reusable Gateway security simulation for MSW and client test runners.
- ✅ Full alignment with API Gateway Version 2 signature spec and Subject RBAC rules.
- ✅ Unit tested via Vitest in
packages/ui/tests/mockGateway.test.js.
- Cons:
- ❌ Requires exposing
mockGatewayexport entry inpackages/ui/package.json.
- ❌ Requires exposing
4. Decision Outcome
- Chosen Option: Option 2
- Justification: Centralizing Gateway simulation in
@cadence/ui/mockGatewayguarantees consistent security and RBAC validation across all client-side MSW mock interceptors and tests.
5. Consequences & Trade-offs
- Positive Impact: MSW mock handlers accurately mirror Gateway behavior (prefix stripping, 403 Forbidden for Subject accessing administrative routes, e-signature gating).
- Negative Impact / Technical Debt: Added export point
mockGatewayinpackages/ui/package.json. - Mitigation Strategy: Covered by dedicated Vitest unit tests in
packages/ui/tests/mockGateway.test.js.
6. Implementation & Verification
- Affected Repositories / Services:
packages/ui - Verification Plan:
pnpm --filter @cadence/ui testanduv run python scripts/validate_adrs.py.
7. Amendment (2026-10-02): which requests the mock gateway intercepts
The web app and the subject portal each kept their own copy of the list of gateway paths their MSW handlers intercept, which the duplication gate flagged (#5164). That check now lives with the rest of the gateway simulation, as isGatewayApiCall(url) in packages/ui/mockGateway.js: a request is intercepted when it targets port 8000 or starts with one of the service prefixes. Both apps' handlers call it, and packages/ui/tests/mockGateway.test.js covers it.
8. Amendment (2026-10-19): no secret in the library (#5335)
The simulator used to export a literal GATEWAY_SECRET from the root of @cadence/ui and fell back to it whenever a caller passed none. A browser bundle never holds the gateway secret, and AGENTS.md §5 forbids fallback secrets, so:
- The simulator is exported only as
ui/testing. The root export (index.js) no longer re-exports it, andpackages/ui/tests/mockGateway.test.jsasserts that. - There is no
GATEWAY_SECRET.validateGatewayRequest(req, { secret })verifies anX-Gateway-Signatureonly against the secret passed in, and rejects a signed request when it has none. The apps' MSW handlers pass no secret, so they fail closed on a signature they cannot check; tests pass their own test secret.