caelum0x 531fabbcab fix(ledgerline): bind source_settlement_receipt signature to all claim fields, not just receipt_hash
Independent re-verification of the 2026-11-05 fix (600d177e) found a real
residual gap: SourceReceiptVerifier::verify() checked the Ed25519 signature
only against the bare receipt_hash bytes, while request_id/payer_account/
operation/amount_micros/sequence sat alongside it as separate, unsigned
fields. MeterEvent::validate()'s "correlation" check only compared those
fields against the same request's own fields (both caller-controlled, so
trivially self-consistent) -- never against independent ground truth. A
caller holding any one genuine (receipt_hash, signature) pair, e.g. from
their own legitimate low-value transaction, could replay it verbatim while
declaring an arbitrary amount_micros/payer_account/operation for an
unrelated event, poisoning the margin ledger exactly as the original bug
intended to prevent.

Fixed by verifying the signature against a new commitment_digest: a
domain-separated SHA-256 over every claim field, not receipt_hash alone.
Changing any field now changes the digest and invalidates the signature,
closing the replay/substitution path. Mirrors the pattern mint-server's
TransferAttestation/HoldSettlementAttestation and relay-aitp-wire's
HoldSettlementAttestation::verify_expected already use elsewhere in this
monorepo. Safe to redefine now: zero other call sites of
SourceSettlementReceipt exist anywhere in the repo, so nothing in
production depends on the old, narrower contract -- confirmed the deployed
railyard-ledgerline-1 container predates even the original 600d177e fix
(image built 2026-10-28), and the real margin_events table holds exactly
one smoke-test row, source=sdk, zero RelayGateway rows -- not yet
load-bearing in prod.

Added 2 regression tests proving a genuine receipt_hash+signature replayed
onto a different amount/payer/request is now rejected (one at the
receipt_verify unit level, one end-to-end through the real ingest_event
chokepoint). cargo test --lib: 42/42 passing (was 40/40 before these 2
additions). cargo clippy --all-targets: only the 2 pre-existing outbox.rs
sort_by_key warnings (confirmed via git stash identical on clean main),
none in the changed files.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-06 22:24:52 +03:00

43 lines
1.1 KiB
TOML

[package]
name = "ledgerline"
version = "0.1.0"
edition = "2021"
license = "MIT"
description = "AI usage metering with request-level revenue and COGS reconciliation"
# Standalone by design: never enter the root desktop GPUI/Metal build graph.
[workspace]
[[bin]]
name = "ledgerline-server"
path = "src/main.rs"
[dependencies]
anyhow = "1"
async-trait = "0.1"
axum = "0.7"
chrono = { version = "0.4", features = ["serde", "clock"] }
ed25519-dalek = "2"
hex = "0.4"
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
sha2 = "0.10"
sqlx = { version = "0.8", default-features = false, features = [
"runtime-tokio",
"tls-rustls",
"postgres",
"macros",
"migrate",
"chrono",
] }
subtle = "2"
thiserror = "2"
tokio = { version = "1", features = ["full"] }
tower-http = { version = "0.6", features = ["trace"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
[dev-dependencies]
tower = { version = "0.5", features = ["util"] }