Skip to main content

sentri_analyzer_soroban/
lib.rs

1#![deny(unsafe_code)]
2#![allow(missing_docs)]
3
4//! Soroban (Stellar) contract analyzer: extracts a program model and runs
5//! vulnerability detectors over `#[contract]`/`#[contractimpl]` Rust source.
6
7pub mod analyzer;
8/// Vulnerability detectors for Soroban contracts
9pub mod detectors;
10/// Chain-agnostic semantic-model extraction (shared cross-chain IR)
11pub mod semantic_model;
12/// Data model for parsed `#[contractimpl]` function facts
13pub mod soroban_model;
14/// `syn`-based parser extracting function facts from contract source
15pub mod soroban_parser;
16
17pub use analyzer::SorobanAnalyzer;
18pub use detectors::*;
19pub use semantic_model::build_semantic_model;
20pub use soroban_model::ContractFunction;
21pub use soroban_parser::parse_contract_functions;
22
23/// Run every live Soroban detector against the given source text.
24///
25/// This is the single entry point the CLI should use for Soroban analysis:
26/// contract functions are parsed once via `syn` and every detector reads
27/// off that shared fact set.
28pub fn run_all_detectors(source: &str, file_path: &str) -> Vec<sentri_core::Finding> {
29    let functions = match parse_contract_functions(source) {
30        Ok(functions) => functions,
31        // A source file `syn` can't parse contributes no findings rather
32        // than failing the whole scan, matching the other chain analyzers'
33        // best-effort behavior.
34        Err(_) => return Vec::new(),
35    };
36
37    let mut findings = detectors::detect_all(&functions, file_path);
38
39    // Chain-agnostic shared-IR rule: flags privileged mutations with no
40    // authorization guard, using the same rule EVM/Solana/Move share.
41    if let Ok(model) = build_semantic_model(source, file_path) {
42        findings.extend(sentri_ir::rules::find_unauthorized_privileged_mutations(
43            &model,
44        ));
45    }
46
47    let mut seen = std::collections::HashSet::new();
48    findings.retain(|f| seen.insert(f.dedup_key()));
49
50    findings.sort_by(|a, b| match b.severity.cmp(&a.severity) {
51        std::cmp::Ordering::Equal => a.line.cmp(&b.line),
52        other => other,
53    });
54
55    findings
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    const VULNERABLE: &str = r#"
63#[contract]
64pub struct VaultContract;
65
66#[contractimpl]
67impl VaultContract {
68    pub fn initialize(env: Env, admin: Address) {
69        env.storage().instance().set(&DataKey::Admin, &admin);
70    }
71
72    pub fn withdraw(env: Env, to: Address, amount: i128) {
73        let balance: i128 = env.storage().persistent().get(&DataKey::Balance(to.clone())).unwrap();
74        let new_balance = balance - amount;
75        env.storage().persistent().set(&DataKey::Balance(to), &new_balance);
76    }
77}
78"#;
79
80    #[test]
81    fn run_all_detectors_includes_shared_and_chain_specific_findings() {
82        let findings = run_all_detectors(VULNERABLE, "vault.rs");
83        let ids: std::collections::HashSet<_> =
84            findings.iter().map(|f| f.invariant_id.as_str()).collect();
85
86        assert!(ids.contains("sor_missing_require_auth"));
87        assert!(ids.contains("sor_reinitialization"));
88        assert!(ids.contains(sentri_ir::rules::UNAUTHORIZED_PRIVILEGED_MUTATION));
89    }
90
91    #[test]
92    fn run_all_detectors_never_panics_on_unparseable_source() {
93        let findings = run_all_detectors("this is not valid rust {{{", "bad.rs");
94        assert!(findings.is_empty());
95    }
96}