caelum0x d7cac9c38a fix(rago): trust Caddy's real IP hop, not client-forged XFF; release Mint hold on insert_event failure
client_ip() read the left-most X-Forwarded-For entry, which a direct
caller to Caddy can forge freely (Caddy appends, never strips, the real
peer address). This let attackers spoof the anon rate-limit bucket key
(observability.rs) and the buyer IP sent to Iyzico / persisted for
clock-triggered charges (checkout_webhooks.rs). Since Caddy is the sole
public listener and every rago host binds 127.0.0.1-only, there is
exactly one trusted hop: read the right-most XFF entry (Caddy's own
append) instead, and only that single hop - never fall through to
client-supplied entries if it's missing or unparsable. Updated the test
that previously locked in the vulnerable left-most behavior and added a
test asserting a forged left-most entry is never trusted.

ingest_event() placed a real-time wallet Mint hold before inserting the
event, but only released it on the Ok(false) duplicate-insert path. Any
other insert_event error (pool exhaustion, connection reset, ...)
propagated via `?` straight out of the function, leaving the hold open
with no corresponding `events` row - money silently reserved for an
event the caller was told failed, later captured by settle_invoice
against a line item that never existed. Now insert_event's Err path
releases the hold before returning the error, same as the duplicate
path.

Verified: cargo check -p rago-api clean; cargo test -p rago-api --lib
25/25 pass (incl. 2 new client_ip tests); standalone before/after repro
confirms the old left-most logic returns the forged "1.2.3.4" from
"X-Forwarded-For: 1.2.3.4, 9.9.9.9" while the fixed right-most logic
returns the real "9.9.9.9".
2026-11-03 15:55:17 +03:00
..