fallow_security/
predicates.rs1use fallow_types::extract::{SinkCallOptions, SinkLiteralValue, SinkObjectProperty};
4
5use crate::Matcher;
6
7impl Matcher {
8 #[must_use]
10 pub fn literal_value_satisfied(&self, literal: Option<&SinkLiteralValue>) -> bool {
11 if self.literal_values.is_empty()
12 && self.literal_contains.is_empty()
13 && self.literal_integers.is_empty()
14 {
15 return true;
16 }
17 let string_satisfied = (self.literal_values.is_empty() && self.literal_contains.is_empty())
18 || match literal {
19 Some(SinkLiteralValue::String(value)) => {
20 let lower = value.to_ascii_lowercase();
21 (self.literal_values.is_empty()
22 || self
23 .literal_values
24 .iter()
25 .any(|expected| lower == expected.to_ascii_lowercase()))
26 && (self.literal_contains.is_empty()
27 || self
28 .literal_contains
29 .iter()
30 .any(|needle| lower.contains(&needle.to_ascii_lowercase())))
31 }
32 _ => false,
33 };
34 let integer_satisfied = self.literal_integers.is_empty()
35 || match literal {
36 Some(SinkLiteralValue::Integer(value)) => self.literal_integers.contains(value),
37 _ => false,
38 };
39 string_satisfied && integer_satisfied
40 }
41
42 #[must_use]
45 pub fn object_properties_satisfied(&self, properties: &[SinkObjectProperty]) -> bool {
46 if self.object_properties.is_empty() && self.object_missing_or_false.is_empty() {
47 return true;
48 }
49 for predicate in &self.object_properties {
50 let Some(property) = properties.iter().find(|p| p.key == predicate.key) else {
51 return false;
52 };
53 if !predicate.value.matches(&property.value) {
54 return false;
55 }
56 }
57 if self.object_missing_or_false.is_empty() {
58 return true;
59 }
60 self.object_missing_or_false.iter().any(|key| {
61 properties
62 .iter()
63 .find(|p| p.key == *key)
64 .is_none_or(|property| matches!(property.value, SinkLiteralValue::Boolean(false)))
65 })
66 }
67
68 #[must_use]
71 pub fn object_missing_satisfied(&self, keys: &[String], keys_complete: bool) -> bool {
72 if self.object_missing.is_empty() {
73 return true;
74 }
75 keys_complete && self.object_missing.iter().any(|key| !keys.contains(key))
76 }
77
78 #[must_use]
83 pub fn options_may_enable_satisfied(&self, options: Option<&SinkCallOptions>) -> bool {
84 if self.options_may_enable.is_empty() {
85 return true;
86 }
87 let Some(options) = options else {
88 return true;
89 };
90 if !options.keys_complete {
91 return true;
92 }
93 self.options_may_enable.iter().any(|key| {
94 options.keys.contains(key)
95 && !options.properties.iter().any(|property| {
96 property.key == *key
97 && matches!(property.value, SinkLiteralValue::Boolean(false))
98 })
99 })
100 }
101
102 #[must_use]
104 pub fn context_satisfied(&self, context_names: &[String]) -> bool {
105 if self.context_keywords.is_empty() {
106 return true;
107 }
108 context_names.iter().any(|name| {
109 let lower = name.to_ascii_lowercase();
110 self.context_keywords
111 .iter()
112 .any(|keyword| lower.contains(&keyword.to_ascii_lowercase()))
113 })
114 }
115}
116
117#[cfg(test)]
118#[expect(
119 clippy::expect_used,
120 reason = "the test asserts a catalogue invariant directly"
121)]
122mod tests {
123 use super::*;
124 use crate::catalogue;
125
126 #[test]
127 fn options_may_enable_needs_proof_that_the_option_is_off() {
128 let shell_spawn = catalogue()
129 .matchers()
130 .iter()
131 .find(|m| m.options_may_enable == ["shell"])
132 .expect("spawn shell row present");
133 let options =
134 |keys: &[&str], properties: Vec<SinkObjectProperty>, complete: bool| SinkCallOptions {
135 properties,
136 keys: keys.iter().map(ToString::to_string).collect(),
137 keys_complete: complete,
138 };
139 let shell = |value: SinkLiteralValue| SinkObjectProperty {
140 key: "shell".to_string(),
141 value,
142 };
143
144 assert!(
145 !shell_spawn.is_literal_aware(),
146 "options_may_enable keeps the non-literal command gate"
147 );
148 assert!(shell_spawn.options_may_enable_satisfied(None));
149 assert!(shell_spawn.options_may_enable_satisfied(Some(&options(&[], vec![], false))));
150 assert!(shell_spawn.options_may_enable_satisfied(Some(&options(
151 &["shell"],
152 vec![shell(SinkLiteralValue::Boolean(true))],
153 true,
154 ))));
155 assert!(shell_spawn.options_may_enable_satisfied(Some(&options(
156 &["shell"],
157 vec![shell(SinkLiteralValue::String("/bin/bash".to_string()))],
158 true,
159 ))));
160 assert!(
161 shell_spawn.options_may_enable_satisfied(Some(&options(&["shell"], vec![], true))),
162 "a non-literal shell value can enable the shell"
163 );
164 assert!(!shell_spawn.options_may_enable_satisfied(Some(&options(&[], vec![], true))));
165 assert!(!shell_spawn.options_may_enable_satisfied(Some(&options(
166 &["stdio"],
167 vec![],
168 true,
169 ))));
170 assert!(!shell_spawn.options_may_enable_satisfied(Some(&options(
171 &["shell"],
172 vec![shell(SinkLiteralValue::Boolean(false))],
173 true,
174 ))));
175 }
176}