Skip to main content

libddwaf/
config.rs

1use crate::object::WafMap;
2use crate::waf_map;
3
4/// The configuration for a new [`Builder`](crate::Builder).
5#[derive(Clone, Default, Debug)]
6pub struct Config {
7    obfuscator: Obfuscator,
8}
9impl Config {
10    /// Creates a new [`Config`] with the provided [`Obfuscator`].
11    #[must_use]
12    pub fn new(obfuscator: Obfuscator) -> Self {
13        Self { obfuscator }
14    }
15
16    /// Returns this configuration as a [`WafMap`].
17    ///
18    /// # Panics
19    /// Panics if memory allocation fails (out of memory).
20    #[must_use]
21    pub fn as_waf_object(&self) -> WafMap {
22        let mut map = WafMap::new(2).expect("configuration map length is representable");
23        let mut used = 0;
24        if let Some(key_regex) = self.obfuscator.key_regex() {
25            map[used] = ("key_regex", key_regex).into();
26            used += 1;
27        }
28        if let Some(value_regex) = self.obfuscator.value_regex() {
29            map[used] = ("value_regex", value_regex).into();
30            used += 1;
31        }
32        map.truncate(used);
33
34        waf_map!(("obfuscator", map))
35    }
36}
37
38/// Obfuscation configuration for the WAF.
39///
40/// This is effectively a pair of regular expressions that are respectively used
41/// to determine which key and value data to obfuscate when producing WAF
42/// outputs.
43#[derive(Clone, Debug)]
44pub struct Obfuscator {
45    key_regex: Option<Vec<u8>>,
46    value_regex: Option<Vec<u8>>,
47}
48impl Obfuscator {
49    /// Creates a new [`Obfuscator`] with the provided key and value regular
50    /// expressions.
51    ///
52    /// # Panics
53    /// Panics if the provided key or value cannot be turned into a [`CString`].
54    pub fn new<T: Into<Vec<u8>>, U: Into<Vec<u8>>>(
55        key_regex: Option<T>,
56        value_regex: Option<U>,
57    ) -> Self {
58        Self {
59            key_regex: key_regex.map(Into::into),
60            value_regex: value_regex.map(Into::into),
61        }
62    }
63
64    /// Returns the regular expression used to determine key data to be obfuscated, if one has been
65    /// set.
66    #[must_use]
67    pub fn key_regex(&self) -> Option<&[u8]> {
68        self.key_regex.as_deref()
69    }
70
71    /// Returns the regular expression used to determine value data to be obfuscated, if one has
72    /// been set.
73    #[must_use]
74    pub fn value_regex(&self) -> Option<&[u8]> {
75        self.value_regex.as_deref()
76    }
77}
78
79impl Default for Obfuscator {
80    fn default() -> Self {
81        // This actually uses the default regexes from libddwaf
82        Obfuscator::new(None::<&str>, None::<&str>)
83    }
84}