Skip to content

fix(trade): refuse Solana txs that hand off control of wallet accounts - #845

Open
kome12 wants to merge 5 commits into
mainfrom
fix/solana-sim-token-account-authority
Open

kome12 wants to merge 5 commits into
mainfrom
fix/solana-sim-token-account-authority

Conversation

@kome12

@kome12 kome12 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Solana swap and limit-order verification now refuses to sign when the simulated transaction would hand control of one of the wallet's accounts to someone else, even when every balance looks right.

Why

simulateSolanaAssetChanges compared only balances. A transaction could pass a top-level instruction the wallet signs to an unrecognised program, which then CPIs Approve(delegate=attacker, amount=MAX) or SetAuthority(owner=attacker) on the wallet's token account. The swap output still arrives and every balance delta is clean, so the transaction got signed and the attacker could drain the account later. The static instruction check classifies only top-level instructions, so it doesn't see CPIs.

How

After simulation, simulateSolanaAssetChanges compares the post-state's authorities for every tracked wallet account. It throws the new SIM_ACCOUNT_CONTROL_CHANGED code when the post-state shows:

  • a token account whose owner is no longer the wallet
  • a new delegate, a different delegate, or a larger delegated amount
  • a new close authority
  • a closed empty non-WSOL token account. Closing the WSOL account is still allowed, because the wrap/unwrap cleanup closes it on every native-SOL swap.
  • the wallet account reassigned off the System Program (a CPI'd Assign)

It also asks simulateTransaction for inner instructions and holds every CPI to the token programs to the same rules as the top-level check: it refuses an Approve or SetAuthority the wallet authorizes, and a CloseAccount of a wallet token account (or one the wallet authorizes) whose rent goes anywhere but the wallet. The post-state can't show these when the account is closed (the WSOL account), or closed and re-created at the same address. A node that returns no inner instructions, or only compiled ones, is refused with the new SIM_INNER_INSTRUCTIONS_UNAVAILABLE code, which tells the user to set NANSEN_SOLANA_SIM_RPC (the default public RPC returns them). Malformed inner instructions fail closed too.

An unreadable token-account post-state now fails closed (SIM_RESULT_UNPARSEABLE). Addresses that were pre-funded with SOL (System-owned) are now tracked too. One that the transaction initializes as the wallet's token account is now checked, and its balance counted. Before, a swap into such an account was falsely blocked.

Both callers, swap execute (verifySolanaSwapOutcome) and limit-order deposit/cancel (verifyLimitOrderOutcome), treat the new code as a refusal, not a degrade. The error code users see is unchanged (OUTCOME_VERIFICATION_FAILED for swaps).

Not covered: stake and nonce accounts the wallet is the authority of (tracked separately).

Testing

  • 13 new cases in solana-simulation.test.js and 1 wiring case in trading.test.js. The 12 refusal and counting cases fail on main (the promise resolves instead of rejecting, or the transaction proceeds). The cases that must still pass: WSOL close, an unchanged delegate, a revoked delegate.
  • npm test (6274 passed), npm run lint.
  • A mocked end-to-end repro through verifySolanaSwapOutcome: delegate, owner, close authority, a closed empty account and a reassigned wallet are all signed on main and refused on the branch. A clean swap proceeds on both.
  • Read-only live simulation of real quotes, SOL→USDC and USDC→SOL with OKX and Jupiter. Nothing was signed or broadcast. All four verify on the branch, so there's no false block.
  • Inner-instruction check: 22 more cases in solana-simulation.test.js and 2 wiring cases in trading.test.js. Each refusal rule was mutation-checked (disabling it fails a test). Read-only live simulation of fresh OKX and Jupiter SOL→USDC quotes against mainnet returns parsed inner instructions, and both verify with the check run. Neither route closes an account from inside a CPI; the WSOL unwrap is a top-level close. USDC→SOL wasn't re-run (no USDC in the test wallet).
scenario main branch
clean swap proceed proceed
attacker delegate on output account proceed refused
attacker owner on output account proceed refused
attacker close authority on output account proceed refused
empty sibling token account closed proceed refused
wallet reassigned to another program proceed refused
clean swap into a pre-funded output address refused (false block) proceed

🤖 Generated with Claude Code

The Solana pre-sign simulation only compared balances, so a transaction
that CPI'd Approve/SetAuthority on one of the wallet's token accounts
(or Assign on the wallet itself) passed every check with the swap output
intact, and the new delegate or owner could drain the account later.

simulateSolanaAssetChanges now compares the post-state's authorities
for every tracked wallet account and throws SIM_ACCOUNT_CONTROL_CHANGED
on a new owner, a new or larger delegate allowance, a new close
authority, a closed empty non-WSOL token account, or the wallet
reassigned off the System Program. An unreadable token-account
post-state fails closed. Pre-funded (System-owned) addresses are now
tracked as candidates, so one initialized as the wallet's token account
is both checked and counted. Both swap execute and limit-order
deposit/cancel go through this check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/solana-simulation.js
// wrap/unwrap cleanup closes it on every native-SOL swap (its rent stays
// inside the native fee/rent slack either way).
if (extractBalance('token', t.preInfo) === 0n && !isSolanaNativeMint(mint)) {
throw controlChanged(`closes your empty token account ${t.pubkey} (mint ${mint}).`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 - Medium | Empty WSOL accounts can be closed to an attacker without detection

When a tracked wallet token account disappears with a zero token balance, the code rejects the closure for ordinary SPL mints but explicitly allows it for WSOL. Because the post-state snapshot contains only null, it cannot verify the close-account destination, and the static instruction guard only covers recognized top-level instructions; a CPI close can therefore send the WSOL account's reclaimed rent to an attacker while all token deltas remain zero. The comment's assumption that the rent remains within fee/rent slack is not enforced by this code. The WSOL exception should require an independently verifiable wallet destination, or the close should be rejected when the destination cannot be observed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same finding as the later thread; accepted risk, not changing. See #845 (comment): the loss is capped at the empty WSOL account's rent (~0.00204 SOL), strictly inside NATIVE_FEE_RENT_SLACK_LAMPORTS, which already tolerates the same outflow from a direct System transfer the wallet signs.

@nansen-pr-reviewer

nansen-pr-reviewer Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

nansen-pr-reviewer summary

The change substantially improves Solana simulation safety by failing closed when inner instructions are unavailable and by checking CPI authority operations. However, the existing empty-WSOL close exception still accepts top-level closes whose rent destination cannot be verified when a third-party close authority performs the close. The resulting native outflow remains covered by the configured fee/rent slack, so the accepted rent-theft path is still present.

Findings by Severity

Severity Count
🟡 Medium 6

Deterministic check: neutral — 5 earlier finding(s) still open (below the blocking bar)

Risk: 4/5 (High) — raised by: complexity: 740 added lines


Token usage: 55,932 input, 2,843 output, 19,239 cache read | Usage Guide

Cooldown: for the next 10 minutes (counting from when this review finished), new pushes to this PR will not trigger another review — the next push after the window expires will. Need a fresh review sooner? Comment @nansen-pr-reviewer[bot] re-review.

The wallet's WSOL account stays closable (the wrap/unwrap cleanup closes
it on every native-SOL swap), but only its token amount was counted as
leaving. An empty one's rent could go to any destination with no delta,
on top of a full-slack native outflow. Count the closed account's whole
pre-simulation lamport balance as native SOL leaving the wallet: a close
back to the wallet nets to zero, and a close elsewhere is bounded by the
existing fee/rent slack. An unreadable lamport balance fails closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/solana-simulation.js
throw controlChanged(`reassigns your wallet account ${t.pubkey} to program ${postInfo.owner}.`);
}
} else if (!postInfo) {
// Closing a token account needs a zero balance, so a non-zero pre-balance

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 - Medium | WSOL close destination remains unverifiable and attacker rent theft can pass

When a tracked empty WSOL account disappears, this branch accepts the close without verifying where its lamports went. It subtracts the entire pre-state lamport balance from the native delta, but the higher-level Solana outcome check explicitly tolerates native sibling outflows up to the fee/rent slack. A CPI can therefore close the wallet's empty WSOL account to an attacker, steal its rent, and remain within the permitted slack while all token deltas look valid. The WSOL exception needs an independently verifiable wallet destination, or empty WSOL closures should fail closed when the destination cannot be established.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same finding as the later thread; accepted risk, not changing. See #845 (comment): the loss is capped at the empty WSOL account's rent (~0.00204 SOL), strictly inside NATIVE_FEE_RENT_SLACK_LAMPORTS, which already tolerates the same outflow from a direct System transfer the wallet signs.

Comment thread src/solana-simulation.js Resolved
A jsonParsed SPL token account missing its mint, owner or token amount
was read as an empty account. A pre-state without tokenAmount counted
the account's existing balance as swap output, and one without owner
dropped out of tracking. Such a snapshot now throws
SIM_RESULT_UNPARSEABLE, so the swap is refused rather than verified
against made-up numbers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/solana-simulation.js Outdated
} else if (!postInfo) {
// Closing a token account needs a zero balance, so a non-zero pre-balance
// already shows up as an outflow. An empty one is a silent 0n delta and
// its rent can go anywhere. The WSOL account is the exception: Jupiter's

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 - Medium | Empty WSOL closures still accept an unverifiable rent destination

When a tracked empty WSOL account disappears, this branch accepts the closure after subtracting its entire pre-state lamport balance from the native delta, but it never verifies that the close destination is the wallet. The static instruction guard only classifies recognized top-level SPL instructions, so a CPI close can redirect the WSOL account's rent to an attacker. The resulting native outflow is then tolerated by the higher-level fee/rent slack check, allowing the transaction to pass when the stolen rent remains within that slack. Empty WSOL closures need an independently verifiable wallet destination, or should fail closed when the destination cannot be established.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted risk, not changing. The loss is capped at the WSOL account's rent (~0.00204 SOL). That is strictly inside NATIVE_FEE_RENT_SLACK_LAMPORTS, which already tolerates the same native outflow from a direct System transfer the wallet signs, so this adds no new attack. Wrapped balance in the account already counts as outflow and is caught. Verifying the close destination would need a separate WSOL-rent budget split out of the shared fee/rent slack, which the slack's own comments say causes false blocks on legitimate swaps.

@kome12 kome12 self-assigned this Nov 17, 2026
@kome12

kome12 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@nansen-pr-reviewer[bot] re-review

@kome12

kome12 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

re-review

1 similar comment
@kome12

kome12 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

re-review

@kome12

kome12 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@nansen-pr-reviewer[bot] re-review

Comment thread src/solana-simulation.js Resolved
Comment thread src/solana-simulation.js
} else if (kind === 'native') {
// A CPI'd System Program Assign (the wallet signs) hands the wallet's
// lamports to whichever program now owns it, with no balance change yet.
if (postInfo && postInfo.owner !== SYSTEM_PROGRAM) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 - Medium | Empty WSOL closure accepts an unverifiable rent destination

When a tracked empty WSOL account disappears, the code accepts the closure and subtracts its entire pre-state lamport balance from the native delta without verifying the close destination. A CPI close can therefore send the reclaimed rent to an attacker, while the higher-level native fee/rent slack permits the resulting outflow when it remains within the configured tolerance. This remains an accepted-risk gap rather than a fixed destination check.

simulateTransaction now also returns inner instructions. Each CPI to the
SPL Token or Token Extensions program is held to the top-level rules:
refuse an Approve or SetAuthority the wallet authorizes, and a
CloseAccount of a wallet token account (or one the wallet authorizes)
whose rent goes anywhere but the wallet. This closes the empty-WSOL rent
path and the close-then-recreate path, which the post-state alone can't
show.

The check runs after the post-state checks. A node that returns no inner
instructions, or only compiled ones, skips this check alone and the CLI
logs a partial-verification warning; malformed entries fail closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/solana-simulation.js Resolved
Comment thread src/solana-simulation.js
// closed through a close authority other than the wallet, its rent can
// go anywhere the destination checks don't see, so refuse it. The WSOL
// account is the exception: Jupiter's wrap/unwrap cleanup closes it on
// every native-SOL swap. The close itself is checked (a top-level close

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 - Medium | Medium | Empty WSOL closure still accepts an unverifiable rent destination

When an empty tracked WSOL account disappears, this branch subtracts its entire lamport balance and accepts the closure without independently proving that the rent returned to the wallet. The destination is checked only if readable inner instructions are available; top-level or CPI metadata can be absent, and the higher-level native fee/rent slack tolerates an attacker-directed outflow within that allowance. This is the same accepted-risk gap raised previously and remains present in the current diff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rent destination is now always checked, by one of two paths:

  • Top-level CloseAccount: assertSolanaInstructionsSafe checks it statically before signing and refuses a wallet-authorized close whose destination isn't the wallet.
  • Close from inside another program (CPI): the simulation is now always run with innerInstructions: true. assertInnerTokenInstructionsSafe refuses any close of a wallet-owned or wallet-authorized token account whose destination isn't the wallet. That includes an emptied WSOL account, and an account the wallet holds but whose close authority was handed to someone else.
  • Inner instructions unavailable: as of 2d69a3c the CLI refuses instead of skipping the check.

So an emptied WSOL account can no longer be closed to an unchecked destination. Tests: token accounts closed by a CPI in solana-simulation.test.js.

Without readable inner instructions the CPI checks can't run, and a CPI
can delegate, re-authorize or close a wallet account in ways the
post-state doesn't show. Refuse with SIM_INNER_INSTRUCTIONS_UNAVAILABLE
instead of signing with a warning. The default public RPC returns them;
the error says to set NANSEN_SOLANA_SIM_RPC otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/solana-simulation.js
// every native-SOL swap. The close itself is checked (a top-level close
// in assertSolanaInstructionsSafe, a CPI close in
// assertInnerTokenInstructionsSafe); here count the whole pre-balance
// (rent + wrapped amount) as leaving under SOL_SENTINEL, so it nets out

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 - Medium | Empty WSOL closes with unverifiable top-level destinations still pass

The WSOL exception accepts any tracked WSOL account that disappears after subtracting its entire pre-state lamport balance, without requiring proof that the rent returned to the wallet. The static instruction check only rejects a top-level CloseAccount when the wallet authorizes it and the destination is elsewhere; a close performed by a pre-existing third-party close authority can therefore send the rent to an attacker and still reach this branch. The resulting native outflow is tolerated by the higher-level NATIVE_FEE_RENT_SLACK_LAMPORTS check. The new CPI checks do not address this top-level path. Either require a verifiable wallet destination for every empty WSOL close or explicitly retain and document this as an accepted bounded-loss risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant