# Audit: Juice savings + trading safes, auth-helpers-v3, Jing router v6-1 **Bounty:** mwvrpx3a633f201e8caa — pre-deploy audit, new code only vs audited bases. **Reviewed:** pillar-wallets-xyz @ 321f103 (`juice-savings.clar`, `juice-trading.clar`, `juice-safe-auth-helpers-v3.clar`), jing-contracts-v3 @ d2f6cad (`swap-router-sbtc-stx-jing-v6-1.clar`), diffed against `juice-autostake-safe.clar`, `fakfun-wallet-v19.clar`, `swap-router-sbtc-stx-jing-v6.clar`. --- ## H1 (High) — Signed `limit-price`/`min-out` do not constrain the AMM legs: a Zest leg can sell below the signed limit **Contracts:** `juice-trading.clar` `jing-swap` (L2088–2160) → `juice-safe-auth-helpers-v3.clar` `build-jing-swap-hash` (L67–85) → `swap-router-sbtc-stx-jing-v6-1.clar` `swap-stx-for-sbtc` / `swap-sbtc-for-stx` (L397–639) + `leg-zest` (L288–334). The passkey signs `{auth-id, action, amount, limit-price, min-out}`. The helpers-v3 comment says "what the user signs is what they sell and the worst they accept (min-out, Jing limit-price)". But the router's **split** path never applies `limit-price` to anything except the Jing leg: - `swap-stx-for-sbtc` uses `limit-price` only in the `jing-swap` call (L557). The Zest leg receives `zest-route { ticks, price }` **verbatim from the caller**, and `leg-zest` passes it straight to `ZEST_ROUTER swap-y-for-x ... (get price route) (get ticks route) u0 (partial-fill) ...` (L311–312) — the `u0` is min-out 0. - Nothing asserts `zest-route.price` against the signed `limit-price` (or the internally computed `zest-bound`). `amm-mins.zest` is also caller-supplied and unsigned. - The only bound is the **global** `min-out` → `min-sbtc-out`/`min-stx-out` assertion (L591). The Jing leg can be given `jing-amount = 0`, so the entire sale runs through the unchecked Zest leg. **Concrete failing scenario (sell STX, worst case):** 1. User passkey-signs `jing-swap` `"stx-sbtc"`, `amount = 100_000_000` µSTX, `limit-price = `, `min-out = 0` (or a low value). 2. A hostile submitter (keeper relayer, or anyone who obtains a signed payload) submits with `jing-amount = 0`, `amm-amounts { zest: 100_000_000, dlmm: 0, xyk: 0, velar: 0 }`, `amm-mins { zest: 0, ... }`, `zest-route { ticks: (walk), price: u1 }`. 3. In `swap-stx-for-sbtc`, `leg-zest` calls `swap-y-for-x` with `min-price = u1` — fill-while `price >= min-price` accepts **every** bin in the pool. The user's STX is sold at whatever prices a front-runner engineered, with partial fill on and per-leg min 0. 4. The only check left is `out >= min-sbtc-out`. With `min-out = 0` the user can lose nearly the whole amount; with `min-out = M > 0` the submittor still extracts everything between `M` and fair value via a sandwich on the Zest leg. For `"sbtc-stx"` (x→y) the same `price: u1` max-price is self-limiting (fills nothing below the bound... actually `max-price = u1` fills nothing, so the sell-sBTC direction is protected by accident), but `swap-y-for-x` with `min-price = u1` is the exploitable direction — exactly "a Zest leg selling below the limit" from the bounty's hit-list. **Fix:** enforce the signed limit on the Zest leg: in `leg-zest`, derive the bound from `limit-price` (same math as `zest-bound` with the router-read fee) and assert `(>= (get price route) bound)` for sell-STX / `(<= (get price route) bound)` for sell-sBTC; or at minimum include `amm-mins` and `zest-route.price` in `build-jing-swap-hash`. --- ## H2 (Medium/High) — `build-update-stake-stx-juice-pool-hash` does not sign `old-signer` (submitter-changeable field) **Contracts:** `juice-safe-auth-helpers-v3.clar` L44–62; `juice-savings.clar` `update-stake-stx-juice` L1452–1521; also `unstake` L1523–1568 (helpers-v1 `build-unstake-stx-juice-hash` signs only `auth-id`). The bounty asks: "Check every field a submitter could change is signed." In `update-stake-stx-juice` the signature covers `auth-id, amount-increase, cycles-to-extend, pool (new signer), signer-calldata` — but **`old-signer` is not in the hash**. A submitter holding a valid signature for a legitimate update (e.g. "add X, stay in pool A") can present the identical signature with any other `old-signer` trait contract. The outcome depends entirely on pox-5's internal validation of the old-signer parameter in `stake-update`: - If pox-5 rejects a mismatched old-signer, the impact is limited to DoS (a swap of the parameter bricks the update until a fresh signature). - If any pox-5 code path accepts a non-current `old-signer` (the v6 description itself flags pool/signer handling as a risk area, and pox-4/5 have a history of signer-mismatch edge cases), the stake's signer registration can be rewritten by a submitter without the user having signed the source pool — i.e. a pool "move" for which the passkey never approved the `old-signer`. This is precisely the field class v3 was created to fix (v3 added `pool` to the stake/update hashes) but `old-signer` was left out. **Fix:** add `old-signer: (contract-of old-signer)` to `build-update-stake-stx-juice-pool-hash`. Same reasoning applies to `unstake`'s `old-signer` (helpers-v1 hash binds only `auth-id`); even if pox-5 validates today, signing it removes the dependence on that assumption. `restake` is fine (keeper-only and `signer` must equal the currently registered pool). --- ## M1 (Medium) — `jing-swap` accepts a passkey-signed swap with **no per-leg or cooldown protection on any AMM parameter**, and the sbtc-usdcx branch silently drops `zest` `juice-trading.clar` L2193–2204: for `"sbtc-usdcx"`/`"usdcx-sbtc"` the `zest-route` and `amm-amounts.zest`/`amm-mins.zest` are dropped. Fine today (v5-6 has no Zest leg), but the signature also covers nothing about the split: `jing-amount`, `amm-amounts`, `amm-mins`, `fallback`, `update` are all submitter-controlled by design ("route split filled in at send time"). That is acceptable **only** as long as the router independently derives per-leg floors from the signed `limit-price`. It does so for Jing, but not for Zest/DLMM/XYK/Velar (see H1) — so the "min-out still protects the user" invariant holds only at the global assertion, not per leg. Treat as the enforcement-side companion of H1. --- ## L1 (Low) — `zest-ticks` walk truncates silently for wide limits; documented "one past tick(bound)" is not reached `swap-router-sbtc-stx-jing-v6-1.clar` `zest-ticks` L1386–1410 + `zest-tick-step` L1362–1384. The walk is `fold` over `ZEST_WALK` (32 entries) starting at `tick(mid) ± 2`. If `|tick(bound) − tick(mid)| ≥ 29` (limit more than ~30% from mid — reachable since `ZEST_MAX_LIMIT` is 1e21 and typical mids are far smaller), the 32-element list ends before `tick(bound)+1`. The header (L12–15) claims "oversized limit/mid ... give in = out = 0 and the rest goes to DLMM", but `> (len ticks) u0` is true for the truncated list, so Zest executes a partial walk. Not a safety loss (Zest still enforces the `bound` parameter, so nothing fills beyond the limit), but the stage's fill range is silently ~30%-short of the user's bound and the "skip" behavior is misdocumented. Consider `asserts!`/explicit skip when `(| (- tb tm)) > 29`, or extend the walk. --- ## L2 (Low) — `unsold` accounting in the split path ignores XYK/Velar residuals `swap-router-sbtc-stx-jing-v6-1.clar` L485–493 / L506–514: `unsold` sums Jing `residual`, `zest-short` and only the DLMM residual (`(- dlmm-in (get in dlmm))`). If `fallback` is unset and XYK/Velar capacity is zero, `cp-stage`'s `unsold` (which is correct) is never merged into the outer `unsold`. Off-chain indexers/keepers reading the print will over-report "sold" for those legs. Observability only; no fund impact. --- ## Notes / verified-clean (new code checked, no issue found) - **Zest direction fix (v6→v6-1) is correct:** pool u3 is x = sBTC, y = Zest wstx; all 4 call sites pass `SBTC ZEST_WSTX` with `swap-x-for-y` for sell-sBTC (max-price, `fee-rate-x`, ticks ascending from `tick(mid)−2`) and `swap-y-for-x` for sell-STX (min-price, `fee-rate-y`, ticks descending from `tick(mid)+2`). v6's swapped direction and `(not sell-sbtc)` walk are both fixed. - **`1e28/limit` inversion is right:** limit = uSTX-per-sat × 1e10 → `ZEST_INV_WAD / limit` = sats-per-uSTX × 1e18 = Zest WAD. Fee folding is conservative in both directions: sell-sBTC `floor(floor(1e28/limit)·(1−f))` (shrinks max-price), sell-STX `ceil(ceil(1e28/limit)/(1−f))` (raises min-price) — rounding never lets a leg fill beyond the user's limit. - **Divide-by-zero:** v6-1 added the `> limit u0` / `> mid u0` sanity guards v6 lacked; `zest-bound` guards `fee < ZEST_FEE_ONE`; `scale-min` guards `planned = 0`. - **2-key `-now` logic is sound:** passkey-created ops require `is-admin-calling`; admin-created ops require a one-shot passkey sig over `build-execute-now-hash {auth-id, op-id}` (different topic ⇒ no cross-replay with the creation signature); `consume-signature` blocks hash and assertion replay; memo/withdrawal payload are re-checked from the stored op, so the confirming key cannot alter content. - **`onboard`** (deployer-only by design): recovery ≠ owner ≠ self enforced; register/log ordering fine. - **v19 smart-router wiring:** `authorize-smart` binds op/smart/amount/min-out/fak-ratio/flag; router must be registry-approved. One observation out of the strict diff scope: `smart-sell-stx`/`smart-sell-sbtc` do not bind the `token` trait in `build-smart-execute-hash` — if the helper is unchanged from the audited v19 base this is pre-existing, but since sell paths spend `(with-ft (contract-of token) amount)`, consider adding `token` to the hash in a future helper version. --- *Line numbers refer to the files at the commits stated above.*