DEV Community

Cover image for anchor-sec-lint: a heuristic security linter for Anchor programs
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

anchor-sec-lint: a heuristic security linter for Anchor programs

Many of the bugs that hurt Solana programs are not clever. An authority account that was never required to sign. An UncheckedAccount that nobody checked. A PDA created with a bump taken from the caller. A vault that stores its owner, while the instruction never compares that owner with the signer. These are account-validation mistakes, and they are easy to make because Anchor lets you describe accounts in a few lines of attributes, where one missing word changes who is allowed to call the instruction.

Reviewers know this list by heart, but people get tired, and a pull request that adds one new #[derive(Accounts)] struct does not always get a full review.

anchor-sec-lint is a static checker for Anchor programs that looks for these patterns. It parses the Rust sources with syn, or reads an Anchor 0.30+ IDL, and flags the common mistakes. It writes text, JSON, or SARIF 2.1.0, so the findings can show up in GitHub code scanning.

The README's first lines set the right expectation, and I repeat them here: this is not audited, and it is a heuristic linter. A clean report does not mean a program is safe, and some findings will be false positives. Use it as a review aid, never instead of an audit.

What it does

There are six rules, each with an id you can use in reports and in suppression comments.

ASL001 missing-signer (error). Authority-like fields (authority, admin, owner, *_authority and similar) typed AccountInfo or UncheckedAccount instead of Signer. It also flags SystemAccount authorities when nothing signs, and, as a warning, structs that mutate program accounts with no signer at all.

ASL002 unchecked-account (error). AccountInfo or UncheckedAccount without a /// CHECK: doc comment. When the comment exists but there is no owner, address, seeds or constraint, it adds a note.

ASL003 weak-seeds-or-bump (warning). A hard-coded bump = 254 (reported as an error), a bump taken from an instruction argument, create_program_address(...) in code, and init from fully static seeds, with a note that the first caller wins.

ASL004 pda-seed-collision (warning). Two different account types with the same seed shape, for example [b"user", *], and adjacent variable-length seeds such as [a.as_bytes(), b.as_bytes()].

ASL005 missing-has-one (error). A mutable Account<T> where T stores a Pubkey field whose name matches a Signer in the struct, with no has_one, constraint or seed tying them together.

ASL006 unchecked-arithmetic (warning). +=, -=, *= and + - * on account data or method results outside checked_* calls. Test modules and constants are skipped.

IDL mode. When you pass a .json file, the linter runs the signer checks on instructions: authority-named accounts that are not signer, and instructions that write accounts but require no signer. This is useful when you have a program's IDL but not its source.

Three output formats. Text for the terminal, JSON for scripts, and SARIF 2.1.0 for GitHub code scanning, where findings appear under Security and as annotations on pull requests.

How to use it

Install it from the repository with Cargo:

cargo install --git https://github.com/AbdeldjalilChougui/anchor-sec-lint
Enter fullscreen mode Exit fullscreen mode

The README also lists prebuilt binaries for Linux, macOS and Windows on the GitHub releases page, with installer scripts. Then run it on your program:

anchor-sec-lint programs/                    # text report, exit 1 on any error
anchor-sec-lint programs/ --fail-on warning
anchor-sec-lint programs/ -f sarif -o asl.sarif --base "$PWD"
anchor-sec-lint target/idl/my_program.json   # IDL only
Enter fullscreen mode Exit fullscreen mode

On the vulnerable fixture that ships with the tests, the report looks like this:

tests/fixtures/vulnerable/lib.rs:14:9: warning [ASL006 unchecked-arithmetic] unchecked `+=` in `v.balance += amount`
tests/fixtures/vulnerable/lib.rs:70:9: error [ASL005 missing-has-one] `Withdraw.vault` (Vault) stores `owner` and `owner` signs, but they are never compared (missing `has_one = owner`)
tests/fixtures/vulnerable/lib.rs:102:9: error [ASL003 weak-seeds-or-bump] `Tag.profile` uses a hard-coded bump `254`
...
Enter fullscreen mode Exit fullscreen mode

Each line has the file, the line and column, the severity, the rule, and a sentence that names the field and says what is missing.

In CI. The repository is also a GitHub Action:

- uses: actions/checkout@v5
- uses: AbdeldjalilChougui/anchor-sec-lint@v0.1.0
  with:
    path: programs          # space-separated files/dirs and/or IDL .json files
    fail-on: warning        # error (default) | warning | note | never
Enter fullscreen mode Exit fullscreen mode

To send findings to code scanning, set format: sarif and output: anchor-sec-lint.sarif on the action, then add a github/codeql-action/upload-sarif step. The job needs the security-events: write permission. The README has the full workflow, and it notes that SARIF URIs are made repo-relative automatically.

Suppressing a finding. When a finding is a false positive, put // asl:allow ASL00N <reason> on the line, or the line above, of the reported field or expression. asl:allow all silences every rule at that spot. The reason stays in the code, where the next reviewer can read it.

How it works

  1. Every .rs file under the given paths is parsed with syn. target/, node_modules/ and hidden directories are skipped.
  2. The linter extracts the #[derive(Accounts)] structs: the field wrapper types (Signer, Account<T>, UncheckedAccount and so on), the #[account(...)] constraints split on top-level commas, the /// CHECK: docs, and the #[instruction(...)] argument names.
  3. It also collects the #[account] data structs, so that ASL005 knows which Pubkey fields an account stores.
  4. A syn::visit pass over function bodies finds raw arithmetic and create_program_address.
  5. The seed shapes of all files are compared to find collisions across instructions.

Things to know

  • It is not an audit. The rules are name and shape heuristics. A clean run only means these six patterns were not found.
  • No data flow. The linter does not follow values through the handler. A check done by hand, such as require_keys_eq!, is not seen by ASL005, so you will get a finding that you then suppress with a reason.
  • Macros are not expanded. Accounts structs generated by macros are invisible to it.
  • Seed collisions are compared by shape only. It cannot tell that two dynamic seeds always hold different kinds of keys.
  • What was tested. The test suite has two fixtures: tests/fixtures/vulnerable must trigger all six rules, and tests/fixtures/fixed, the same program with the issues fixed, must be clean. The tests also cover IDL mode, the SARIF shape, suppression comments, exit codes and the line numbers of findings.

Why I built it

The mistakes this linter looks for are well known, and that is the reason to automate them. A human reviewer should spend attention on the logic of a program: what the instruction is supposed to allow, and whether the economics hold. Checking that every authority is a Signer and every UncheckedAccount has a CHECK comment is mechanical work, and mechanical work is where a tired reader slips.

A linter that runs on every pull request does not get tired. It will not find a subtle bug, and it says so, but it makes sure the obvious questions are asked each time a new accounts struct appears. The SARIF output matters for the same reason: a finding shown as an annotation on the changed line gets read, while a report in a separate log is often skipped.

Get the code

The source, the two fixture programs, the tests and the GitHub Action are on GitHub: https://github.com/AbdeldjalilChougui/anchor-sec-lint. It is MIT licensed. Issues and pull requests are welcome, including reports of false positives.


Originally published at abdeldjalilchougui.biz.

Top comments (0)