Appearance
ADR-2604: Signed Gateway Expiry Guard and Scoped Trial Upgrades
- Status: Accepted
- Date: 2026-11-21
- Authors: @fderuiter
- Deciders: @fderuiter
- Requirements: PRD-SYS-001, PRD-SYS-004, PRD-SYS-053, PRD-SYS-192
1. Context & Problem Statement
Following ADR-2547, self-registered workspaces provisioned a 30-day Trialing subscription in the Clinical Kernel, and KernelCommandPipeline refused non-baseline kernel mutations with 403 K.Subscription.Expired once a trial expired. However, three commercial and cross-service gaps remained:
- Standalone Clinical Service Bypass: The seven standalone clinical hosts (
execution,ctms,etmf,econsent,safety,payments,tickets) did not receive tenant commercial read-only state in the gateway's signed identity headers, allowing expired workspaces to mutate standalone clinical records. - Unscoped Reactivation & Countdown Reset: Switching commercial tiers (
Individual,Select,Complete) shared a single command with paid activation, immediately promoting workspaces toActive, clearingTrialEndsAt, and allowing expired workspaces to reactivate without structured study scoping or purchase-order/quote confirmation. - Web Expiry Drift: Authenticated web views (
AppShell.vue,OverviewView.vue,PlansView.vue) neither displayed exact trial countdown days nor synchronized read-and-export workspace state when mutations failed with403 K.Subscription.Expired.
2. Decision Drivers & Constraints
- Tamper-Proof Edge Propagation (
PRD-SYS-053,ADR-2256): Client-supplied commercial headers must never be trusted. AnyX-Tenant-Read-Onlyflag forwarded to downstream hosts must be stripped from inbound requests, resolved at the gateway, bound into the canonical HMAC-SHA256 signature (HmacSigner), and verified fail-closed byGatewayAuthenticationHandler. - Regulated Read, Export, and Signature Continuity (
PRD-SYS-001,docs/strategy/PRODUCT_CATALOG.md): Expired workspaces enter a read-and-export posture rather than a hard lockout:GET/HEADreads, regulated archive exports (POST /api/v1/etmf/documents/exportwithetmf_document:export), 21 CFR Part 11 step-up electronic signatures (ElectronicSignatureRequired), and explicit commercial activation/onboarding endpoints (AllowExpiredTenantWriteMetadata) must remain available. - Scoped Commercial Activation Without Live Card Processing: Transitioning a
TrialingorExpiredworkspace toActiverequires validated study scoping (study_phase,study_duration_months,countries,site_count,participant_count,active_study_count) and a purchase order or signed quote reference (billing_confirmation), returning422 K.Commercial.ScopingInvalidwith RFC 9457 field errors when incomplete. - Strict Commercial vs. Clinical Authority Separation (
PRD-SYS-004,PRD-SYS-192,ADR-2547): Commercial entitlements govern workspace packaging and trial mutation posture only; they never confer study/site visibility or bypassGrant,ClinicalScopeGuard, blinding, or participant consent.
3. Options Considered
Option 1: Per-Service Direct Database Reads of kernel.subscriptions
- Overview: Have each standalone clinical service query
kernel.subscriptionsdirectly on every mutating HTTP request. - Pros:
- Avoids gateway caching.
- Cons:
- Violates
K-XS-2by coupling standalone clinical services to kernel commercial persistence tables and adds cross-schema database load on every write.
- Violates
Option 2: Gateway-Cached Signed X-Tenant-Read-Only Identity, Shared ExpiredSubscriptionWriteGuardMiddleware, and Split SwitchTrialTier / ActivateSubscription Commands (Selected)
- Overview: Resolve tenant read-only posture at
apps/gatewayvia a 30-secondIClock-backedGatewayTenantEntitlementCache(with immediate response-transform cache eviction after/api/v1/kernel/entitlements/tier,/api/v1/kernel/entitlements/activate, and/api/v1/kernel/onboarding/trial); bindtenant_read_only(X-Tenant-Read-Only) intoGatewayIdentityandHmacSigner; enforceExpiredSubscriptionWriteGuardMiddlewareinUseCadenceSecurity()across every service with explicit regulated exemptions; splitSwitchTrialTier(POST /api/v1/kernel/entitlements/tier, active trials only, preservingStatus = Trialingand originalTrialEndsAt) fromActivateSubscription(POST /api/v1/kernel/entitlements/activate,[BaselineKernelCommand]requiring validatedSubscriptionScoping); and surface trial countdowns, scoped intake, andK.Subscription.Expiredstate synchronization inapps/web. - Pros:
- Preserves
K-XS-2boundaries while enforcing tamper-proof403 K.Subscription.Expiredwrite protection uniformly across kernel and standalone hosts. - Bounds stale read-only state to at most 30 seconds on natural clock expiry and 0 seconds after in-band tier or activation mutations.
- Prevents expired tenants from reactivating without study scoping and PO/quote confirmation while keeping active trial countdowns intact across tier evaluations.
- Preserves
- Cons:
- Natural clock-driven trial expiry can lag by up to 30 seconds on standalone hosts until the gateway cache entry expires, though kernel mutations still check
Subscription.IsExpiredAt(clock.UtcNow)transactionally.
- Natural clock-driven trial expiry can lag by up to 30 seconds on standalone hosts until the gateway cache entry expires, though kernel mutations still check
4. Decision Outcome
- Chosen Option: Option 2
- Justification: Signing
X-Tenant-Read-Onlyat the gateway and enforcingExpiredSubscriptionWriteGuardMiddlewareinsideUseCadenceSecurity()closes the standalone write gap without violatingK-XS-2, while separatingSwitchTrialTierfrom scopedActivateSubscriptionenforces structured commercial scoping without gating regulated reads, exports, or electronic signatures.
5. Consequences & Trade-offs
- Positive Impact: Expired workspaces cannot create or mutate clinical records in either
kernel-apior the seven standalone clinical services (403 K.Subscription.Expired), regulated exports and Part 11 signatures remain uninterrupted, trial tier switches preserve the original 30-day window, activation captures auditable study scoping and PO/quote references, and the web shell surfaces exact trial days remaining and upgrade guidance. - Negative Impact / Technical Debt:
kernel.subscriptionscarries seven nullable scoping columns (study_phase,study_duration_months,countries_csv,site_count,participant_count,active_study_count,billing_confirmation) populated upon activation. - Mitigation Strategy:
SubscriptionScopingValidatornormalizes and bounds all seven inputs before persistence and emits structured per-field errors (422 K.Commercial.ScopingInvalid) consumed directly byPlansView.vue.
6. Implementation & Verification
- Affected Repositories / Services:
packages/security,apps/gateway,packages/kernel(Abstractions,Domain,Application,Persistence,Api),apps/kernel-api,apps/web,docs/openapi/kernel_openapi.json,docs/schema/kernel.md. - Verification Plan: Verified via
CadenceSecurityUnitTests,GatewayAuthenticationTests,ExpiredSubscriptionStandaloneServicesTests(all 7 standalone hosts),CommercialAndOnboardingUnitTests,Cadence.ArchitectureTests, PostgreSQL 18 forced-RLS integration tests (KernelTrialOnboardingPostgresTests,KernelSchemaPostgresTests,KernelTenantIsolationPostgresTests), and Vitest/axe-core tests (trial_onboarding_and_plans.test.js).