Claude Skill

substrate-vulnerability-scanner

Scans Substrate/Polkadot pallets for 7 critical vulnerabilities including arithmetic overflow, panic DoS, incorrect weights, and bad origin checks. Use when auditing Substrate runtimes or FRAME pallets.

LLM Mart · 0 points · 5 views 0 listing impressions 0 install-command copies
Virus-scanned Reviewed automatically before listing.

Full trust report

Download trailofbits-skills-plugins_building-secure-contracts_skills_substrate-vulnerability-scanner-123037e.zip · 13 KB
trailofbits/skills 7234 616 forks CC-BY-SA-4.0 Updated 9h ago
Part of trailofbits/skills — 100 skills

Install

skills CLI npx skills add https://github.com/trailofbits/skills/tree/main/plugins/building-secure-contracts/skills/substrate-vulnerability-scanner
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install trailofbits-skills@llmmart
Git git clone https://github.com/trailofbits/skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole trailofbits/skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Substrate Vulnerability Scanner

1. Purpose

Systematically scan Substrate runtime modules (pallets) for platform-specific security vulnerabilities that can cause node crashes, DoS attacks, or unauthorized access. This skill encodes 7 critical vulnerability patterns unique to Substrate/FRAME-based chains.

2. When to Use This Skill

  • Auditing custom Substrate pallets
  • Reviewing FRAME runtime code
  • Pre-launch security assessment of Substrate chains (Polkadot parachains, standalone chains)
  • Validating dispatchable extrinsic functions
  • Reviewing weight calculation functions
  • Assessing unsigned transaction validation logic

3. Platform Detection

File Extensions & Indicators

  • Rust files: .rs

Language/Framework Markers

// Substrate/FRAME indicators
#[pallet]
pub mod pallet {
    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;

    #[pallet::config]
    pub trait Config: frame_system::Config { }

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::weight(10_000)]
        pub fn example_function(origin: OriginFor<T>) -> DispatchResult { }
    }
}

// Common patterns
DispatchResult, DispatchError
ensure!, ensure_signed, ensure_root
StorageValue, StorageMap, StorageDoubleMap
#[pallet::storage]
#[pallet::call]
#[pallet::weight]
#[pallet::validate_unsigned]

Project Structure

  • pallets/*/lib.rs - Pallet implementations
  • runtime/lib.rs - Runtime configuration
  • benchmarking.rs - Weight benchmarks
  • Cargo.toml with frame-* dependencies

Tool Support

  • cargo-fuzz: Fuzz testing for Rust
  • test-fuzz: Property-based testing framework
  • benchmarking framework: Built-in weight calculation
  • try-runtime: Runtime migration testing

4. How This Skill Works

When invoked, I will:

  1. Search your codebase for Substrate pallets
  2. Analyze each pallet for the 7 vulnerability patterns
  3. Report findings with file references and severity, above them a coverage table carrying a verdict for every pattern
  4. Provide fixes for each identified issue
  5. Check weight calculations and origin validation

5. Vulnerability Patterns (7 Critical Patterns)

I check for 7 critical vulnerability patterns unique to Substrate/FRAME. For detailed detection patterns, code examples, mitigations, and testing strategies, see VULNERABILITY_PATTERNS.md.

Pattern Summary:

  1. Arithmetic Overflow ⚠️ CRITICAL

    • Direct +, -, *, / operators wrap in release mode
    • Must use checked_* or saturating_* methods
    • Affects balance/token calculations, reward/fee math
  2. Don't Panic ⚠️ CRITICAL - DoS

    • Panics cause node to stop processing blocks
    • No unwrap(), expect(), array indexing without bounds check
    • All user input must be validated with ensure!
  3. Weights and Fees ⚠️ CRITICAL - DoS

    • Incorrect weights allow spam attacks
    • Fixed weights for variable-cost operations enable DoS
    • Must use benchmarking framework, bound all input parameters
  4. Verify First, Write Last ⚠️ HIGH (Pre-v0.9.25)

    • Storage writes before validation persist on error (pre-v0.9.25)
    • Pattern: validate → write → emit event
    • Upgrade to v0.9.25+ or use manual #[transactional]
  5. Unsigned Transaction Validation ⚠️ HIGH

    • Insufficient validation allows spam/replay attacks
    • Prefer signed transactions
    • If unsigned: validate parameters, replay protection, authenticate source
  6. Bad Randomness ⚠️ MEDIUM

    • pallet_randomness_collective_flip vulnerable to collusion
    • Must use BABE randomness (pallet_babe::RandomnessFromOneEpochAgo)
    • Use random(subject) not random_seed()
  7. Bad Origin ⚠️ CRITICAL

    • ensure_signed allows any user for privileged operations
    • Must use ensure_root or custom origins (ForceOrigin, AdminOrigin)
    • Origin types must be properly configured in runtime

For complete vulnerability patterns with code examples, see VULNERABILITY_PATTERNS.md.


6. Scanning Workflow

Step 1: Platform Identification

  1. Verify Substrate/FRAME framework usage
  2. Check Substrate version (v0.9.25+ has transactional storage)
  3. Locate pallet implementations (pallets/*/lib.rs)
  4. Identify runtime configuration (runtime/lib.rs)

Step 2: Dispatchable Analysis

For each #[pallet::call] function:

  • Arithmetic: Uses checked/saturating operations?
  • Panics: No unwrap/expect/indexing?
  • Weights: Proportional to cost, bounded inputs?
  • Origin: Appropriate validation level?
  • Validation: All checks before storage writes?

Step 3: Panic Sweep

# Search for panic-prone patterns
rg "unwrap\(\)" pallets/
rg "expect\(" pallets/
rg "\[.*\]" pallets/  # Array indexing
rg " as u\d+" pallets/  # Type casts
rg "\.unwrap_or" pallets/

Step 4: Arithmetic Safety Check

# Find direct arithmetic
rg " \+ |\+=| - |-=| \* |\*=| / |/=" pallets/

# Should find checked/saturating alternatives instead
rg "checked_add|checked_sub|checked_mul|checked_div" pallets/
rg "saturating_add|saturating_sub|saturating_mul" pallets/

Step 5: Weight Analysis

  • Run benchmarking: cargo test --features runtime-benchmarks
  • Verify weights match computational cost
  • Check for bounded input parameters
  • Review weight calculation functions

Step 6: Origin & Privilege Review

# Find privileged operations
rg "ensure_signed" pallets/ | grep -E "pause|emergency|admin|force|sudo"

# Should use ensure_root or custom origins
rg "ensure_root|ForceOrigin|AdminOrigin" pallets/

Step 7: Testing Review

  • Unit tests cover all dispatchables
  • Fuzz tests for panic conditions
  • Benchmarks for weight calculation
  • try-runtime tests for migrations

Step 8: Report Coverage

Report on every pattern in §5, whether or not it turned anything up. This skill has no Finding Template, so the table opens the report and the findings follow it, with all 7 rows present:

# Pattern Verdict Evidence
1 Arithmetic Overflow found src/lib.rs:212 -- + on BalanceOf<T> in do_transfer
2 Don't Panic
3 Weights and Fees
4 Verify First, Write Last
5 Unsigned Transaction Validation
6 Bad Randomness
7 Bad Origin

Each verdict is one of:

  • found — cite file:line and write the finding up in full after the table.
  • clear — the pattern applies to this pallet and the pallet handles it. Name the macro, origin check, or arithmetic method you searched for, so a reader can repeat the search.
  • n/a — the pattern cannot apply here. Give the reason in one clause ("this pallet accepts no unsigned transactions"). Not having looked is not n/a. Note that pattern 4 is version-scoped (pre-v0.9.25): say which runtime version the pallet targets rather than dropping the row.

A table with fewer than 7 rows is an incomplete scan and must be reported as one. A row whose Verdict cell is empty is incomplete in the same way: row 1 above is filled in to show the shape, and every row is filled in the same way before the report is done. Seven clear verdicts is a result a reader can act on. A report that covers three patterns and says nothing about the other four reads exactly like a clean pallet, and that is the failure this table exists to prevent.


7. Priority Guidelines

Critical (Immediate Fix Required)

  • Arithmetic overflow (token creation, balance manipulation)
  • Panic DoS (node crash risk)
  • Bad origin (unauthorized privileged operations)

High (Fix Before Launch)

  • Incorrect weights (DoS via spam)
  • Verify-first violations (state corruption, pre-v0.9.25)
  • Unsigned validation issues (spam, replay attacks)

Medium (Address in Audit)

  • Bad randomness (manipulation possible but limited impact)

8. Testing Recommendations

Fuzz Testing

// Use test-fuzz for property-based testing
#[cfg(test)]
mod tests {
    use test_fuzz::test_fuzz;

    #[test_fuzz]
    fn fuzz_transfer(from: AccountId, to: AccountId, amount: u128) {
        // Should never panic
        let _ = Pallet::transfer(from, to, amount);
    }

    #[test_fuzz]
    fn fuzz_no_panics(call: Call) {
        // No dispatchable should panic
        let _ = call.dispatch(origin);
    }
}

Benchmarking

# Run benchmarks to generate weights
cargo build --release --features runtime-benchmarks
./target/release/node benchmark pallet \
    --chain dev \
    --pallet pallet_example \
    --extrinsic "*" \
    --steps 50 \
    --repeat 20

try-runtime

# Test runtime upgrades
cargo build --release --features try-runtime
try-runtime --runtime ./target/release/wbuild/runtime.wasm \
    on-runtime-upgrade live --uri wss://rpc.polkadot.io

9. Additional Resources


10. Quick Reference Checklist

Before completing Substrate pallet audit:

Arithmetic Safety (CRITICAL):

  • No direct +, -, *, / operators in dispatchables
  • All arithmetic uses checked_* or saturating_*
  • Type conversions use try_into() with error handling

Panic Prevention (CRITICAL):

  • No unwrap() or expect() in dispatchables
  • No direct array/slice indexing without bounds check
  • All user inputs validated with ensure!
  • Division operations check for zero divisor

Weights & DoS (CRITICAL):

  • Weights proportional to computational cost
  • Input parameters have maximum bounds
  • Benchmarking used to determine weights
  • No free (zero-weight) expensive operations

Access Control (CRITICAL):

  • Privileged operations use ensure_root or custom origins
  • ensure_signed only for user-level operations
  • Origin types properly configured in runtime
  • Sudo pallet removed before production

Storage Safety (HIGH):

  • Using Substrate v0.9.25+ OR manual #[transactional]
  • Validation before storage writes
  • Events emitted after successful operations

Other (MEDIUM):

  • Unsigned transactions use signed alternative if possible
  • If unsigned: proper validation, replay protection, authentication
  • BABE randomness used (not RandomnessCollectiveFlip)
  • Randomness uses random(subject) not random_seed()

Testing:

  • Unit tests for all dispatchables
  • Fuzz tests to find panics
  • Benchmarks generated and verified
  • try-runtime tests for migrations
  • Coverage table emitted with all 7 rows, each carrying a verdict of found, clear or n/a with a reason

11. Rationalizations to Reject

  • "The pallet is small, so most patterns obviously don't apply." Obvious to whom? An n/a costs one clause and makes the judgment reviewable. Silence records nothing, and a reader cannot tell it apart from not having checked.
  • "It compiles without warnings, so the arithmetic is fine." Release builds wrap silently. + on a Balance is pattern 1 whether or not the compiler said anything, and debug assertions do not run on a production node.
  • "I checked the patterns that matter for this pallet." Deciding which patterns matter is the scan, not a precondition for starting it. Rank by severity after the table is complete, not by leaving rows out.
  • "No findings, so there is nothing to report." A zero-finding scan still emits the full coverage table. That table is the deliverable: it is what distinguishes a pallet that was examined from one that was glanced at.
  • "ensure_signed is an origin check." It establishes that someone signed, not that the right someone did. Pattern 7 is about which origin is required, and ensure_signed where ensure_root belongs passes this rationalization while failing the check.
  • "The weight is benchmarked, so it's correct." Benchmarks measure the path they exercise. A dispatchable whose worst case depends on storage size or an unbounded loop is pattern 3 even with a benchmark attached.
Files (skills)
  • agents
    • openai.yaml 258 B
      interface:
        display_name: "Substrate Vulnerability Scanner"
        short_description: "Audit Substrate and Polkadot pallets for vulnerabilities"
        icon_small: "assets/trail-of-bits-mark.svg"
        icon_large: "assets/trail-of-bits-mark.svg"
        brand_color: "#D83A34"
      
  • assets
    • trail-of-bits-mark.svg 3 KB · in bundle
  • resources
    • VULNERABILITY_PATTERNS.md 24.3 KB
      # Substrate Vulnerability Patterns (7 Patterns)
      
      This document contains detailed descriptions, detection patterns, and mitigations for 7 critical Substrate/FRAME vulnerabilities.
      
      ---
      
      ## 6.1 ARITHMETIC OVERFLOW ⚠️ CRITICAL
      
      **Description**: Primitive integer types wrap in release mode instead of panicking on overflow/underflow. In debug mode they panic, but production builds silently produce incorrect values.
      
      **Detection Patterns**:
      ```rust
      // VULNERABLE: Direct arithmetic on primitive types
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          pub fn transfer(origin: OriginFor<T>, amount: u128) -> DispatchResult {
              let sender = ensure_signed(origin)?;
              let mut balance = Self::balance_of(&sender);
      
              // OVERFLOW: balance - amount wraps in release mode!
              balance = balance - amount;  // If amount > balance, wraps to huge number
      
              // UNDERFLOW: balance + amount wraps in release mode!
              balance = balance + amount;  // If balance + amount > u128::MAX, wraps to small number
      
              Self::set_balance(&sender, balance);
              Ok(())
          }
      }
      
      // VULNERABLE: Multiplication overflow
      let total = price * quantity;  // Wraps if price * quantity > u128::MAX
      ```
      
      **What to Check**:
      - [ ] NO direct arithmetic operators (`+`, `-`, `*`, `/`) on primitive types in dispatchables
      - [ ] ALL arithmetic uses `checked_*`, `saturating_*`, or `overflowing_*` methods
      - [ ] Balance updates use safe arithmetic
      - [ ] Reward/fee calculations use safe arithmetic
      - [ ] Type conversions checked for overflow
      
      **Mitigation**:
      ```rust
      // SECURE: Use checked arithmetic
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          pub fn transfer(origin: OriginFor<T>, amount: u128) -> DispatchResult {
              let sender = ensure_signed(origin)?;
              let balance = Self::balance_of(&sender);
      
              // checked_sub returns None on underflow
              let new_balance = balance.checked_sub(amount)
                  .ok_or(Error::<T>::InsufficientBalance)?;
      
              Self::set_balance(&sender, new_balance);
              Ok(())
          }
      
          pub fn calculate_reward(stakes: u128, rate: u128) -> Result<u128, Error<T>> {
              // checked_mul returns None on overflow
              stakes.checked_mul(rate)
                  .ok_or(Error::<T>::ArithmeticOverflow)
          }
      }
      
      // SECURE: Use saturating arithmetic (when clamping is acceptable)
      use sp_runtime::traits::Saturating;
      
      let new_value = old_value.saturating_add(increment);  // Clamps at u128::MAX
      let new_value = old_value.saturating_sub(decrement);  // Clamps at 0
      ```
      
      **Available Safe Methods**:
      ```rust
      // CheckedAdd, CheckedSub, CheckedMul, CheckedDiv traits
      value.checked_add(other)?
      value.checked_sub(other)?
      value.checked_mul(other)?
      value.checked_div(other)?
      
      // Saturating trait (clamps at min/max)
      value.saturating_add(other)
      value.saturating_sub(other)
      value.saturating_mul(other)
      
      // Overflowing (returns bool flag)
      let (result, overflowed) = value.overflowing_add(other);
      ensure!(!overflowed, Error::<T>::ArithmeticOverflow);
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/arithmetic_overflow
      
      ---
      
      ## 4.2 DON'T PANIC ⚠️ CRITICAL - DoS
      
      **Description**: Panics in dispatchable functions cause the node to stop processing blocks, enabling DoS attacks. Production runtime must never panic.
      
      **Detection Patterns**:
      
      ### Pattern 1: Array Indexing
      ```rust
      // VULNERABLE: Direct array indexing panics on out-of-bounds
      pub fn get_validator(index: u32) -> DispatchResult {
          let validators = Self::validator_set();
          let validator = validators[index as usize];  // PANIC if index >= len!
          // ...
      }
      ```
      
      ### Pattern 2: unwrap() / expect()
      ```rust
      // VULNERABLE: unwrap() panics on None
      pub fn process_data(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
          let value = Self::parse_data(&data).unwrap();  // PANIC on parse error!
          // ...
      }
      
      // VULNERABLE: expect() also panics
      let sender = ensure_signed(origin).expect("must be signed");  // PANIC!
      ```
      
      ### Pattern 3: Type Conversions
      ```rust
      // VULNERABLE: as casts can panic or produce wrong values
      pub fn set_value(origin: OriginFor<T>, value: u128) -> DispatchResult {
          let small_value = value as u32;  // Truncates if value > u32::MAX!
          Self::store_value(small_value);
      }
      ```
      
      ### Pattern 4: Missing Input Validation
      ```rust
      // VULNERABLE: No bounds checking on user input
      pub fn divide(numerator: u128, denominator: u128) -> DispatchResult {
          let result = numerator / denominator;  // PANIC if denominator == 0!
          // ...
      }
      ```
      
      **What to Check**:
      - [ ] NO array/slice indexing without bounds check
      - [ ] NO `unwrap()`, `expect()` in dispatchables
      - [ ] NO `as` casts without validation
      - [ ] ALL user input validated before use
      - [ ] Division operations check for zero divisor
      - [ ] All `?` operator paths return DispatchError, not panic
      
      **Mitigation**:
      ```rust
      // SECURE: Bounds checking for array access
      pub fn get_validator(index: u32) -> DispatchResult {
          let validators = Self::validator_set();
          let validator = validators.get(index as usize)
              .ok_or(Error::<T>::ValidatorIndexOutOfBounds)?;
          // ...
      }
      
      // SECURE: Handle Result/Option properly
      pub fn process_data(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
          let value = Self::parse_data(&data)
              .map_err(|_| Error::<T>::InvalidData)?;
          // ...
      }
      
      // SECURE: Safe type conversions
      use sp_runtime::traits::TryConvert;
      
      pub fn set_value(origin: OriginFor<T>, value: u128) -> DispatchResult {
          let small_value: u32 = value.try_into()
              .map_err(|_| Error::<T>::ValueTooLarge)?;
          Self::store_value(small_value);
          Ok(())
      }
      
      // SECURE: Input validation with ensure!
      pub fn divide(numerator: u128, denominator: u128) -> DispatchResult {
          ensure!(denominator != 0, Error::<T>::DivisionByZero);
          let result = numerator / denominator;  // Safe now
          // ...
      }
      
      // SECURE: Comprehensive validation
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          pub fn create_proposal(
              origin: OriginFor<T>,
              title: Vec<u8>,
              description: Vec<u8>,
          ) -> DispatchResult {
              let proposer = ensure_signed(origin)?;
      
              // Validate all inputs
              ensure!(title.len() <= 100, Error::<T>::TitleTooLong);
              ensure!(description.len() <= 1000, Error::<T>::DescriptionTooLong);
              ensure!(!title.is_empty(), Error::<T>::TitleEmpty);
      
              // All validation passed, safe to proceed
              Self::store_proposal(proposer, title, description)?;
              Ok(())
          }
      }
      ```
      
      **Testing for Panics**:
      ```rust
      // Use test-fuzz to find panic conditions
      #[cfg(test)]
      mod tests {
          use super::*;
      
          #[test]
          #[should_panic]
          fn test_panics_on_invalid_input() {
              // This test should NOT panic in production code
              // If it does, you found a vulnerability
              Pallet::divide(100, 0).unwrap();
          }
      
          // Fuzz testing
          #[test_fuzz::test_fuzz]
          fn fuzz_transfer(sender: AccountId, amount: u128) {
              // Should never panic regardless of input
              let _ = Pallet::transfer(sender, amount);
          }
      }
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/dont_panic
      
      ---
      
      ## 4.3 WEIGHTS AND FEES ⚠️ CRITICAL - DoS
      
      **Description**: Incorrect weight functions allow cheap calls to expensive operations, enabling DoS attacks by spamming low-fee transactions that consume excessive resources.
      
      **Detection Patterns**:
      ```rust
      // VULNERABLE: Fixed weight for variable-cost operation
      #[pallet::weight(10_000)]  // Same cost regardless of input!
      pub fn process_items(origin: OriginFor<T>, items: Vec<Item>) -> DispatchResult {
          // Processing cost grows with items.len(), but weight is fixed!
          for item in items {
              Self::expensive_operation(item);  // O(n) operation
          }
          Ok(())
      }
      
      // VULNERABLE: No bounds on input size
      #[pallet::weight(items.len() as u64 * 1000)]  // Weight grows with input
      pub fn process_items(origin: OriginFor<T>, items: Vec<Item>) -> DispatchResult {
          // But no maximum limit! Attacker can send items.len() = 1 billion
          // and cause block to exceed weight limit
          Ok(())
      }
      
      // VULNERABLE: Zero weight for non-trivial operation
      #[pallet::weight(0)]  // FREE operation!
      pub fn expensive_computation(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
          let _ = Self::compute_hash_10000_times(&data);  // Expensive but free!
          Ok(())
      }
      ```
      
      **What to Check**:
      - [ ] Weight functions account for input size
      - [ ] Loops have bounded iterations
      - [ ] Storage access counted in weight
      - [ ] Upper bounds enforced on Vec/array parameters
      - [ ] Weights determined empirically via benchmarking
      - [ ] No free/zero-weight dispatchables (unless trivial)
      
      **Mitigation**:
      ```rust
      // SECURE: Weight proportional to input size with bounds
      #[pallet::weight({
          // Ensure items.len() has a reasonable maximum
          let bounded_len = items.len().min(T::MaxItems::get() as usize);
          T::DbWeight::get().reads_writes(bounded_len as u64, bounded_len as u64)
              .saturating_add(T::WeightPerItem::get().saturating_mul(bounded_len as u64))
      })]
      pub fn process_items(origin: OriginFor<T>, items: Vec<Item>) -> DispatchResult {
          // Enforce maximum items
          ensure!(items.len() <= T::MaxItems::get() as usize, Error::<T>::TooManyItems);
      
          for item in items {
              Self::expensive_operation(item);
          }
          Ok(())
      }
      
      // SECURE: Use benchmarking framework
      #[pallet::weight(T::WeightInfo::transfer())]
      pub fn transfer(
          origin: OriginFor<T>,
          dest: T::AccountId,
          amount: BalanceOf<T>,
      ) -> DispatchResult {
          // Weight calculated by benchmarking.rs
          // ...
      }
      
      // BETTER: Benchmark with input parameters
      #[pallet::weight(T::WeightInfo::process_items(items.len() as u32))]
      pub fn process_items(origin: OriginFor<T>, items: Vec<Item>) -> DispatchResult {
          ensure!(items.len() <= T::MaxItems::get() as usize, Error::<T>::TooManyItems);
          // ...
      }
      ```
      
      **Benchmarking**:
      ```rust
      // benchmarking.rs - Empirically determine weights
      #[benchmarks]
      mod benchmarks {
          use super::*;
      
          #[benchmark]
          fn process_items(n: Linear<1, 100>) {
              let items: Vec<Item> = (0..n).map(|i| Item::new(i)).collect();
      
              #[extrinsic_call]
              process_items(RawOrigin::Signed(caller), items);
      
              // Verify operation succeeded
              assert!(SomeStorage::<T>::get().is_some());
          }
      }
      ```
      
      **Configuration Constants**:
      ```rust
      #[pallet::config]
      pub trait Config: frame_system::Config {
          // Define maximum bounds
          #[pallet::constant]
          type MaxItems: Get<u32>;
      
          #[pallet::constant]
          type MaxDataSize: Get<u32>;
      
          // Weight info from benchmarking
          type WeightInfo: WeightInfo;
      }
      
      // In runtime/lib.rs
      impl pallet_example::Config for Runtime {
          type MaxItems = ConstU32<100>;  // Max 100 items per call
          type MaxDataSize = ConstU32<10_000>;  // Max 10KB data
          type WeightInfo = pallet_example::weights::SubstrateWeight<Runtime>;
      }
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/weights_fees
      
      ---
      
      ## 4.4 VERIFY FIRST, WRITE LAST ⚠️ HIGH (Pre-v0.9.25)
      
      **Description**: In Substrate versions before v0.9.25, storage writes before validation persist even if the dispatch later fails, allowing attackers to modify state without paying the full cost.
      
      **Detection Patterns**:
      ```rust
      // VULNERABLE: Storage write before validation (pre-v0.9.25)
      pub fn claim_reward(origin: OriginFor<T>) -> DispatchResult {
          let claimer = ensure_signed(origin)?;
      
          // WRONG: Writing to storage before all validation!
          <ClaimCount<T>>::mutate(|count| *count += 1);
      
          // Validation happens AFTER storage write
          let reward = Self::calculate_reward(&claimer)?;
          ensure!(reward > 0, Error::<T>::NoReward);
      
          // If this fails, ClaimCount was still incremented!
          Self::transfer_reward(&claimer, reward)?;
      
          Ok(())
      }
      
      // VULNERABLE: Event emitted before validation
      pub fn submit_proposal(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
          let proposer = ensure_signed(origin)?;
      
          // WRONG: Event before validation
          Self::deposit_event(Event::ProposalSubmitted(proposer.clone()));
      
          // Validation after event
          ensure!(data.len() <= 1000, Error::<T>::DataTooLarge);
      
          Ok(())
      }
      ```
      
      **What to Check**:
      - [ ] Using Substrate v0.9.25+ (transactional storage layer)
      - [ ] OR all validation happens BEFORE any storage writes
      - [ ] OR manual `#[transactional]` attribute used
      - [ ] Events emitted AFTER all validation and state changes
      - [ ] Pattern: validate → write → emit event
      
      **Mitigation**:
      ```rust
      // OPTION 1: Upgrade to v0.9.25+ (automatic transactional storage)
      // Storage writes automatically rolled back on error
      
      // OPTION 2: Verify First, Write Last pattern
      pub fn claim_reward(origin: OriginFor<T>) -> DispatchResult {
          let claimer = ensure_signed(origin)?;
      
          // ALL VALIDATION FIRST
          let reward = Self::calculate_reward(&claimer)?;
          ensure!(reward > 0, Error::<T>::NoReward);
          ensure!(Self::can_transfer(&claimer, reward)?, Error::<T>::TransferFailed);
      
          // THEN ALL WRITES
          <ClaimCount<T>>::mutate(|count| *count += 1);
          Self::transfer_reward(&claimer, reward)?;
      
          // FINALLY EVENTS
          Self::deposit_event(Event::RewardClaimed {
              claimer,
              amount: reward,
          });
      
          Ok(())
      }
      
      // OPTION 3: Manual transactional attribute (pre-v0.9.25)
      use frame_support::transactional;
      
      #[transactional]  // Rollback all storage on error
      pub fn claim_reward(origin: OriginFor<T>) -> DispatchResult {
          // Storage writes rolled back if function returns Err
          Ok(())
      }
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/verify_first
      
      ---
      
      ## 4.5 UNSIGNED TRANSACTION VALIDATION ⚠️ HIGH
      
      **Description**: Insufficient validation in `ValidateUnsigned` trait allows spam, replay attacks, or spoofed data from offchain workers or external sources.
      
      **Detection Patterns**:
      ```rust
      // VULNERABLE: No validation in validate_unsigned
      #[pallet::validate_unsigned]
      impl<T: Config> ValidateUnsigned for Pallet<T> {
          type Call = Call<T>;
      
          fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
              match call {
                  Call::submit_price { price, .. } => {
                      // WRONG: No validation of price source or replay protection!
                      ValidTransaction::with_tag_prefix("OffchainWorker")
                          .priority(100)
                          .build()
                  }
                  _ => InvalidTransaction::Call.into(),
              }
          }
      }
      
      // VULNERABLE: No replay protection
      pub fn submit_data(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
          ensure_none(origin)?;  // Unsigned transaction
          // WRONG: Same data can be submitted multiple times!
          Self::process_data(data)?;
          Ok(())
      }
      ```
      
      **What to Check**:
      - [ ] Consider using signed transactions instead (strongly preferred)
      - [ ] IF unsigned is necessary:
        - [ ] All parameters validated in `validate_unsigned`
        - [ ] Replay protection via nonce or one-time tag
        - [ ] Data source authenticated (cryptographic signature)
        - [ ] Rate limiting or spam protection
        - [ ] Priority set appropriately (high for OCW, low for user-submitted)
      
      **Mitigation**:
      ```rust
      // OPTION 1: Use signed transactions (PREFERRED)
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          #[pallet::weight(10_000)]
          pub fn submit_price(
              origin: OriginFor<T>,  // Signed
              price: u128,
              signature: Signature,
          ) -> DispatchResult {
              let signer = ensure_signed(origin)?;
              // Signed transaction provides natural replay protection and authentication
              Ok(())
          }
      }
      
      // OPTION 2: Proper unsigned validation (if truly necessary)
      #[pallet::validate_unsigned]
      impl<T: Config> ValidateUnsigned for Pallet<T> {
          type Call = Call<T>;
      
          fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity {
              match call {
                  Call::submit_price { price, block_number, signature } => {
                      // 1. Validate signature from authorized source
                      let public_key = Self::authority_key();
                      ensure!(signature.verify(price, block_number, &public_key),
                          InvalidTransaction::BadProof);
      
                      // 2. Replay protection - check block_number is current
                      let current_block = <frame_system::Pallet<T>>::block_number();
                      ensure!(block_number == current_block,
                          InvalidTransaction::Stale);
      
                      // 3. One-time submission via unique tag
                      let tag = (b"price", block_number).encode();
      
                      ValidTransaction::with_tag_prefix("OffchainWorker")
                          .priority(TransactionPriority::MAX)
                          .and_provides(tag)  // Prevents duplicate submission
                          .longevity(5)  // Valid for 5 blocks
                          .propagate(true)
                          .build()
                  }
                  _ => InvalidTransaction::Call.into(),
              }
          }
      }
      
      // With replay protection in dispatch
      pub fn submit_price(
          origin: OriginFor<T>,
          price: u128,
          block_number: T::BlockNumber,
      ) -> DispatchResult {
          ensure_none(origin)?;
      
          // Check not already processed
          ensure!(!<ProcessedBlocks<T>>::contains_key(block_number),
              Error::<T>::AlreadyProcessed);
      
          // Mark as processed
          <ProcessedBlocks<T>>::insert(block_number, true);
      
          // Process data
          Self::update_price(price)?;
      
          Ok(())
      }
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/unsigned_validation
      
      ---
      
      ## 4.6 BAD RANDOMNESS ⚠️ MEDIUM
      
      **Description**: Using low-security randomness source (`pallet_randomness_collective_flip`) in production allows validator collusion or manipulation.
      
      **Detection Patterns**:
      ```rust
      // VULNERABLE: Using RandomnessCollectiveFlip in production
      impl pallet_example::Config for Runtime {
          type Randomness = pallet_randomness_collective_flip::RandomnessCollectiveFlip;
      }
      
      // VULNERABLE: Using random_seed() instead of random()
      pub fn draw_winner(origin: OriginFor<T>) -> DispatchResult {
          let seed = T::Randomness::random_seed();  // WRONG: Doesn't incorporate subject
          let winner_index = Self::pick_from_seed(&seed);
          // ...
      }
      
      // VULNERABLE: Using randomness for critical security
      pub fn generate_secret_key(origin: OriginFor<T>) -> DispatchResult {
          let random = T::Randomness::random(&b"secret"[..]);
          // BAD: Even with BABE, not sufficient for cryptographic keys!
          let secret_key = Self::derive_key(random.0);
          // ...
      }
      ```
      
      **What to Check**:
      - [ ] NOT using `pallet_randomness_collective_flip` in production
      - [ ] Using BABE randomness (`pallet_babe::RandomnessFromOneEpochAgo`)
      - [ ] Using `random(subject)` instead of `random_seed()`
      - [ ] NOT using on-chain randomness for cryptographic keys
      - [ ] Understanding randomness can be influenced by validators
      
      **Mitigation**:
      ```rust
      // SECURE: Use BABE randomness (production)
      impl pallet_example::Config for Runtime {
          // Use BABE VRF for production randomness
          type Randomness = pallet_babe::RandomnessFromOneEpochAgo<Runtime>;
      }
      
      // SECURE: Use random() with subject, not random_seed()
      pub fn draw_winner(origin: OriginFor<T>) -> DispatchResult {
          let _ = ensure_signed(origin)?;
      
          // Incorporate subject to make each call's randomness unique
          let (random_hash, _block_number) = T::Randomness::random(&b"lottery"[..]);
      
          // Convert to integer for selection
          let random_number = u32::from_le_bytes([
              random_hash.as_ref()[0],
              random_hash.as_ref()[1],
              random_hash.as_ref()[2],
              random_hash.as_ref()[3],
          ]);
      
          let winner_index = random_number % Self::participant_count();
          Self::award_prize(winner_index)?;
      
          Ok(())
      }
      
      // SECURE: Don't use on-chain randomness for crypto keys
      // Instead: generate off-chain, store hash on-chain for verification
      pub fn register_key(origin: OriginFor<T>, key_hash: Hash) -> DispatchResult {
          // Store hash of externally-generated key
          // User generates key off-chain with proper entropy
          Ok(())
      }
      ```
      
      **Randomness Quality by Source**:
      ```rust
      // NOT SECURE for production
      pallet_randomness_collective_flip  // Low influence, collusion risk
      
      // SECURE for production
      pallet_babe::RandomnessFromOneEpochAgo  // VRF-based, high security
      
      // SECURE for testing only
      pallet_insecure_randomness_collective_flip  // Explicit "insecure" naming
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/randomness
      
      ---
      
      ## 4.7 BAD ORIGIN ⚠️ CRITICAL
      
      **Description**: Using `ensure_signed` for privileged operations instead of proper origin validation (`ensure_root`, custom origins) allows unauthorized access.
      
      **Detection Patterns**:
      ```rust
      // VULNERABLE: Using ensure_signed for privileged operation
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          pub fn emergency_pause(origin: OriginFor<T>) -> DispatchResult {
              let _caller = ensure_signed(origin)?;  // WRONG: Any signed user!
              <SystemPaused<T>>::put(true);  // Critical operation with no real authorization
              Ok(())
          }
      
          pub fn set_global_config(
              origin: OriginFor<T>,
              new_fee: Balance,
          ) -> DispatchResult {
              let _caller = ensure_signed(origin)?;  // WRONG: Any user can change fees!
              <GlobalFee<T>>::put(new_fee);
              Ok(())
          }
      }
      
      // VULNERABLE: Not using configured ForceOrigin
      pub fn force_transfer(
          origin: OriginFor<T>,
          from: T::AccountId,
          to: T::AccountId,
      ) -> DispatchResult {
          let _admin = ensure_signed(origin)?;  // WRONG: Should check ForceOrigin!
          // Should be: T::ForceOrigin::ensure_origin(origin)?;
          Self::do_transfer(from, to)?;
          Ok(())
      }
      ```
      
      **What to Check**:
      - [ ] Root-level operations use `ensure_root(origin)?`
      - [ ] Privileged operations use custom origin types (ForceOrigin, AdminOrigin)
      - [ ] `ensure_signed` only for user-level operations
      - [ ] Origin configuration documented for governance
      - [ ] Sudo privileges removed before production launch
      
      **Mitigation**:
      ```rust
      // SECURE: Use ensure_root for root operations
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          #[pallet::weight(10_000)]
          pub fn emergency_pause(origin: OriginFor<T>) -> DispatchResult {
              ensure_root(origin)?;  // Only root account
              <SystemPaused<T>>::put(true);
              Self::deposit_event(Event::SystemPaused);
              Ok(())
          }
      }
      
      // SECURE: Use custom origin types for privileged operations
      #[pallet::config]
      pub trait Config: frame_system::Config {
          /// Origin that can execute force operations
          type ForceOrigin: EnsureOrigin<Self::RuntimeOrigin>;
      
          /// Origin that can update parameters
          type UpdateOrigin: EnsureOrigin<Self::RuntimeOrigin>;
      }
      
      #[pallet::call]
      impl<T: Config> Pallet<T> {
          #[pallet::weight(10_000)]
          pub fn force_transfer(
              origin: OriginFor<T>,
              from: T::AccountId,
              to: T::AccountId,
              amount: BalanceOf<T>,
          ) -> DispatchResult {
              // Validate against configured ForceOrigin
              T::ForceOrigin::ensure_origin(origin)?;
      
              Self::do_transfer(from, to, amount)?;
              Ok(())
          }
      
          #[pallet::weight(10_000)]
          pub fn update_fee(
              origin: OriginFor<T>,
              new_fee: BalanceOf<T>,
          ) -> DispatchResult {
              // Validate against configured UpdateOrigin
              T::UpdateOrigin::ensure_origin(origin)?;
      
              <GlobalFee<T>>::put(new_fee);
              Self::deposit_event(Event::FeeUpdated { new_fee });
              Ok(())
          }
      
          #[pallet::weight(10_000)]
          pub fn transfer(
              origin: OriginFor<T>,
              to: T::AccountId,
              amount: BalanceOf<T>,
          ) -> DispatchResult {
              // Regular user operation - ensure_signed is correct
              let from = ensure_signed(origin)?;
      
              Self::do_transfer(from, to, amount)?;
              Ok(())
          }
      }
      
      // Runtime configuration
      impl pallet_example::Config for Runtime {
          // Require governance approval for force operations
          type ForceOrigin = EnsureRootOrHalfCouncil;
      
          // Require 2/3 council for parameter updates
          type UpdateOrigin = EnsureRootOrTwoThirdsCouncil;
      }
      ```
      
      **Origin Types Reference**:
      ```rust
      // Built-in origins
      ensure_root(origin)?              // Root/sudo only
      ensure_signed(origin)?            // Any signed account
      ensure_none(origin)?              // Unsigned (use with extreme caution)
      
      // Custom origins (configured in runtime)
      T::ForceOrigin::ensure_origin(origin)?
      T::UpdateOrigin::ensure_origin(origin)?
      T::AdminOrigin::ensure_origin(origin)?
      
      // Common origin configurations
      EnsureRoot                        // Root only
      EnsureSigned                      // Any signed
      EnsureSignedBy<AccountId>         // Specific account
      EnsureRootOrHalfCouncil          // Root or 50%+ council
      EnsureRootOrTwoThirdsCouncil     // Root or 67%+ council
      ```
      
      **References**: building-secure-contracts/not-so-smart-contracts/substrate/bad_origin
      
  • SKILL.md 12.5 KB
    ---
    name: substrate-vulnerability-scanner
    description: Scans Substrate/Polkadot pallets for 7 critical vulnerabilities including arithmetic overflow, panic DoS, incorrect weights, and bad origin checks. Use when auditing Substrate runtimes or FRAME pallets.
    ---
    
    # Substrate Vulnerability Scanner
    
    ## 1. Purpose
    
    Systematically scan Substrate runtime modules (pallets) for platform-specific security vulnerabilities that can cause node crashes, DoS attacks, or unauthorized access. This skill encodes 7 critical vulnerability patterns unique to Substrate/FRAME-based chains.
    
    ## 2. When to Use This Skill
    
    - Auditing custom Substrate pallets
    - Reviewing FRAME runtime code
    - Pre-launch security assessment of Substrate chains (Polkadot parachains, standalone chains)
    - Validating dispatchable extrinsic functions
    - Reviewing weight calculation functions
    - Assessing unsigned transaction validation logic
    
    ## 3. Platform Detection
    
    ### File Extensions & Indicators
    - **Rust files**: `.rs`
    
    ### Language/Framework Markers
    ```rust
    // Substrate/FRAME indicators
    #[pallet]
    pub mod pallet {
        use frame_support::pallet_prelude::*;
        use frame_system::pallet_prelude::*;
    
        #[pallet::config]
        pub trait Config: frame_system::Config { }
    
        #[pallet::call]
        impl<T: Config> Pallet<T> {
            #[pallet::weight(10_000)]
            pub fn example_function(origin: OriginFor<T>) -> DispatchResult { }
        }
    }
    
    // Common patterns
    DispatchResult, DispatchError
    ensure!, ensure_signed, ensure_root
    StorageValue, StorageMap, StorageDoubleMap
    #[pallet::storage]
    #[pallet::call]
    #[pallet::weight]
    #[pallet::validate_unsigned]
    ```
    
    ### Project Structure
    - `pallets/*/lib.rs` - Pallet implementations
    - `runtime/lib.rs` - Runtime configuration
    - `benchmarking.rs` - Weight benchmarks
    - `Cargo.toml` with `frame-*` dependencies
    
    ### Tool Support
    - **cargo-fuzz**: Fuzz testing for Rust
    - **test-fuzz**: Property-based testing framework
    - **benchmarking framework**: Built-in weight calculation
    - **try-runtime**: Runtime migration testing
    
    ---
    
    ## 4. How This Skill Works
    
    When invoked, I will:
    
    1. **Search your codebase** for Substrate pallets
    2. **Analyze each pallet** for the 7 vulnerability patterns
    3. **Report findings** with file references and severity, above them a coverage table carrying a verdict for every pattern
    4. **Provide fixes** for each identified issue
    5. **Check weight calculations** and origin validation
    
    ---
    
    ## 5. Vulnerability Patterns (7 Critical Patterns)
    
    I check for 7 critical vulnerability patterns unique to Substrate/FRAME. For detailed detection patterns, code examples, mitigations, and testing strategies, see [VULNERABILITY_PATTERNS.md](resources/VULNERABILITY_PATTERNS.md).
    
    ### Pattern Summary:
    
    1. **Arithmetic Overflow** ⚠️ CRITICAL
       - Direct `+`, `-`, `*`, `/` operators wrap in release mode
       - Must use `checked_*` or `saturating_*` methods
       - Affects balance/token calculations, reward/fee math
    
    2. **Don't Panic** ⚠️ CRITICAL - DoS
       - Panics cause node to stop processing blocks
       - No `unwrap()`, `expect()`, array indexing without bounds check
       - All user input must be validated with `ensure!`
    
    3. **Weights and Fees** ⚠️ CRITICAL - DoS
       - Incorrect weights allow spam attacks
       - Fixed weights for variable-cost operations enable DoS
       - Must use benchmarking framework, bound all input parameters
    
    4. **Verify First, Write Last** ⚠️ HIGH (Pre-v0.9.25)
       - Storage writes before validation persist on error (pre-v0.9.25)
       - Pattern: validate → write → emit event
       - Upgrade to v0.9.25+ or use manual `#[transactional]`
    
    5. **Unsigned Transaction Validation** ⚠️ HIGH
       - Insufficient validation allows spam/replay attacks
       - Prefer signed transactions
       - If unsigned: validate parameters, replay protection, authenticate source
    
    6. **Bad Randomness** ⚠️ MEDIUM
       - `pallet_randomness_collective_flip` vulnerable to collusion
       - Must use BABE randomness (`pallet_babe::RandomnessFromOneEpochAgo`)
       - Use `random(subject)` not `random_seed()`
    
    7. **Bad Origin** ⚠️ CRITICAL
       - `ensure_signed` allows any user for privileged operations
       - Must use `ensure_root` or custom origins (ForceOrigin, AdminOrigin)
       - Origin types must be properly configured in runtime
    
    For complete vulnerability patterns with code examples, see [VULNERABILITY_PATTERNS.md](resources/VULNERABILITY_PATTERNS.md).
    
    ---
    
    ## 6. Scanning Workflow
    
    ### Step 1: Platform Identification
    1. Verify Substrate/FRAME framework usage
    2. Check Substrate version (v0.9.25+ has transactional storage)
    3. Locate pallet implementations (`pallets/*/lib.rs`)
    4. Identify runtime configuration (`runtime/lib.rs`)
    
    ### Step 2: Dispatchable Analysis
    For each `#[pallet::call]` function:
    - [ ] Arithmetic: Uses checked/saturating operations?
    - [ ] Panics: No unwrap/expect/indexing?
    - [ ] Weights: Proportional to cost, bounded inputs?
    - [ ] Origin: Appropriate validation level?
    - [ ] Validation: All checks before storage writes?
    
    ### Step 3: Panic Sweep
    ```bash
    # Search for panic-prone patterns
    rg "unwrap\(\)" pallets/
    rg "expect\(" pallets/
    rg "\[.*\]" pallets/  # Array indexing
    rg " as u\d+" pallets/  # Type casts
    rg "\.unwrap_or" pallets/
    ```
    
    ### Step 4: Arithmetic Safety Check
    ```bash
    # Find direct arithmetic
    rg " \+ |\+=| - |-=| \* |\*=| / |/=" pallets/
    
    # Should find checked/saturating alternatives instead
    rg "checked_add|checked_sub|checked_mul|checked_div" pallets/
    rg "saturating_add|saturating_sub|saturating_mul" pallets/
    ```
    
    ### Step 5: Weight Analysis
    - [ ] Run benchmarking: `cargo test --features runtime-benchmarks`
    - [ ] Verify weights match computational cost
    - [ ] Check for bounded input parameters
    - [ ] Review weight calculation functions
    
    ### Step 6: Origin & Privilege Review
    ```bash
    # Find privileged operations
    rg "ensure_signed" pallets/ | grep -E "pause|emergency|admin|force|sudo"
    
    # Should use ensure_root or custom origins
    rg "ensure_root|ForceOrigin|AdminOrigin" pallets/
    ```
    
    ### Step 7: Testing Review
    - [ ] Unit tests cover all dispatchables
    - [ ] Fuzz tests for panic conditions
    - [ ] Benchmarks for weight calculation
    - [ ] try-runtime tests for migrations
    
    ### Step 8: Report Coverage
    
    Report on every pattern in §5, whether or not it turned anything up. This skill has no Finding Template, so the
    table opens the report and the findings follow it, with all 7 rows present:
    
    | # | Pattern | Verdict | Evidence |
    |---|---------|---------|----------|
    | 1 | Arithmetic Overflow | `found` | `src/lib.rs:212` -- `+` on `BalanceOf<T>` in `do_transfer` |
    | 2 | Don't Panic | | |
    | 3 | Weights and Fees | | |
    | 4 | Verify First, Write Last | | |
    | 5 | Unsigned Transaction Validation | | |
    | 6 | Bad Randomness | | |
    | 7 | Bad Origin | | |
    
    Each verdict is one of:
    
    - **`found`** — cite `file:line` and write the finding up in full after the table.
    - **`clear`** — the pattern applies to this pallet and the pallet handles it. Name the macro, origin check, or
      arithmetic method you searched for, so a reader can repeat the search.
    - **`n/a`** — the pattern cannot apply here. Give the reason in one clause ("this pallet accepts no unsigned
      transactions"). Not having looked is not `n/a`. Note that pattern 4 is version-scoped (pre-v0.9.25): say
      which runtime version the pallet targets rather than dropping the row.
    
    A table with fewer than 7 rows is an incomplete scan and must be reported as one. A row whose Verdict cell is empty is incomplete in the same way: row 1 above is filled in to show the shape, and every row is filled in the same way before the report is done. Seven `clear` verdicts is a
    result a reader can act on. A report that covers three patterns and says nothing about the other four reads
    exactly like a clean pallet, and that is the failure this table exists to prevent.
    
    ---
    
    ## 7. Priority Guidelines
    
    ### Critical (Immediate Fix Required)
    - Arithmetic overflow (token creation, balance manipulation)
    - Panic DoS (node crash risk)
    - Bad origin (unauthorized privileged operations)
    
    ### High (Fix Before Launch)
    - Incorrect weights (DoS via spam)
    - Verify-first violations (state corruption, pre-v0.9.25)
    - Unsigned validation issues (spam, replay attacks)
    
    ### Medium (Address in Audit)
    - Bad randomness (manipulation possible but limited impact)
    
    ---
    
    ## 8. Testing Recommendations
    
    ### Fuzz Testing
    ```rust
    // Use test-fuzz for property-based testing
    #[cfg(test)]
    mod tests {
        use test_fuzz::test_fuzz;
    
        #[test_fuzz]
        fn fuzz_transfer(from: AccountId, to: AccountId, amount: u128) {
            // Should never panic
            let _ = Pallet::transfer(from, to, amount);
        }
    
        #[test_fuzz]
        fn fuzz_no_panics(call: Call) {
            // No dispatchable should panic
            let _ = call.dispatch(origin);
        }
    }
    ```
    
    ### Benchmarking
    ```bash
    # Run benchmarks to generate weights
    cargo build --release --features runtime-benchmarks
    ./target/release/node benchmark pallet \
        --chain dev \
        --pallet pallet_example \
        --extrinsic "*" \
        --steps 50 \
        --repeat 20
    ```
    
    ### try-runtime
    ```bash
    # Test runtime upgrades
    cargo build --release --features try-runtime
    try-runtime --runtime ./target/release/wbuild/runtime.wasm \
        on-runtime-upgrade live --uri wss://rpc.polkadot.io
    ```
    
    ---
    
    ## 9. Additional Resources
    
    - **Building Secure Contracts**: `building-secure-contracts/not-so-smart-contracts/substrate/`
    - **Substrate Documentation**: https://docs.substrate.io/
    - **FRAME Documentation**: https://paritytech.github.io/substrate/master/frame_support/
    - **test-fuzz**: https://github.com/trailofbits/test-fuzz
    - **Substrate StackExchange**: https://substrate.stackexchange.com/
    
    ---
    
    ## 10. Quick Reference Checklist
    
    Before completing Substrate pallet audit:
    
    **Arithmetic Safety (CRITICAL)**:
    - [ ] No direct `+`, `-`, `*`, `/` operators in dispatchables
    - [ ] All arithmetic uses `checked_*` or `saturating_*`
    - [ ] Type conversions use `try_into()` with error handling
    
    **Panic Prevention (CRITICAL)**:
    - [ ] No `unwrap()` or `expect()` in dispatchables
    - [ ] No direct array/slice indexing without bounds check
    - [ ] All user inputs validated with `ensure!`
    - [ ] Division operations check for zero divisor
    
    **Weights & DoS (CRITICAL)**:
    - [ ] Weights proportional to computational cost
    - [ ] Input parameters have maximum bounds
    - [ ] Benchmarking used to determine weights
    - [ ] No free (zero-weight) expensive operations
    
    **Access Control (CRITICAL)**:
    - [ ] Privileged operations use `ensure_root` or custom origins
    - [ ] `ensure_signed` only for user-level operations
    - [ ] Origin types properly configured in runtime
    - [ ] Sudo pallet removed before production
    
    **Storage Safety (HIGH)**:
    - [ ] Using Substrate v0.9.25+ OR manual `#[transactional]`
    - [ ] Validation before storage writes
    - [ ] Events emitted after successful operations
    
    **Other (MEDIUM)**:
    - [ ] Unsigned transactions use signed alternative if possible
    - [ ] If unsigned: proper validation, replay protection, authentication
    - [ ] BABE randomness used (not RandomnessCollectiveFlip)
    - [ ] Randomness uses `random(subject)` not `random_seed()`
    
    **Testing**:
    - [ ] Unit tests for all dispatchables
    - [ ] Fuzz tests to find panics
    - [ ] Benchmarks generated and verified
    - [ ] try-runtime tests for migrations
    - [ ] Coverage table emitted with all 7 rows, each carrying a verdict of `found`, `clear` or `n/a` with a reason
    
    ---
    
    ## 11. Rationalizations to Reject
    
    - **"The pallet is small, so most patterns obviously don't apply."** Obvious to whom? An `n/a` costs one
      clause and makes the judgment reviewable. Silence records nothing, and a reader cannot tell it apart from
      not having checked.
    - **"It compiles without warnings, so the arithmetic is fine."** Release builds wrap silently. `+` on a
      `Balance` is pattern 1 whether or not the compiler said anything, and debug assertions do not run on a
      production node.
    - **"I checked the patterns that matter for this pallet."** Deciding which patterns matter *is* the scan,
      not a precondition for starting it. Rank by severity after the table is complete, not by leaving rows out.
    - **"No findings, so there is nothing to report."** A zero-finding scan still emits the full coverage table.
      That table is the deliverable: it is what distinguishes a pallet that was examined from one that was
      glanced at.
    - **"`ensure_signed` is an origin check."** It establishes that *someone* signed, not that the right someone
      did. Pattern 7 is about which origin is required, and `ensure_signed` where `ensure_root` belongs passes
      this rationalization while failing the check.
    - **"The weight is benchmarked, so it's correct."** Benchmarks measure the path they exercise. A dispatchable
      whose worst case depends on storage size or an unbounded loop is pattern 3 even with a benchmark attached.
    

Comments (0)

Sign in to join the conversation.

No comments yet.

Reviews (0)

No reviews yet.

Related