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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Event logsource extraction for opt-in, conflict-based logsource pruning.
//!
//! A [`LogSourceExtractor`] derives a [`LogSource`] from an event by reading
//! configurable field names (defaulting to the literals `product`, `service`,
//! and `category`), falling back to optional static defaults. The result feeds
//! the engine's conflict-based pruning: an event tagged `product: windows`
//! skips `product: linux` rules without dropping Windows-category or
//! logsource-less rules.
//!
//! Extraction is fail-open per dimension: a field that is absent, null, or
//! blank leaves that dimension unset (after the static default is consulted),
//! so a missing tag never prunes anything.
//!
//! Embedders whose events carry their logsource somewhere other than a
//! matchable field (out-of-band metadata, a transport envelope) implement
//! [`ExtractLogSource`] for their event type instead, and build the engine
//! around it with [`Engine::with_logsource_extractor`].
//!
//! [`Engine::with_logsource_extractor`]: crate::Engine::with_logsource_extractor
use LogSource;
use crateEvent;
/// Derives an event's [`LogSource`] for conflict-based logsource pruning.
///
/// [`Engine`](crate::Engine), [`CorrelationEngine`](crate::CorrelationEngine),
/// and [`SchemaRouter`](crate::SchemaRouter) are generic over their extractor
/// type, defaulting to the field-reading [`LogSourceExtractor`], and call it
/// once per event with static dispatch. The trait is generic over the event
/// type, so an extractor can read state that only a concrete event type
/// carries.
///
/// A dimension left unset is a wildcard for pruning. An extractor that cannot
/// determine a dimension must leave it `None` rather than guess, so a missing
/// value never prunes a rule.
///
/// # Example
///
/// An event type that carries its logsource as metadata beside the payload:
///
/// ```rust
/// use std::borrow::Cow;
///
/// use rsigma_eval::event::{Event, EventValue, JsonEvent};
/// use rsigma_eval::{Engine, ExtractLogSource};
/// use rsigma_parser::{LogSource, parse_sigma_yaml};
/// use serde_json::{Value, json};
///
/// struct TaggedEvent {
/// product: &'static str,
/// payload: JsonEvent<'static>,
/// }
///
/// impl Event for TaggedEvent {
/// fn get_field(&self, path: &str) -> Option<EventValue<'_>> {
/// self.payload.get_field(path)
/// }
/// fn any_string_value(&self, pred: &dyn Fn(&str) -> bool) -> bool {
/// self.payload.any_string_value(pred)
/// }
/// fn all_string_values(&self) -> Vec<Cow<'_, str>> {
/// self.payload.all_string_values()
/// }
/// fn to_json(&self) -> Value {
/// self.payload.to_json()
/// }
/// }
///
/// struct MetadataExtractor;
///
/// impl ExtractLogSource<TaggedEvent> for MetadataExtractor {
/// fn extract(&self, event: &TaggedEvent) -> LogSource {
/// LogSource {
/// product: Some(event.product.to_string()),
/// ..LogSource::default()
/// }
/// }
/// }
///
/// let rules = parse_sigma_yaml(r#"
/// title: Linux whoami
/// logsource:
/// product: linux
/// detection:
/// selection:
/// CommandLine|contains: whoami
/// condition: selection
/// "#).unwrap();
///
/// let mut engine = Engine::with_logsource_extractor(MetadataExtractor);
/// engine.add_collection(&rules).unwrap();
///
/// let payload = || JsonEvent::owned(json!({"CommandLine": "whoami"}));
/// let linux = TaggedEvent { product: "linux", payload: payload() };
/// let windows = TaggedEvent { product: "windows", payload: payload() };
///
/// assert_eq!(engine.evaluate(&linux).len(), 1);
/// assert!(engine.evaluate(&windows).is_empty()); // pruned by metadata
/// ```
/// Derives an event [`LogSource`] from configurable fields plus static
/// defaults, for conflict-based logsource pruning on the evaluation hot path.
///
/// Each dimension is resolved independently in precedence order: the value of
/// the configured event field, then the static default, then unset (`None`).
/// A present-but-blank field value is treated as unset.
///
/// # Example
///
/// ```rust
/// use rsigma_eval::LogSourceExtractor;
/// use rsigma_eval::event::JsonEvent;
/// use serde_json::json;
///
/// let extractor = LogSourceExtractor::new();
/// let ev = json!({"product": "windows"});
/// let event = JsonEvent::borrow(&ev);
///
/// let ls = extractor.extract(&event);
/// assert_eq!(ls.product.as_deref(), Some("windows"));
/// assert_eq!(ls.category, None); // absent fields stay unset (fail-open)
/// ```