flex-auth/pkg/api/approval_binding_test.go
tegwick dd3ce4c109
Some checks are pending
CI Smoke / host-smoke (push) Waiting to run
CI Smoke / container-smoke (push) Waiting to run
Build and Publish Container Image / build-and-push (push) Successful in 52s
Publish approval_binding_digest: a claim cannot name the request carrying it
secrets-engine confirmed T03, and re-verifying against the regenerated
destroy fixture found something neither repository can fix alone: a
pdp_digest recorded at issue time can never equal the request_digest of a
request that carries the claim in its context, because the claim is part
of the context that is hashed. Embedding the claim changes the very
digest the claim would need to name.

Not fixture staleness. It holds for every dual-control request whose
claim travels in context -- the shape GH-DEC-2026-008 had just ruled
mandatory. Left unresolved that ruling was unimplementable for exactly
the case it was written for, and destroy would have been permanently
un-allowable in production, failing closed forever on a check that could
never pass.

flex-auth owns the canonical request digest, so the fix is ours.
binding.approval_binding_digest is the same material with
context.approval removed, emitted only when a claim was carried. An
approval issued against a claim-free Check records that Check's
request_digest; the claim-bearing request reproduces it here.

DELIBERATELY ADDITIVE, and the reason matters. The tempting fix is to
drop context.approval from request_digest entirely. That is wrong:
request_digest is the replay identity, and two requests differing only in
which approval was presented must not share one, because their decisions
differ -- one allows, the other denies dual_control_required. Collapsing
them would let an allow obtained with a valid claim be replayed against a
request carrying none. So request_digest still covers the claim and still
moves; approval_binding_digest deliberately does not, and is documented
as not a replay identity. The tests assert the two functions DISAGREE on
a claim-bearing request, which is approval-engine's formulation of how to
defend a distinction that looks like duplication.

The fixture now demonstrates the property rather than asserting it: its
claim's pdp_digest equals the envelope's approval_binding_digest with
pdp_path true, and changing the claim's contents moved request_digest
while leaving approval_binding_digest untouched. Two files a consumer can
diff.

Also picked up approval-engine's new required binding.pdp_path via the
cross-repo schema test added yesterday -- which is the test doing exactly
what it was built for, one day later.

T03 is done. secrets-engine's own digest-material defect, which our two
real envelopes caught, is recorded in the workplan.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JTbVXpEiXA7mNJVpDnEPcB

Assistant: claude-code
Assistant-Model: opus
Assistant-Process: 412054@bnt-lap001
Assistant-Session: 3968fae1-8d59-4209-9bd6-c22594b8ab19
2026-09-06 14:52:33 +02:00

86 lines
3.5 KiB
Go

package api
import "testing"
func claimBearing() CheckRequest {
return CheckRequest{
Tenant: "tenant:platform",
Subject: SubjectRef{ID: "secrets-engine", Type: "service"},
Action: "destroy",
Resource: ResourceRef{ID: "lane:glas-primary", Type: "secret-catalog-lane", System: "secrets-engine"},
Context: map[string]any{
"approval": map[string]any{"kind": "approval-claim", "valid_now": true},
},
}
}
func claimFree() CheckRequest {
r := claimBearing()
r.Context = nil
return r
}
// TestApprovalBindingDigestSurvivesAttachingTheClaim is the property the whole
// field exists for. An approval's pdp_digest is recorded at issue time against a
// claim-free Check; the request that later carries the claim must still be able
// to name it.
func TestApprovalBindingDigestSurvivesAttachingTheClaim(t *testing.T) {
atIssue := RequestDigest(claimFree())
atExecute := ApprovalBindingDigest(claimBearing())
if atIssue != atExecute {
t.Fatalf("approval binding digest moved when the claim was attached:\n issue: %s\n execute: %s", atIssue, atExecute)
}
}
// TestRequestDigestStillMovesWhenTheClaimIsAttached guards the reason this is a
// second digest rather than a redefinition of the first. request_digest remains
// the full replay identity: attaching a claim changes the request, so it must
// change the digest. Collapsing the two would let an allow obtained with a valid
// claim be replayed against a request carrying none.
func TestRequestDigestStillMovesWhenTheClaimIsAttached(t *testing.T) {
if RequestDigest(claimFree()) == RequestDigest(claimBearing()) {
t.Fatal("request_digest ignored context.approval — replay identity must cover the claim")
}
}
// TestTheTwoDigestsDisagreeOnAClaimBearingRequest asserts the distinction is
// real rather than decorative. A test that the two functions disagree is how a
// distinction that looks like duplication is defended.
func TestTheTwoDigestsDisagreeOnAClaimBearingRequest(t *testing.T) {
req := claimBearing()
if RequestDigest(req) == ApprovalBindingDigest(req) {
t.Fatal("the two digests agree on a claim-bearing request; one of them is not doing its job")
}
}
// TestApprovalBindingDigestEqualsRequestDigestWithoutAClaim keeps the field
// honest for every consumer that never sends one.
func TestApprovalBindingDigestEqualsRequestDigestWithoutAClaim(t *testing.T) {
req := claimFree()
if RequestDigest(req) != ApprovalBindingDigest(req) {
t.Fatal("digests differ on a claim-free request; they must be the same value")
}
}
// TestBindingOmitsApprovalDigestWhenNoClaimIsPresent avoids publishing a field
// that would read as a second identity on every ordinary decision.
func TestBindingOmitsApprovalDigestWhenNoClaimIsPresent(t *testing.T) {
if got := NewDecisionBinding(claimFree()).ApprovalBindingDigest; got != "" {
t.Fatalf("expected no approval_binding_digest on a claim-free request, got %q", got)
}
if got := NewDecisionBinding(claimBearing()).ApprovalBindingDigest; got == "" {
t.Fatal("expected approval_binding_digest on a claim-bearing request")
}
}
// TestOtherContextClaimsStillBindUnderTheApprovalDigest confirms the exclusion is
// surgical: only the approval key leaves the material.
func TestOtherContextClaimsStillBindUnderTheApprovalDigest(t *testing.T) {
a := claimBearing()
a.Context["purpose"] = "rotate-exposed-key"
b := claimBearing()
b.Context["purpose"] = "something-else"
if ApprovalBindingDigest(a) == ApprovalBindingDigest(b) {
t.Fatal("approval binding digest ignored a non-approval context claim")
}
}