yara-x 1.15.0

A pure Rust implementation of YARA.
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use regex::{Error, Regex};

use yara_x_parser::ast::{self, Meta, WithSpan};

use crate::compiler::Warning;
use crate::compiler::report::ReportBuilder;
use crate::compiler::{errors, warnings};
use crate::errors::CompileError;

/// Trait implemented by all linters.
///
/// All types in [`crate::linters`] implement this trait and can be passed
/// to [`crate::Compiler::add_linter`].
#[allow(private_bounds)]
pub trait Linter: LinterInternal {}

// Types that implement [`LinterInternal`] automatically implement [`Linter`].
impl<T: LinterInternal> Linter for T {}

/// This is the actual trait implemented by all linters. [`Linter`] is a
/// supertrait of [`LinterInternal`], while the former is visible to the public
/// API, the latter is for internal use. This prevents users of this crate
/// from implementing their own linters and keep the signature of the trait
/// private. This is because [`ReportBuilder`] is an internal type that we
/// don't want to expose publicly, and because users can't define their own
/// warnings.
pub(crate) trait LinterInternal {
    fn check(
        &self,
        report_builder: &ReportBuilder,
        rule: &ast::Rule,
    ) -> LinterResult;
}

/// Represents the result of a linter.
pub(crate) enum LinterResult {
    Ok,
    Warn(Warning),
    Warns(Vec<Warning>),
    Err(CompileError),
}

/// A linter that ensures that rule names match a given regular expression.
///
/// ```
/// # use yara_x::Compiler;
/// use yara_x::linters::rule_name;
/// let mut compiler = Compiler::new();
/// let warnings = compiler
///     .add_linter(rule_name("APT_.*").unwrap())
///     // This produces a warning because the rule name doesn't match the regex.
///     .add_source(r#"rule foo { strings: $foo = "foo" condition: $foo }"#)
///     .unwrap()
///     .warnings();
///
/// assert_eq!(
///     warnings[0].to_string(),
///     r#"warning[invalid_rule_name]: rule name does not match regex `APT_.*`
///  --> line:1:6
///   |
/// 1 | rule foo { strings: $foo = "foo" condition: $foo }
///   |      --- this rule name does not match regex `APT_.*`"#);
/// ```
pub struct RuleName {
    regex: String,
    error: bool,
    compiled_regex: Regex,
}

impl RuleName {
    fn new<R: Into<String>>(regex: R) -> Result<Self, regex::Error> {
        let regex = regex.into();
        let compiled_regex = Regex::new(regex.as_str())?;
        Ok(Self { regex, compiled_regex, error: false })
    }

    /// Specifies whether the linter should produce an error instead of a warning.
    ///
    /// By default, the linter raises warnings about rule names that don't match
    /// the regular expression. This setting allows turning such warnings into
    /// errors.
    pub fn error(mut self, yes: bool) -> Self {
        self.error = yes;
        self
    }
}

impl LinterInternal for RuleName {
    fn check(
        &self,
        report_builder: &ReportBuilder,
        rule: &ast::Rule,
    ) -> LinterResult {
        if !self.compiled_regex.is_match(rule.identifier.name) {
            if self.error {
                LinterResult::Err(errors::InvalidRuleName::build(
                    report_builder,
                    report_builder.span_to_code_loc(rule.identifier.span()),
                    self.regex.clone(),
                ))
            } else {
                LinterResult::Warn(warnings::InvalidRuleName::build(
                    report_builder,
                    report_builder.span_to_code_loc(rule.identifier.span()),
                    self.regex.clone(),
                ))
            }
        } else {
            LinterResult::Ok
        }
    }
}

type Predicate<'a> = dyn Fn(&Meta) -> bool + 'a;

/// A linter that ensures tags meet specified requirements in either an allowed
/// list of tags or in a regex.
///
/// ```
/// # use yara_x::Compiler;
/// use yara_x::linters;
/// let mut compiler = Compiler::new();
/// let warnings = compiler
///     .add_linter(linters::tags_allowed(vec!["foo".to_string(), "bar".to_string()]))
///     // This produces a warning because the rule tags are not from the
///     // allowed list
///     .add_source(r#"rule foo : test { strings: $foo = "foo" condition: $foo }"#)
///     .unwrap()
///     .warnings();
///
/// assert_eq!(
///     warnings[0].to_string(),
///     r#"warning[unknown_tag]: tag not in allowed list
///  --> line:1:12
///   |
/// 1 | rule foo : test { strings: $foo = "foo" condition: $foo }
///   |            ---- tag `test` not in allowed list
///   |
///   = note: allowed tags: foo, bar"#);
pub struct Tags {
    allowed: Vec<String>,
    regex: Option<String>,
    compiled_regex: Option<Regex>,
    error: bool,
}

impl Tags {
    /// A list of strings that tags for each rule must match one of.
    pub(crate) fn from_list(list: Vec<String>) -> Self {
        Self { allowed: list, regex: None, compiled_regex: None, error: false }
    }

    /// Regular expression that tags for each rule must match.
    pub(crate) fn from_regex<R: Into<String>>(
        regex: R,
    ) -> Result<Self, regex::Error> {
        let regex = regex.into();
        let compiled_regex = Some(Regex::new(regex.as_str())?);
        let tags = Self {
            allowed: Vec::new(),
            regex: Some(regex),
            compiled_regex,
            error: false,
        };
        Ok(tags)
    }

    /// Specifies whether the linter should produce an error instead of a
    /// warning.
    ///
    /// By default, the linter raises warnings about tags that don't match the
    /// regular expression. This setting allows turning such warnings into
    /// errors.
    pub fn error(mut self, yes: bool) -> Self {
        self.error = yes;
        self
    }
}

impl LinterInternal for Tags {
    fn check(
        &self,
        report_builder: &ReportBuilder,
        rule: &ast::Rule,
    ) -> LinterResult {
        if rule.tags.is_none() {
            return LinterResult::Ok;
        }

        let mut results: Vec<Warning> = Vec::new();
        let tags = rule.tags.as_ref().unwrap();
        if !self.allowed.is_empty() {
            for tag in tags.iter() {
                if !self.allowed.contains(&tag.name.to_string()) {
                    if self.error {
                        return LinterResult::Err(errors::UnknownTag::build(
                            report_builder,
                            report_builder.span_to_code_loc(tag.span()),
                            tag.name.to_string(),
                            Some(format!(
                                "allowed tags: {}",
                                self.allowed.join(", ")
                            )),
                        ));
                    } else {
                        results.push(warnings::UnknownTag::build(
                            report_builder,
                            report_builder.span_to_code_loc(tag.span()),
                            tag.name.to_string(),
                            Some(format!(
                                "allowed tags: {}",
                                self.allowed.join(", ")
                            )),
                        ));
                    }
                }
            }
        } else {
            let compiled_regex = self.compiled_regex.as_ref().unwrap();

            for tag in tags.iter() {
                if !compiled_regex.is_match(tag.name) {
                    if self.error {
                        return LinterResult::Err(errors::InvalidTag::build(
                            report_builder,
                            report_builder.span_to_code_loc(tag.span()),
                            tag.name.to_string(),
                            self.regex.as_ref().unwrap().clone(),
                        ));
                    } else {
                        results.push(warnings::InvalidTag::build(
                            report_builder,
                            report_builder.span_to_code_loc(tag.span()),
                            tag.name.to_string(),
                            self.regex.as_ref().unwrap().clone(),
                        ));
                    }
                }
            }
        }

        if results.is_empty() {
            LinterResult::Ok
        } else {
            LinterResult::Warns(results)
        }
    }
}

/// A linter that validates metadata entries.
///
/// ```
/// # use yara_x::Compiler;
/// use yara_x::linters::metadata;
/// let mut compiler = Compiler::new();
/// let warnings = compiler
///     .add_linter(metadata("author").required(true))
///     // This produces a warning because the rule name doesn't have the
///     // required metadata.
///     .add_source(r#"rule foo { strings: $foo = "foo" condition: $foo }"#)
///     .unwrap()
///     .warnings();
///
/// assert_eq!(
///     warnings[0].to_string(),
///     r#"warning[missing_metadata]: required metadata is missing
///  --> line:1:6
///   |
/// 1 | rule foo { strings: $foo = "foo" condition: $foo }
///   |      --- required metadata `author` not found"#);
/// ```
pub struct Metadata<'a> {
    identifier: String,
    predicate: Option<Box<Predicate<'a>>>,
    required: bool,
    error: bool,
    message: Option<String>,
    note: Option<String>,
}

impl<'a> Metadata<'a> {
    fn new<I: Into<String>>(identifier: I) -> Self {
        Self {
            identifier: identifier.into(),
            predicate: None,
            required: false,
            error: false,
            message: None,
            note: None,
        }
    }

    /// Specifies whether the metadata is required in all rules.
    pub fn required(mut self, yes: bool) -> Self {
        self.required = yes;
        self
    }

    /// Specifies whether the linter should produce an error instead of a warning.
    ///
    /// By default, the linter raises warnings about required metadata that is
    /// missing, or metadata that doesn't pass the validation. This setting allows
    /// turning such warnings into errors.
    pub fn error(mut self, yes: bool) -> Self {
        self.error = yes;
        self
    }

    /// Sets a predicate that determines whether the metadata is valid or not.
    ///
    /// The predicate must return `true` if the metadata is considered valid.
    /// If it returns `false`, the metadata is deemed invalid and a warning
    /// will be raised with the specified message.
    ///
    /// ```
    /// # use yara_x::Compiler;
    /// use yara_x_parser::ast::MetaValue;
    /// use yara_x::linters::metadata;
    /// let mut compiler = Compiler::new();
    /// let warnings = compiler
    ///     .add_linter(
    ///         // The validator for the `author` metadata returns true only
    ///         // when its value is a string.
    ///         metadata("author").validator(|meta| {
    ///            matches!(meta.value, MetaValue::String(_))
    ///         },
    ///         "author must be a string"))
    ///     // This produces a warning because the `author` metadata
    ///     // is a boolean, and it must be a string.
    ///     .add_source(r#"rule foo {
    ///         meta:
    ///            author = false
    ///         strings:
    ///            $foo = "foo"
    ///         condition:
    ///            $foo
    ///         }"#)
    ///     .unwrap()
    ///     .warnings();
    ///
    /// assert_eq!(
    ///     warnings[0].to_string(),
    ///     r#"warning[invalid_metadata]: metadata `author` is not valid
    ///  --> line:3:21
    ///   |
    /// 3 |            author = false
    ///   |                     ----- author must be a string"#);
    /// ```
    pub fn validator<P, M>(mut self, predicate: P, message: M) -> Self
    where
        P: Fn(&Meta) -> bool + 'a,
        M: Into<String>,
    {
        self.predicate = Some(Box::new(predicate));
        self.message = Some(message.into());
        self
    }
}

impl LinterInternal for Metadata<'_> {
    fn check(
        &self,
        report_builder: &ReportBuilder,
        rule: &ast::Rule,
    ) -> LinterResult {
        let mut found = false;
        for meta in rule.meta.iter().flatten() {
            if meta.identifier.name == self.identifier.as_str() {
                if let Some(predicate) = &self.predicate
                    && !predicate(meta)
                {
                    return if self.error {
                        LinterResult::Err(errors::InvalidMetadata::build(
                            report_builder,
                            meta.identifier.name.to_string(),
                            report_builder.span_to_code_loc(meta.value.span()),
                            self.message
                                .clone()
                                .unwrap_or("invalid metadata".to_string()),
                        ))
                    } else {
                        LinterResult::Warn(warnings::InvalidMetadata::build(
                            report_builder,
                            meta.identifier.name.to_string(),
                            report_builder.span_to_code_loc(meta.value.span()),
                            self.message
                                .clone()
                                .unwrap_or("invalid metadata".to_string()),
                        ))
                    };
                }
                found = true;
            }
        }

        if self.required && !found {
            return if self.error {
                LinterResult::Err(errors::MissingMetadata::build(
                    report_builder,
                    report_builder.span_to_code_loc(rule.identifier.span()),
                    self.identifier.clone(),
                    self.note.clone(),
                ))
            } else {
                LinterResult::Warn(warnings::MissingMetadata::build(
                    report_builder,
                    report_builder.span_to_code_loc(rule.identifier.span()),
                    self.identifier.clone(),
                    self.note.clone(),
                ))
            };
        }

        LinterResult::Ok
    }
}

/// Creates a tag linter from a list of allowed tags.
pub fn tags_allowed(list: Vec<String>) -> Tags {
    Tags::from_list(list)
}

/// Creates a tag linter that makes sure that each tag matches the given regular
/// expression.
pub fn tag_regex<R: Into<String>>(regex: R) -> Result<Tags, Error> {
    Tags::from_regex(regex)
}

/// Creates a linter that validates metadata entries.
///
/// See [`Metadata`] for details.
pub fn metadata<'a, I: Into<String>>(identifier: I) -> Metadata<'a> {
    Metadata::new(identifier)
}

/// Creates a linter that makes sure that rule names match the given
/// regular expression.
///
/// See [`RuleName`] for details.
pub fn rule_name<R: Into<String>>(regex: R) -> Result<RuleName, Error> {
    RuleName::new(regex)
}