Skip to content

feat(gateway): wire authority chain verification into PEP middleware#69

Merged
beonde merged 4 commits intomainfrom
feat/gateway-chain-verification
May 6, 2026
Merged

feat(gateway): wire authority chain verification into PEP middleware#69
beonde merged 4 commits intomainfrom
feat/gateway-chain-verification

Conversation

@beonde
Copy link
Copy Markdown
Member

@beonde beonde commented May 6, 2026

Summary

Implements RFC-008 §9.2 Step 7 — the gateway PEP now calls VerifyChain() on incoming X-Capiscio-Authority / X-Capiscio-Authority-Chain headers before querying the PDP.

Changes

Header Extraction (pkg/gateway/headers.go)

  • ExtractLeafAuthority() — reads X-Capiscio-Authority header
  • ExtractAuthorityChain() — decodes base64url chain from X-Capiscio-Authority-Chain
  • ValidateChainLeafConsistency() — cross-checks leaf envelope subject DID against badge subject

Middleware Wiring (pkg/gateway/middleware.go)

  • PEPConfig gains EnvelopeVerifier, MaxChainDepth, OrgTrustBoundary fields
  • verifyAuthorityChain() inserted between badge verification and PDP query in serveHTTP()
  • buildPIPRequest() enriched with verified chain data (LeafCapability, DelegationDepth, Constraints, EnforcementMode)
  • handleChainError() with EM-OBSERVE pass-through (RFC-005 §6.3)
  • ChainErrorHTTPStatus() maps 14 RFC-008 error codes to HTTP 400/401/403
  • Enforcement mode escalation from envelope EnforcementModeMin field

Error Constants (pkg/envelope/errors.go)

  • Added ErrCodeChainTooDeep for MaxChainDepth enforcement (distinct from ErrCodeDepthExceeded which is per-envelope depth)

Test Coverage

  • 12 unit tests in headers_test.go: header extraction, chain decoding, leaf consistency validation
  • 11 integration tests in chain_verification_test.go:
    • Badge-only backward compatibility (no chain headers → proceeds normally)
    • Single envelope verification
    • Two-hop chain verification with capability narrowing
    • MaxChainDepth enforcement
    • Tampered signature rejection
    • PDP request enrichment verification
    • Nil EnvelopeVerifier bypass (opt-in behavior)
    • HTTP error code mapping (14 codes)
    • EM-OBSERVE pass-through (chain errors logged but request allowed)
    • Enforcement mode escalation (envelope EnforcementModeMin overrides PEP config)

All 40+ pre-existing gateway tests continue to pass — fully backward compatible.

Backward Compatibility

  • EnvelopeVerifier is optional in PEPConfig. When nil, the middleware skips chain verification entirely (existing behavior).
  • No chain headers → badge-only flow continues as before.
  • No breaking changes to the PEPConfig struct (all new fields have zero-value semantics).

Related

Implements RFC-008 §9.2 Step 7 — the gateway PEP now calls VerifyChain()
on incoming X-Capiscio-Authority / X-Capiscio-Authority-Chain headers
before querying the PDP.

Changes:
- Add header extraction functions (headers.go) with base64url chain decoding
- Add ValidateChainLeafConsistency() for subject/badge-DID cross-check
- Wire verifyAuthorityChain() into serveHTTP() between badge verify and PDP query
- Enrich PDP request with verified chain data (LeafCapability, DelegationDepth, etc.)
- Add handleChainError() with EM-OBSERVE pass-through (RFC-005 §6.3)
- Map all 14 RFC-008 error codes to HTTP status codes (400/401/403)
- Add ErrCodeChainTooDeep constant to envelope errors
- Support enforcement mode escalation from envelope EnforcementModeMin field

Tests:
- 12 unit tests for header extraction and chain consistency validation
- 11 integration tests covering: badge-only compat, single envelope, two-hop chain,
  max depth enforcement, tampered signature rejection, PDP enrichment verification,
  nil verifier bypass, HTTP error code mapping, EM-OBSERVE pass-through, and
  enforcement mode escalation

All 40+ existing gateway tests continue to pass (backward compatible).
Copilot AI review requested due to automatic review settings May 6, 2026 04:16
@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 2026

Codecov Report

❌ Patch coverage is 87.67123% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/gateway/middleware.go 86.36% 13 Missing and 2 partials ⚠️
pkg/gateway/headers.go 91.66% 2 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the gateway Policy Enforcement Point (PEP) middleware to verify RFC-008 Authority Envelope chains from X-Capiscio-Authority / X-Capiscio-Authority-Chain headers before querying the PDP, and to enrich the PDP (PIP) decision request with verified chain-derived attributes.

Changes:

  • Added RFC-008 authority chain header extraction/decoding helpers and leaf/chain consistency checks.
  • Wired chain verification into the PEP request flow and enriched PIP requests with verified capability, depth, constraints, envelope ID, and enforcement mode data.
  • Introduced a new RFC-008 error code for PEP-level max chain-length enforcement and added HTTP status mapping for chain verification errors.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pkg/gateway/middleware.go Integrates chain verification into the PEP flow, enriches PIP requests with verified chain data, and maps verification errors to HTTP responses.
pkg/gateway/headers.go Adds RFC-008 header constants and helpers to extract leaf envelope, decode chain and badge-map headers, and validate basic header consistency.
pkg/gateway/headers_test.go Adds unit tests covering the new gateway header helper functions.
pkg/gateway/chain_verification_test.go Adds integration tests for chain verification behavior, HTTP mappings, and PIP request enrichment.
pkg/envelope/errors.go Adds ENVELOPE_CHAIN_TOO_DEEP for PEP-level chain-length enforcement.

Comment thread pkg/gateway/middleware.go Outdated
Comment on lines +142 to +146
// --- 2-8. Verify authority chain (RFC-008 §9.2 steps 2–8) ---
var chainResult *envelope.ChainVerifyResult
if p.config.EnvelopeVerifier != nil {
var err error
chainResult, err = p.verifyAuthorityChain(r, token)
Comment thread pkg/gateway/middleware.go
Comment on lines +239 to +243
for _, link := range chain.Links {
linkMode, _ := pip.ParseEnforcementMode(link.EffectiveMode.String())
if linkMode > p.config.EnforcementMode {
req.Context.EnforcementMode = linkMode.String()
}
Comment thread pkg/gateway/middleware.go
Comment on lines +297 to +301
opts := envelope.VerifyOptions{
Now: func() time.Time { return time.Now() },
}

result, err := p.config.EnvelopeVerifier.VerifyChain(r.Context(), chain, badgeMap, opts)
beonde added 2 commits May 6, 2026 11:11
Previously the loop compared each link's mode to the config baseline,
which could downgrade the selected mode if a stricter link was followed
by a less-strict one. Now tracks the max seen so far and only updates
once at the end. Also handles ParseEnforcementMode errors.
Copilot AI review requested due to automatic review settings May 6, 2026 15:30
RFC-008 §9.2 step 6: The leaf envelope's subject_did MUST match
the authenticated caller's badge subject. This prevents a valid chain
from being replayed by a different agent.

Also ensures the caller's badge JWS is present in the badge map
passed to chain verification, preventing lookup failures.

Updated tests to align badge subject with envelope subject_did.
@beonde beonde merged commit 9157964 into main May 6, 2026
4 checks passed
@beonde beonde deleted the feat/gateway-chain-verification branch May 6, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants