Skip to content

Commit e64a691

Browse files
cursoragentscript3r
andcommitted
feat: Add TOML-based AST pattern loading
This commit introduces the ability to load AST patterns from TOML files, enabling more flexible and dynamic configuration of detectors. It also includes necessary dependency updates and refactors related to pattern management. Co-authored-by: script3r <[email protected]>
1 parent 46592a1 commit e64a691

File tree

10 files changed

+225
-220
lines changed

10 files changed

+225
-220
lines changed

Cargo.lock

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ repository = "https://example.com/cipherscope/repo"
1717
anyhow = "1"
1818
serde = { version = "1", features = ["derive"] }
1919
serde_json = "1"
20+
toml = "0.8"
2021
rayon = "1"
2122
ignore = "0.4"
2223
clap = { version = "4", features = ["derive"] }

GROUND_TRUTH_REGENERATION_SUMMARY.md

Lines changed: 0 additions & 84 deletions
This file was deleted.

crates/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "Apache-2.0"
88
anyhow = { workspace = true }
99
clap = { workspace = true }
1010
serde_json = { workspace = true }
11+
toml = { workspace = true }
1112
rayon = { workspace = true }
1213
indicatif = "0.17"
1314
scanner-core = { path = "../scanner-core" }

crates/cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use indicatif::{ProgressBar, ProgressStyle};
44
use scanner_core::{Config, Detector, Language, AstBasedDetector, AstDetector, Scanner, CryptoFindings};
55
use std::fs;
66
use std::path::PathBuf;
7+
use std::sync::Arc;
78

89
#[derive(Parser, Debug)]
910
#[command(name = "cipherscope")]

crates/cli/tests/progress_reporting.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,27 @@ fn test_progress_monotonic_increase() {
138138

139139
for (processed, discovered, findings) in &all_updates {
140140
assert!(
141-
processed >= last_processed,
141+
*processed >= last_processed,
142142
"Processed count should never decrease: {} -> {}",
143143
last_processed,
144144
processed
145145
);
146146
assert!(
147-
discovered >= last_discovered,
147+
*discovered >= last_discovered,
148148
"Discovered count should never decrease: {} -> {}",
149149
last_discovered,
150150
discovered
151151
);
152152
assert!(
153-
findings >= last_findings,
153+
*findings >= last_findings,
154154
"Findings count should never decrease: {} -> {}",
155155
last_findings,
156156
findings
157157
);
158158

159-
last_processed = processed;
160-
last_discovered = discovered;
161-
last_findings = findings;
159+
last_processed = *processed;
160+
last_discovered = *discovered;
161+
last_findings = *findings;
162162
}
163163

164164
println!("✅ Monotonic increase test passed with {} updates", all_updates.len());

crates/scanner-core/src/ast.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,65 @@ impl AstDetector {
7878

7979
/// Load AST patterns from TOML content
8080
pub fn load_patterns_from_toml(toml_content: &str) -> Result<Vec<AstPattern>> {
81-
// For now, return default patterns - this can be expanded to parse the TOML
82-
// and convert the library definitions into AST patterns
83-
Ok(Self::default_patterns())
81+
use crate::PatternsFile;
82+
83+
let patterns_file: PatternsFile = toml::from_str(toml_content)?;
84+
let mut ast_patterns = Vec::new();
85+
86+
// Convert library specs to AST patterns
87+
for library in patterns_file.library {
88+
ast_patterns.extend(Self::convert_library_to_ast_patterns(&library)?);
89+
}
90+
91+
// Also include default patterns for comprehensive coverage
92+
ast_patterns.extend(Self::default_patterns());
93+
94+
Ok(ast_patterns)
95+
}
96+
97+
/// Convert a library specification to AST patterns
98+
fn convert_library_to_ast_patterns(library: &crate::LibrarySpec) -> Result<Vec<AstPattern>> {
99+
let mut patterns = Vec::new();
100+
101+
for &language in &library.languages {
102+
// Convert include patterns to AST patterns
103+
for pattern in &library.patterns.include_patterns {
104+
patterns.push(AstPattern {
105+
query: pattern.clone(),
106+
language,
107+
match_type: AstMatchType::Library { name: library.name.clone() },
108+
metadata: HashMap::new(),
109+
});
110+
}
111+
112+
// Convert import patterns to AST patterns
113+
for pattern in &library.patterns.import_patterns {
114+
patterns.push(AstPattern {
115+
query: pattern.clone(),
116+
language,
117+
match_type: AstMatchType::Library { name: library.name.clone() },
118+
metadata: HashMap::new(),
119+
});
120+
}
121+
122+
// Convert algorithm patterns
123+
for algorithm in &library.algorithms {
124+
for pattern in &algorithm.symbol_patterns {
125+
patterns.push(AstPattern {
126+
query: pattern.clone(),
127+
language,
128+
match_type: AstMatchType::Algorithm {
129+
name: algorithm.name.clone(),
130+
primitive: algorithm.primitive.clone(),
131+
nist_quantum_security_level: algorithm.nist_quantum_security_level,
132+
},
133+
metadata: HashMap::new(),
134+
});
135+
}
136+
}
137+
}
138+
139+
Ok(patterns)
84140
}
85141

86142
/// Default AST patterns for common cryptographic libraries and algorithms

0 commit comments

Comments
 (0)