FiniToshi · cyber

Invariant-first defense: auditing Solana programs the way compilers think

2026-10-30 · by FiniToshi
EVERY TRANSACTION pay · gate · route · deliver THE GATE what must always be true MAINNET the real network REFUSED · A FINDING
The gate: every transaction is checked against what must always be true. The one with no enforcer bounces off and is refused as a finding.

Two hashes on one screen. Same program, two networks. Different bytes.

For weeks we had tested our flow against devnet, Solana's rehearsal network, where the money is fake and nobody minds if you break things. Every test green. The whole path: pay, gate, route the fee, deliver. Then, one step before launch, someone asked the dumbest question in the room: is the program we tested the program that users will actually run?

We hashed the deployed code on both networks to find out. They did not match. Different lengths. The devnet copy was a different build of the same idea. Every test had been honest. The artifact they tested was one nobody on mainnet will ever run.

One approval earlier and we would have pointed real money through a program we had never actually tested. And our bug hunt would not have caught it either. That is not an insult; it is the failure mode every audit carries. The audit only finds what it thinks to look for. The vulnerability without a category walks past you, waving.

This post is about the discipline we run instead. It comes from compilers, which solved this problem decades ago, and it has one property we value above cleverness: it can fail. Each audit produces rows that either hold or don't. An audit that cannot fail is a ceremony.

weeks of green tests every check honest, on the rehearsal network the dumb question is the tested program the program users run? two hashes, different bytes the rehearsal copy was a different build launch refused the invariant had no enforcer; the gate held hash gate wired forever proven before launch, watched after
The catch, as a timeline: weeks of green tests, the dumb question, two hashes with different bytes, the launch refused, the hash gate wired in for good.

What compilers know

A compiler does not hope your program is correct. It keeps a short list of statements that must be true at every instant your program runs, and it refuses to continue the moment one breaks. Think of a strict librarian: not clever, not creative, absolutely certain about the few rules it keeps, and loud the instant a book is shelved wrong.

Each statement on the list is an invariant: something that must be true always. Not usually. Not on the happy path. Always, or the program is wrong.

Notice what this is not. It is not a search for bugs. It is an accounting of truths. Rust programmers know the feeling as the borrow checker, the famous rule that refuses to let two parts of a program own the same memory at once. The checker does not understand your program. It is trustworthy because it is mechanical about a short list of non-negotiables and refuses to look away.

Assumptions in a compiler are not comments. They are checks. The checks are the product.

On-chain, nobody holds the list for you

On Solana, a shared referee called the runtime enforces a few invariants for you. It runs on every computer in the network, and it checks who signed a transaction, who owns which account, whether an account can pay its rent. A handful. Everything about your program's own rules, who may mint, what a lawful amount is, when a withdrawal may happen, exists only if you wrote it down, and holds only if you wrote it correctly.

That gap between "the network checks a few things" and "your rules are your own responsibility" is where most program-level exploits we have studied live. Not in exotic cryptography. In rules that existed only as intentions.

So here is the method. It is smaller than you would think.

Five moves

1 · draw the money path 2 · write what must always be true 3 · name the enforcer of each truth the network rejects it for you your code a potential bug NOTHING a finding. 4 · attack the list, row by row 5 · fail closed: missing input → refuse
The whole method, as five moves.

1. Draw the money path. List every instruction that moves value or changes who is in charge. Not the feature list; the value list. A token launch flow is short: configure, fund, trade, migrate, withdraw, upgrade. Forgetting an entry here is the first way an audit fails. If "upgrade" is not on your list, you have already decided not to look at the most powerful instruction in the program.

2. Write what must always be true, per step. Before, during, after. Who can call this, and how is that proven here? What must already exist, and what happens if it doesn't? Which amounts are lawful, and what makes one unlawful? And for every error path: what state does the program leave behind? That last one is the question audits skip.

3. Name the enforcer of each truth. This is the heart of the method. Every invariant lands in one of three buckets. Runtime-enforced: the network itself rejects the violation. Program-enforced: your code checks it, and every one of those checks is a potential bug. Or enforced-by-nothing: a comment, a docs page, a hope. Bucket 3 is where the bodies are. Our rule after several audits: an invariant with no enforcer is a finding, full stop, even if no exploit is known today. "Nobody sends that instruction right now" is not a control. It is a race between your documentation and everyone else's creativity.

4. Attack the list, row by row. Now, and only now, go bug hunting. You finally know what a bug is: a way to make one row false. Every row owes you either a proof or a counterexample. "Looks fine" is not an outcome the method accepts.

5. Run the fail-closed test. One question for the whole program: for every missing, zero, or empty input, does it refuse, or does it proceed? Every proceed is a candidate finding. Defaults are where permissiveness hides, because nobody reviews a default as a decision.

Which row caught the hashes

Go back to the two hashes on the screen. The row that caught our near-miss reads like this: the program I call is the program I audited. Ask who enforces it, and the honest answer was nobody. Nothing in the network requires the rehearsal copy to match the real one. It was a hope. Bucket 3.

The fix is cheap, and it is wired in. Hash the deployed code on every network we depend on. Gate the launch on the hashes matching what we tested. Keep checking after launch, because a dependency can redeploy at any time, and our proof will not notice on its own.

The story has one more turn. The first version of that hash check lied to us. Our script hashed the first account it found for the program and reported both networks identical. High confidence. Wrong. Upgradeable programs store their code in a separate account; what we hashed first was a 36-byte forwarding address, identical on both networks precisely because it contained no code at all. A check that passes trivially is not a check. Even the checker owes you an invariant: assert that the bytes you hashed could be code.

Compilers learned all of this decades ago. It is why reproducible builds exist. Trust belongs in artifacts, not in intentions.

Three more from the same page

We recently ran this method through a real money path: a payment gate, fee routing, a dependency on someone else's program. A page of invariants. What follows are the classes worth stealing.

Fail-closed configuration. The flow had a platform fee wallet. If that wallet is not configured, the correct behavior is to refuse the flow. The tempting version treats it as zero-fee and continues, which converts an omission into a free ride. A default nobody reviewed is a vulnerability that ships with a config file.

Replay protection must bind to this purchase. A gate that checks "a payment with this signature exists on-chain" proves that a payment happened once, somewhere, by someone. It does not prove this purchase was paid. The invariant: each payment authorizes exactly one unit of exactly one thing, and spent authorizations are consumed irreversibly.

Authority is the bug. Every upgradeable program has an upgrade authority: the single key allowed to replace its code. A money path secured by perfect invariants is still one signature away from arbitrary behavior if that key is one hot wallet. The question is not "is the code safe". It is "what is the smallest set of keys that can make the code anything else". Multisig and timelock, several required keys and a mandatory waiting period, are how "we promise not to rug" becomes "the program cannot express rugging".

The one-page rule

They fit on one page, and we now treat that as a law of method. If your invariant list doesn't fit on a page, you haven't compressed. If you can't name the failure mode per row, you have a wish list. The page is the deliverable: someone who wasn't in the room can review it, it converts directly into test cases, and, the part we care about most, it converts into gates. Rows checked automatically before every deploy. Rows asserted by a monitor forever after.

Security auditing done this way stops being a search for wrong things and becomes an accounting of true things. The count is published. Every row is either holding or screaming.

We're preparing the full template as a follow-up. If you run a money path and want to argue with the method before then, the comment section of your own audit log is a fine place to start.

Author: FiniToshi. Generalized from real audit work; no identifying details of any reviewed program, deployment, or party are included by design.