{"id":"XE-0078","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"103","source_url":"https://github.com/xeprotocol/xe/issues/103","reporter_id":30,"title":"[SECURITY][Minor] SDK - rentLease skips its cancel on a non-retryable wait error","area":"sdk-docs-web","claimed_tier":"minor","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:22.308904+00:00","updated_at":"2026-11-11T15:12:16.507996+00:00","source_created_at":"2026-10-27T17:59:47+00:00","told_at":"2026-11-10T00:15:47+00:00","status_changed_at":"2026-10-29T14:04:13.400914+00:00","url":"https://bounty.xe.network/XE-0078","body":"# [SECURITY] rentLease skips its cancel on a non-retryable wait error\n\n| Field | Value |\n|---|---|\n| **Severity** | Minor |\n| **Area** | SDK — compute leasing |\n| **Component** | `src/xe.ts` (`rentLease`) |\n| **Status** | Reproduced (control-flow) |\n| **Date** | 2026-10-27 |\n\n---\n\n## 1. Summary\n\n`rentLease` opens a lease (escrowing XUSD), waits for the provider to accept it, and on failure cancels the lease to refund the escrow. The cancel path is only reached when the wait error is *retryable*. For any other error the function re-throws immediately, before `cancelLease` runs:\n\n```typescript\n// src/xe.ts:322–331\ntry {\n  const lease = await this.waitForLease(opened.hash, ['accepted'], { timeoutMs: … })\n  return { lease, opened, attempts: attempt }\n} catch (err) {\n  lastError = err\n  if (!(err instanceof XeApiError && err.retryable)) throw err   // ← leaves without cancelling\n}\ntry {\n  await this.cancelLease(opened.hash)                              // ← refund path, skipped\n  await this.waitForLease(opened.hash, ['cancelled'], { timeoutMs: 30_000 })\n} catch (err) { … }\n```\n\nTwo distinct error shapes reach that guard:\n\n- **A terminal-state `XeUsageError`** — `waitForLease` throws this when the lease reaches `settled`, `cancelled`, `unfulfilled` or `expired` (`src/xe.ts:410`).\n- **A non-retryable `XeApiError`** — e.g. a `400` from `client.lease()` while the lease is still `created`.\n\nIn the first case the escrow has *already* been resolved by the ledger (see §4), so the missing cancel costs nothing. In the second case the lease is still `created` and the escrow genuinely stays locked — but it is recoverable by a manual `cancelLease`.\n\n## 2. Affected code\n\n| File | Line | Issue |\n|---|---|---|\n| `src/xe.ts` | 329–331 | Non-retryable error re-thrown before `cancelLease` — escrow not refunded |\n| `src/xe.ts` | 333–339 | The only cancel path; unreachable when the throw above fires |\n| `src/xe.ts` | 394–418 | `waitForLease` throws `XeUsageError` on a terminal state (`settled\\|cancelled\\|unfulfilled\\|expired`) |\n| `src/xe.ts` | 493–508 | `cancelLease` requires state `created`; only refunds an unaccepted lease |\n\n## 3. Reproduction\n\nDeterministic control-flow reproduction — models the exact branch structure of `rentLease` with a `waitForLease` that throws a terminal-state error.\n\n```javascript\n// rentlease-flow.mjs\nconst calls = []\nconst openLease    = async () => { calls.push('openLease');    return { hash: 'h' } }\nconst waitForLease = async () => { calls.push('waitForLease'); throw new Error('lease ended unfulfilled') }\nconst cancelLease  = async () => { calls.push('cancelLease') }\nconst isRetryable  = () => false\n\ntry {\n  for (let attempt = 1; attempt <= 3; attempt++) {\n    const opened = await openLease()\n    try {\n      await waitForLease()\n    } catch (err) {\n      if (!(err instanceof Error && isRetryable())) throw err   // mirrors src/xe.ts:331\n    }\n    await cancelLease()\n  }\n} catch { /* rentLease throws here */ }\n\nconsole.log('calls:', calls.join(' -> '))\n```\n\n**Observed:**\n\n```\ncalls: openLease -> waitForLease\n        (cancelLease is never reached — escrow not refunded)\n```\n\n## 4. Impact\n\nThe impact depends entirely on which error shape fires, and the two are very different. This distinction was checked against the ledger and store code rather than assumed:\n\n### Case 1 — terminal-state `XeUsageError`: no escrow impact\n\n| Terminal state | How it is reached | Escrow at that moment |\n|---|---|---|\n| `settled` | Provider settled the lease | Already credited back to the consumer by the settle block |\n| `cancelled` | Someone cancelled it | Already refunded by the cancel block |\n| `unfulfilled` | Only reachable *after* a successful `lease_force_settle` — `ledger.go:1983` sets `UnfulfillLeaseHash`, and the store requires `state == LeaseAccepted` first (`mem_store.go:413`) | Already returned by that force-settle |\n| `expired` | `node.archiveExpiredLease()` | Already **burned** by the node — and that burn happens on the expiry timer regardless of this SDK defect |\n\nSo in Case 1 the missing `cancelLease` call is a no-op: there is nothing left to refund, and nothing the SDK could have recovered.\n\n### Case 2 — non-retryable `XeApiError` while the lease is still `created`: escrow locked, recoverable\n\n- **Escrow stays locked.** The lease remains in `created` with its XUSD escrow, and `rentLease` throws without attempting a cancel.\n- **Recoverable.** The consumer can call `cancelLease(hash)` directly afterwards — the lease is still `created`, which is exactly the state `cancelLease` requires (`src/xe.ts:493–508`). The funds are not lost.\n- **Cost is delay and manual work**, not value: the escrow is out of the consumer's spendable balance until they notice and cancel, and if they never do, the lease eventually expires and the node burns it — the same outcome as any abandoned lease, not one caused by this bug.\n- **Requires a specific server response.** The node must return a non-retryable error (e.g. `400`) during the acceptance poll. A `503` or a retryable error takes the cancel path correctly, and a `404` is retried until timeout, which then throws a retryable `503` and also cancels correctly.\n\n> **Honest boundary:** no funds are permanently lost as a result of this defect. In the terminal-state case the ledger has already resolved the escrow before the SDK sees the error; in the non-retryable case the escrow is recoverable by a manual `cancelLease`. The defect is a missing cleanup on an error path — a robustness and accounting-hygiene issue that leaves a consumer's escrow locked longer than intended. That places it at Minor, not Medium.\n\n## 5. Suggested fix\n\n1. Cancel before re-throwing: run the `cancelLease` block for *every* wait failure, not only retryable ones, then re-throw the original error if the cancel also fails.\n2. Use a `try/finally` that guarantees a cancel attempt whenever a lease was opened but not accepted before the function returns or throws.\n3. Treat `unfulfilled`/`expired` as an explicit `done()`-style outcome rather than an exception, so the caller receives a typed result and the escrow state, not a bare throw.\n\n## 6. Duplicate check\n\n- `xeprotocol/xe` issue tracker scanned (98 items, open + closed + PRs) on 2026-10-27.\n- No issue or PR references `rentLease`, `rent_lease`, or an escrow-lock on a non-retryable wait error. All 17 PRs touch Go node/core/net/statechain or DAO Python.\n- Related but distinct ledger-side issues exist (**#92** cancel refunds only base cost, **#21** escrow without service, **#38/#39** lease_cancel unwinding) — all node-side, none about the SDK `rentLease` control flow.\n\n\n## 7. Submission checklist (per programme \"How to Report\")\n\n| Step | Requirement | Status |\n|---|---|---|\n| 1 | Reproduce against testnet; capture tx hashes / block heights | Reproduced locally by control-flow modelling of `src/xe.ts:322-339`. Not yet exercised against `test.network` — triggering it live needs the node to return a non-retryable error mid-poll, which is not reproducible on demand. The branch structure is confirmed by direct source reading. |\n| 2 | GitHub issue with suggested severity tier and area | Tier: **Minor** · Area: **SDK — compute leasing (cancellation)** |\n| 3 | Clear PoC + impact analysis (who is affected, worst case) | Included — sections 3 and 4 |\n| 4 | Critical/Severe goes to `security@xe.network` first | Not applicable — Minor is below Severe, so a public issue is the correct channel |\n| 5 | Triage by the core team | Awaiting |\n| 6 | XE address for payout at genesis | `d31bab17b9fd6635aa6cce191b4bea87e51d8ae8c93860537febe4294b01d05a` — XE native chain (block lattice, ed25519), BIP39 24-word + SLIP-0010 ed25519 m/44'/1'/account'; backup phrase in `xe-payout-wallet.secret` (0600) |\n\n---\n\n*Suggested tier: Minor (650 XE) — missing cleanup on an error path; escrow recoverable, no permanent loss*","patch_refs":[{"id":199,"repo":"xeprotocol/sdk","kind":"pr","ref":"12","title":"","url":"https://github.com/xeprotocol/sdk/pull/12","note":"","state":"","author":"","added_by":"desk","created_at":"2026-10-29T14:22:09.061497+00:00"}],"thread":[{"id":26,"source":"github","source_ref":"6462243272","author":"arturbarszcz78","team":false,"body":"[comment from a blocked account removed]","created_at":"2026-10-28T06:22:04+00:00"},{"id":1575,"source":"github","source_ref":"6755094099","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-0078.","created_at":"2026-11-10T00:15:47+00:00"}]}