test(x402-buyer): make ConfirmSpendFailure test work under uid 0#373
Merged
test(x402-buyer): make ConfirmSpendFailure test work under uid 0#373
Conversation
The test forced StateStore.writeLocked to fail by creating its state directory with mode 0o500. That works for non-root uids but is silently bypassed when tests run as uid 0 (CI containers, dev sandboxes), because CAP_DAC_OVERRIDE lets root write through read-only directory perms. The spend-confirm failure path never fired, the metric family was never registered, and the test failed with "missing metric family". Switch to a permission-independent failure injection: after LoadStateStore succeeds, pre-create the target state path as a directory. The final os.Rename(tmpfile, statePath) in writeLocked then returns EEXIST/EISDIR, which root cannot bypass (it's a filesystem semantic, not a DAC check).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
TestProxy_ConfirmSpendFailure_IncrementsMetricso it actually exercises the confirm-spend failure path when tests run asuid 0(CI containers, dev sandboxes).Root cause
The test forced
StateStore.writeLockedto fail by creating its state directory with mode0o500. That works for non-root uids but is silently bypassed underCAP_DAC_OVERRIDEwhen tests run as root — the write goes through,ConfirmSpendreturns nil, theOnConfirmSpendFailurecallback never fires, the metric family is never registered, and the test fails withmissing metric familyatproxy_test.go:1897.Reproducible on every commit on
mainin a root-uid container, e.g.:Fix
After
LoadStateStoresucceeds, pre-create the target state path as a directory. The finalos.Rename(tmpfile, statePath)inwriteLockedthen returnsEEXIST/EISDIR, which root cannot bypass — it's a filesystem semantic, not a DAC check. Permission-independent failure injection without touching production code.Test plan
go test -run TestProxy_ConfirmSpendFailure_IncrementsMetric -v ./internal/x402/buyer/— PASS (log showsconfirm spend: rename state: … file exists, confirming the intended path fires)go test ./internal/x402/buyer/— PASS (full package)go vet ./internal/x402/buyer/— cleango build ./...— cleanGenerated by Claude Code