{"id":"XE-0017","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"38","source_url":"https://github.com/xeprotocol/xe/issues/38","reporter_id":11,"title":"Invalid lease_cancel still unwinds provider accept (cancel-wins before validation)","area":"consensus","claimed_tier":"high","assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":true,"internal_ref":"","internal_url":null,"status":"accepted","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-29T13:57:15.321312+00:00","updated_at":"2026-11-10T11:38:28.149293+00:00","source_created_at":"2026-10-24T20:19:41+00:00","told_at":"2026-11-10T00:15:22+00:00","status_changed_at":"2026-10-29T14:04:12.071474+00:00","url":"https://bounty.xe.network/XE-0017","body":"# Invalid lease_cancel still unwinds a provider accept (cancel-wins runs before cancel validation)\n\n**Credit:** Mgal Ghifari  \n**Target:** XE Protocol — https://test.network/bounty  \n**Repo:** https://github.com/xeprotocol/xe @ `978785929bc17a2f4ab30c383e5fc9cf4d634512`  \n**Component:** `core/ledger.go` — `validateAndAddLeaseCancel` / `resolveCancelWinsOverAccept`  \n**Reward address (XE):** `fe280837c418c6a918cb046d9531648163316d339d93f1498f7b3b9740560570`\n\n---\n\n## Summary\n\nAn **invalid** `lease_cancel` (valid consumer signature, but body fails ledger checks — wrong balance, wrong previous, or timestamp-before-previous) still triggers `resolveCancelWinsOverAccept` → `undoBlockApply` and permanently unwinds a provider's not-yet-finalized accept. The unwind runs **before** the cancel's own pending / previous / timestamp / balance checks. So a block the ledger ultimately **rejects** still mutates state.\n\nOn any node that received that junk cancel: the accept is gone, re-sending the real accept is a silent no-op, after restart the provider's `LeasesAcceptedAsProvider` reads 0 vs 1 on honest nodes, and the provider's next block fails forever with `previous mismatch`. Honest nodes that never saw the cancel keep the accept. Lasting node split. Honest-path finality **locks** the damage — 0 conflicts on the victim, so voting never repairs it.\n\nI'm filing this as **High**. You could also read it as **Severe** under XE's wording (validation bypass / node compromise) — a rejected block still corrupts ledger state, and finality locks it. Not Critical: no mint, no double-spend, no escrow theft, and honest nodes that never saw the cancel stay consistent.\n\n---\n\n## Severity\n\n**High** (XE High tier: targeted DoS / race / privilege escalation a single client can cause)\n\nAlso readable as **Severe**: validation-ordering bug. `undoBlockApply` runs before the cancel is fully validated, so a block that fails checks still mutates the ledger, and honest finality locks the split.\n\nNot claiming Critical. No mint / double-spend / escrow drain. Honest nodes that never received the junk cancel stay consistent with each other.\n\n---\n\n## Affected code\n\nCommit: `978785929bc17a2f4ab30c383e5fc9cf4d634512`\n\n**Call site — unwind before cancel checks** (`core/ledger.go`):\n\n```go\n// validateAndAddLeaseCancel — lines 1814-1856\nif existing != nil && existing.State == LeaseAccepted && existing.Provider != b.Account {\n    if rerr := l.resolveCancelWinsOverAccept(b.Source, existing.Provider); rerr != nil {\n        return rerr\n    }\n}\n// ... only AFTER that:\n// previous mismatch  (1842-1843)\n// timestamp before previous (1847-1848)\n// balance mismatch   (1854-1855)\n```\n\n**Unwind itself** (`core/ledger.go:1749-1789`):\n\n```go\nfunc (l *Ledger) resolveCancelWinsOverAccept(leaseHash, provider string) error {\n    // ... find non-finalized tip accept ...\n    if err := l.undoBlockApply(provider, acceptBlock); err != nil {  // line 1784\n        return fmt.Errorf(\"cancel-wins: unwind provider accept: %w\", err)\n    }\n    return nil\n}\n```\n\n**Gossip delivery** (`node/node.go:705-711`):\n\n```go\nfunc (n *Node) handleIncoming(blocks <-chan *core.Block) {\n    for b := range blocks {\n        // ...\n        err := n.Ledger.AddBlock(b)  // every gossiped block, including ones that will be rejected\n```\n\n**Why re-sync does not heal** (`core/ledger.go:793-799`): known-hash early return. The rejected accept body stays in the block store, so `AddSyncedBlock(accept)` returns `nil` and does nothing.\n\n---\n\n## Preconditions\n\n1. A lease exists and a provider has accepted it (`LeaseAccepted`), accept **not** yet finalized.\n2. Attacker is the **lease consumer** (only the consumer can sign a cancel for that lease).\n3. Cancel has a **valid consumer signature**. A bad-sig cancel does **not** unwind (I checked — credential check runs before cancel-wins). The body is just semantically invalid.\n4. Delivery: gossip → `AddBlock`. Stock `SubmitBlock` will not re-gossip a rejected cancel. A malicious peer can `Gossip.Publish` the signed junk cancel directly onto the mesh; `handleIncoming` feeds it into `AddBlock`.\n5. Window: only before the accept is finalized. Once finalized, cancel-wins refuses with `accept is finalized`.\n\n---\n\n## Steps to reproduce\n\nAll testing was **local** — in-memory `store.MemStore` + in-process `core.Ledger`. No traffic to test.network. No public load.\n\nPoC lives under `reports/poc/` in a checkout of this commit. Relevant tests: `h4_cancel_test.go` (`TestH4b_*`), `h4b_multinode_test.go`, `round2_test.go` (`TestH9_*`).\n\n### 1) Core bug — three invalidity variants\n\n```bash\ncd /path/to/xe\ngo test ./reports/poc/ -run 'TestH4b_InvalidCancelStillUnwindsAccept' -v\n```\n\nWhat you see (each of `wrong_balance`, `wrong_previous`, `timestamp_before_prev`):\n\n- log line: `cancel-wins over a racing accept — unwound provider ...`\n- cancel error e.g. `balance mismatch: expected 1000000, got 1000999` (cancel **rejected**)\n- `lease state=created`, `provider frontier==accept? false`, accept status Rejected\n- re-submit canonical accept: `err=<nil>`, frontier still not accept (silent no-op)\n- provider's next block: `previous mismatch`\n- honest node that never saw the junk cancel: `provider frontier==accept? true`\n\n### 2) Multi-node lasting split + finality lock\n\n```bash\ngo test ./reports/poc/ -run 'TestH4b_MultiNode_LastingSplit|TestH4b_HonestFinalityCannotRepair' -v\n```\n\nObserved:\n\n- junk cancel → victim only: victim lease=`created`, honest stays `accepted`\n- honest finalizes accept; victim re-sync of accept: `healed=false`, body still in store\n- victim restart: `accepted-as-provider=0` vs honest `1`\n- provider next block on victim: permanent `previous mismatch`\n- `GetConflictsForAccount(provider)` on victim = **0** → voting never engages\n\n### 3) Amplification (H9) — dual-accept reality after unwind\n\n```bash\ngo test ./reports/poc/ -run 'TestH9_' -v\n```\n\nAfter the junk cancel on victim, a **new** accept on the restored frontier:\n\n- victim applies it (`lease=accepted` on a new hash)\n- honest keeps the original accept frontier and stages a conflict (`conflicts=1`)\n- lasting dual-accept reality across nodes\n\n### 4) Follow-on refund asymmetry (same root cause)\n\n```bash\ngo test ./reports/poc/ -run 'TestH4b_FollowOn_RefundAfterNetworkFinalizedAccept' -v\n```\n\nAfter junk cancel on victim, both nodes mark accept finalized network-wide; a later **valid** cancel:\n\n- victim: succeeds, lease=`cancelled`, consumer refunded\n- honest: `accept is finalized`, lease stays `accepted`\n\nSame lease, opposite outcomes, depending on who saw the junk cancel.\n\n---\n\n## Root cause\n\nOrdering bug in `validateAndAddLeaseCancel`:\n\n1. See lease in `Accepted` → call `resolveCancelWinsOverAccept`\n2. That calls `undoBlockApply` and **persists** the unwind\n3. **Then** check pending / previous / timestamp / balance\n4. Those checks fail → cancel is rejected\n5. Unwind is **not** rolled back\n\nSo a rejected cancel still mutates provider chain + lease state. No conflict object is registered on the provider account, so quorum / finality has nothing to vote on. Honest-side finality of the original accept then freezes the victim on `previous mismatch`.\n\n---\n\n## Impact\n\nWith one signed junk cancel, a lease consumer can:\n\n- Permanently unwind a provider's not-yet-finalized accept on every peer that receives the gossip\n- Freeze that provider's account chain on those peers (`previous mismatch` forever)\n- Split node state: affected peers vs honest peers disagree on lease state, frontier, and reputation (0 vs 1 after restart)\n- Create a dual-accept reality if a new accept is built on the unwound frontier (H9)\n- Leave no on-chain explanation — the cancel itself was rejected\n\nAttack cost: one properly signed consumer block + PoW, delivered via gossip. No special role beyond being the lease consumer.\n\n---\n\n## Suggested fix\n\n1. Fully validate the cancel (pending / previous / timestamp / balance — in a staged overlay) **before** any unwind.\n2. Only call `resolveCancelWinsOverAccept` / `undoBlockApply` after the cancel itself would commit.\n3. Make the unwind part of the cancel's atomic commit (same commit guards / `ExpectLeaseState`), so a rejected cancel cannot leave a half-applied unwind.\n4. Optional hardening: delete or tombstone rejected accept bodies so a later re-sync can re-apply the canonical accept instead of hitting the known-hash early return.\n\n---\n\n## Testing note\n\nAll of this was local: `MemStore` + in-process ledgers only. No traffic to test.network, no Track 2 load on the public testnet, no contact with the project before this report.\n","patch_refs":[{"id":122,"repo":"xeprotocol/xe","kind":"pr","ref":"39","title":"fix(core): validate lease_cancel before unwinding racing provider accept","url":"https://github.com/xeprotocol/xe/pull/39","note":"","state":"open","author":"ocakciomursel-pixel","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"},{"id":167,"repo":"xeprotocol/xe","kind":"pr","ref":"149","title":"fix(core): validate lease_cancel before unwinding racing provider accept (#38)","url":"https://github.com/xeprotocol/xe/pull/149","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"}],"thread":[{"id":8,"source":"github","source_ref":"6389073166","author":"efootballyay-dot","team":false,"body":"PoC source (local `MemStore` + in-process ledger tests only — no traffic to test.network):\n\nhttps://gist.github.com/efootballyay-dot/b096fe5ea57de21dcc0ccc8c699cc556\n\nRelevant tests in the gist: `TestH4b_*`, `TestH4b_MultiNode_*`, `TestH9_*`.","created_at":"2026-10-24T20:19:53+00:00"},{"id":1552,"source":"github","source_ref":"6755086537","author":"XE desk","team":true,"body":"Read and accepted. It is tracked internally and the fix will land in a public PR; this issue is\nupdated when it does, and the tier and award follow the fix. The desk entry is https://bounty.xe.network/XE-0017.","created_at":"2026-11-10T00:15:22+00:00"},{"id":1900,"source":"github","source_ref":"6764997236","author":"Reyzz95","team":false,"body":"Still reproduces at HEAD `89a542e` (testnet-0005, 2026-11-10).\n\n`validateAndAddLeaseCancel` (`core/ledger.go:1792-1877`) still runs `resolveCancelWinsOverAccept` before the pending / previous / timestamp / balance checks, so a rejected cancel still permanently unwinds the provider's not-yet-finalized accept.\n\nPoC (local `go test`, `core/zz_poc_area_b_test.go` `TestPOCAreaB_XE0017_InvalidCancelUnwindsAccept`): cancel with balance off by 1 → rejected with `balance mismatch`, but the lease state reverted to `created` and the provider chain lost the accept. Fix PRs xe#39 / xe#149 are both still open.","created_at":"2026-11-10T10:28:42+00:00"}]}