Skip to main content

truent_analyzer_solana/
lib.rs

1#![deny(unsafe_code)]
2#![allow(missing_docs)]
3
4//! Solana program analyzer: Extracts program model from Rust source.
5
6pub mod analyzer;
7/// Data models for parsed Anchor account structures and security analysis
8pub mod anchor_model;
9/// Parser for extracting account information from Anchor source code
10pub mod anchor_parser;
11/// Vulnerability detectors for Solana programs
12pub mod detectors;
13/// Chain-agnostic semantic-model extraction (Epic 6.1 shared IR)
14pub mod semantic_model;
15pub mod solana_durable_nonce;
16pub mod solana_fake_sysvar_instruction;
17pub mod solana_pda_authority_validation;
18pub mod solana_rent_exemption;
19pub mod solana_unchecked_token_account;
20
21pub use analyzer::SolanaAnalyzer;
22pub use anchor_model::{
23    AccountSecurity, AnchorAccountField, AnchorAccountStruct, AnchorConstraint,
24};
25pub use anchor_parser::parse_anchor_accounts;
26pub use detectors::*;
27pub use semantic_model::build_semantic_model;
28pub use solana_durable_nonce::detect_solana_durable_nonce_validation;
29pub use solana_fake_sysvar_instruction::detect_fake_sysvar_instruction_account;
30pub use solana_pda_authority_validation::detect_solana_pda_authority_validation;
31pub use solana_rent_exemption::detect_solana_rent_exemption;
32pub use solana_unchecked_token_account::detect_unchecked_token_account_type;
33
34/// Run every live Solana detector against the given source text.
35///
36/// This is the single entry point the CLI should use for Solana analysis: each
37/// detector operates directly on raw source text, no syn/AST parse is required.
38pub fn run_all_detectors(source: &str, file_path: &str) -> Vec<truent_core::Finding> {
39    let mut findings = detectors::detect_all(source, file_path);
40
41    findings.extend(detect_solana_durable_nonce_validation(source, file_path));
42    findings.extend(detect_solana_pda_authority_validation(source, file_path));
43    findings.extend(detect_solana_rent_exemption(source, file_path));
44    findings.extend(detect_unchecked_token_account_type(source, file_path));
45    findings.extend(detect_fake_sysvar_instruction_account(source, file_path));
46
47    // Chain-agnostic shared-IR rule (Epic 6.1): flags privileged mutations
48    // with no authorization guard, using the same rule EVM and Move share.
49    // Best-effort: a source file the Anchor-account parser can't handle
50    // simply contributes nothing from this rule rather than failing the scan.
51    if let Ok(model) = build_semantic_model(source, file_path) {
52        findings.extend(truent_ir::rules::find_unauthorized_privileged_mutations(
53            &model,
54        ));
55    }
56
57    let mut seen = std::collections::HashSet::new();
58    findings.retain(|f| seen.insert(f.dedup_key()));
59
60    findings.sort_by(|a, b| match b.severity.cmp(&a.severity) {
61        std::cmp::Ordering::Equal => a.line.cmp(&b.line),
62        other => other,
63    });
64
65    findings
66}