Add tamper-evident integrity to spend-cap state#22
Merged
Conversation
The cap-state spend log was plain JSONL with no integrity, and load silently skipped malformed lines. The same OS user could therefore erase or rewrite spend history to bypass caps, and a garbage line that overwrote a real spend went unnoticed. Add a per-record chained HMAC-SHA256 (UseCapStateFileWithHMAC), keyed off the wallet identity via HKDF (info=pilot-cap-state-v1). Load now fails closed: a malformed line, an HMAC mismatch, or a signed-chain mixed with an unauthenticated record refuses to load rather than under-counting. A wholly-legacy file is migrated once to an authenticated chain; the legacy unauthenticated format stays readable via UseCapStateFile and still fails closed on malformed lines. Tests: tampered record detected, malformed line not silently dropped, HMAC round-trip survives restart, legacy migration then tamper-evident.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Problem
The cap-state spend log (
cap-state.jsonl) was plain JSONL with no integrity, andloadSpendRecordssilently skipped malformed lines. The same OS user could therefore tamper with or erase spend history to bypass caps, and a garbage line overwriting a real spend went unnoticed (under-counting → cap loses force).Fix
info=pilot-cap-state-v1), exposed asUseCapStateFileWithHMAC.cmd/walletderives the key from theLocalSignerand uses the authenticated path by default.UseCapStateFilepath stays for unauthenticated files but also stops silently skipping malformed lines.The HKDF derivation matches pilotctl's reader (
appstore caps) so the daemon reads what the wallet writes.Tests
TestCapStateTamperedRecordDetected— altered amount with stale HMAC, and a plain record spliced into a signed chain, both caught.TestCapStateMalformedLineNotSilentlyDropped— garbage line errors (with and without a key) instead of being dropped.TestCapStateHMACRoundTrip— authenticated chain survives restart and the cap still holds.TestCapStateLegacyMigration— legacy file migrated, then tamper-evident.Validation
GOWORK=off go build/vet ./...clean;go test -race ./...green.