1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use crate::cli::{RulesAction, RulesArgs};
use crate::config::ZiftConfig;
use crate::error::Result;
use crate::rules;
use crate::scanner::parser as ts_parser;
use crate::types::Language;
pub fn execute(args: RulesArgs, config: ZiftConfig) -> Result<()> {
match args.action {
RulesAction::List => {
let loaded = rules::load_rules(None, &config)?;
if loaded.is_empty() {
println!("No pattern rules loaded.");
return Ok(());
}
println!(
"{:<35} {:<15} {:<10} Languages",
"ID", "Category", "Confidence"
);
println!("{}", "-".repeat(80));
for rule in &loaded {
let langs: Vec<String> = rule.languages.iter().map(|l| l.to_string()).collect();
println!(
"{:<35} {:<15} {:<10} {}",
rule.id,
rule.category.to_string(),
rule.confidence.to_string(),
langs.join(", "),
);
}
println!("\n{} rules loaded.", loaded.len());
}
RulesAction::Validate => {
let loaded = rules::load_rules(None, &config)?;
let mut errors = 0;
for rule in &loaded {
// Validate tree-sitter queries against all grammar variants
for lang in &rule.languages {
let variants: &[bool] = if *lang == Language::TypeScript {
&[false, true] // validate against both TS and TSX grammars
} else {
&[false]
};
for &is_tsx_jsx in variants {
let ts_lang = ts_parser::get_language(*lang, is_tsx_jsx)?;
if let Err(e) = tree_sitter::Query::new(&ts_lang, &rule.query_source) {
let suffix = if is_tsx_jsx { "/tsx" } else { "" };
eprintln!("FAIL {} ({lang}{suffix}): query: {e}", rule.id);
errors += 1;
}
}
}
// Validate each generated policy template against its
// engine's parser. The template-level validators wrap each
// body in a minimal module before parsing, which is engine
// shape — keep the dispatch local rather than pushing a
// template-flavored variant onto the PolicyGenerator trait.
for tmpl in &rule.policy_templates {
let (valid, error) = match tmpl.engine {
crate::types::PolicyEngine::Rego => {
let r = crate::rego::validator::validate_template(&tmpl.template);
(r.valid, r.error)
}
crate::types::PolicyEngine::Cedar => {
let r = crate::cedar::validator::validate_template(&tmpl.template);
(r.valid, r.error)
}
};
if !valid {
let err = error.unwrap_or_default();
eprintln!("FAIL {} {} template: {err}", rule.id, tmpl.engine);
errors += 1;
}
}
}
if errors == 0 {
println!("All {} rules validated successfully.", loaded.len());
} else {
eprintln!("{errors} rule(s) failed validation.");
std::process::exit(1);
}
}
RulesAction::Test => {
let loaded = rules::load_rules(None, &config)?;
let mut passed = 0;
let mut failed = 0;
for rule in &loaded {
for (i, test) in rule.tests.iter().enumerate() {
let Some(default_lang) = rule.languages.first().copied() else {
eprintln!("FAIL {}[{i}]: rule has no languages configured", rule.id);
failed += 1;
continue;
};
let lang = test.language.unwrap_or(default_lang);
let variants: Vec<bool> = if lang == Language::TypeScript {
vec![false, true] // test against both TS and TSX grammars
} else {
vec![false]
};
for is_tsx_jsx in variants {
let ts_lang = ts_parser::get_language(lang, is_tsx_jsx)?;
let mut parser = tree_sitter::Parser::new();
let tree = match ts_parser::parse_source(
&mut parser,
test.input.as_bytes(),
lang,
is_tsx_jsx,
) {
Ok(t) => t,
Err(e) => {
let suffix = if is_tsx_jsx { "/tsx" } else { "" };
eprintln!(
"FAIL {}[{i}] ({lang}{suffix}): parse error: {e}",
rule.id
);
failed += 1;
continue;
}
};
let compiled = match crate::scanner::matcher::compile_rule(rule, &ts_lang) {
Ok(c) => c,
Err(e) => {
let suffix = if is_tsx_jsx { "/tsx" } else { "" };
eprintln!(
"FAIL {}[{i}] ({lang}{suffix}): compile error: {e}",
rule.id
);
failed += 1;
continue;
}
};
let findings = match crate::scanner::matcher::execute_query(
&compiled,
&tree,
test.input.as_bytes(),
std::path::Path::new("test"),
lang,
) {
Ok(f) => f,
Err(e) => {
let suffix = if is_tsx_jsx { "/tsx" } else { "" };
eprintln!(
"FAIL {}[{i}] ({lang}{suffix}): query error: {e}",
rule.id
);
failed += 1;
continue;
}
};
let matched = !findings.is_empty();
if matched == test.expect_match {
passed += 1;
} else {
let suffix = if is_tsx_jsx { "/tsx" } else { "" };
eprintln!(
"FAIL {}[{i}] ({lang}{suffix}): expected match={}, got match={}",
rule.id, test.expect_match, matched,
);
failed += 1;
}
}
}
}
println!("{passed} passed, {failed} failed.");
if failed > 0 {
std::process::exit(1);
}
}
}
Ok(())
}