openmoat-core 0.29.0

Policy model and decision engine for OpenMoat. Pure: no I/O, no OS calls.
Documentation
//! Matching one shell word against one pattern word.

use std::sync::Arc;

use globset::GlobBuilder;

use super::{Built, LazyGlob};

/// Stands in for a literal backslash while matching one shell word. globset
/// rewrites `\` to `/` in every candidate on Windows (it assumes paths), so a
/// backslash would match nothing there; swapping it on both sides first makes
/// shell words match byte for byte on every platform. It is ASCII because
/// globset's character classes do not match characters outside it.
const BACKSLASH: &str = "\u{1}";

/// Characters that make a word more than a literal for globset (wildcards,
/// classes, alternation, escapes), plus `,` and `!`, which are only special
/// inside those but are cheap to send down the glob path.
const GLOB_CHARS: [char; 9] = ['*', '?', '[', ']', '{', '}', '\\', ',', '!'];

/// Whether globset parses `glob` whatever its options, so its glob need not be
/// built to know that. Its parser fails only on a class (`[`), an alternation
/// (`{`, `}`) or an escape (`\`); `*`, `**`, `?`, `,` and `!` outside those
/// always parse (globset 0.4's `Parser::parse`, held by the tests below).
pub(super) fn cannot_fail(glob: &str) -> bool {
    !glob.contains(['[', '{', '}', '\\'])
}

/// A glob for one shell word. A backslash escapes the next character on every
/// platform, as in a POSIX shell, so `\\` is a literal backslash.
///
/// Most words in a policy (`git`, `push`, `--force`) contain no glob syntax.
/// Those compare as strings: for them globset would build a regex that accepts
/// exactly the same word, and building ~500 regexes was most of the cost of
/// loading a policy on every hook call.
#[derive(Debug, Clone)]
pub(super) enum WordGlob {
    Literal(String),
    /// Shared with every word of the same compile spelled the same ([`Built`]).
    Glob(Arc<LazyGlob>),
}

impl WordGlob {
    pub(super) fn new(word: &str) -> Result<Self, globset::Error> {
        Self::new_in(word, &mut Built::default())
    }

    /// [`Self::new`], taking the glob from `built` when the same compile
    /// already built one for `word`.
    pub(super) fn new_in(word: &str, built: &mut Built) -> Result<Self, globset::Error> {
        if !word.contains(GLOB_CHARS) {
            return Ok(Self::Literal(word.to_owned()));
        }
        built
            .word(word, || {
                let mut glob = String::with_capacity(word.len());
                let mut chars = word.chars().peekable();
                while let Some(c) = chars.next() {
                    if c == '\\' && chars.peek() == Some(&'\\') {
                        chars.next();
                        glob.push_str(BACKSLASH);
                    } else {
                        glob.push(c);
                    }
                }
                GlobBuilder::new(&glob)
                    .literal_separator(false)
                    .backslash_escape(true)
                    .build()
                    // Shell words compare case-sensitively.
                    .map(|g| LazyGlob::new(g, false))
            })
            .map(Self::Glob)
    }

    pub(super) fn is_match(&self, word: &str) -> bool {
        match self {
            Self::Literal(literal) => literal == word,
            Self::Glob(glob) if word.contains('\\') => {
                glob.is_match(&word.replace('\\', BACKSLASH))
            }
            Self::Glob(glob) => glob.is_match(word),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The string comparison must agree with the regex globset would have
    /// built for the same word, on every candidate.
    #[test]
    fn literal_words_match_exactly_what_their_glob_matches() {
        let words = [
            "git", "--force", "-rf", "", "ünï", "a/b", "C:", "x.y", "$", "|", "=", "@",
        ];
        let candidates = [
            "git",
            "Git",
            "git ",
            "gitx",
            "xgit",
            "--force",
            "--force-with-lease",
            "-rf",
            "-fr",
            "",
            "ünï",
            "ÜNÏ",
            "a/b",
            "a\\b",
            "a//b",
            "C:",
            "c:",
            "x.y",
            "xzy",
            "$",
            "|",
            "=",
            "@",
        ];
        for word in words {
            let literal = WordGlob::new(word).unwrap();
            assert!(matches!(literal, WordGlob::Literal(_)), "{word}");
            let glob = WordGlob::Glob(Arc::new(LazyGlob::new(
                GlobBuilder::new(word)
                    .literal_separator(false)
                    .backslash_escape(true)
                    .build()
                    .unwrap(),
                false,
            )));
            for candidate in candidates {
                assert_eq!(
                    literal.is_match(candidate),
                    glob.is_match(candidate),
                    "{word:?} vs {candidate:?}"
                );
            }
        }
    }

    /// Every string over the characters [`cannot_fail`] lets through, up to
    /// five long, parses as a path glob and as a word glob: what lets the
    /// policy lint skip building them.
    #[test]
    fn what_cannot_fail_always_parses() {
        let alphabet = ['a', '/', '*', '?', ',', '!', '.', '-', 'é', ']'];
        let mut strings = vec![String::new()];
        let mut last = vec![String::new()];
        for _ in 0..5 {
            last = last
                .iter()
                .flat_map(|s| alphabet.iter().map(move |c| format!("{s}{c}")))
                .collect();
            strings.extend(last.iter().cloned());
        }
        for s in &strings {
            assert!(cannot_fail(s));
            assert!(WordGlob::new(s).is_ok(), "{s:?}");
            for separator in [false, true] {
                let built = GlobBuilder::new(s).literal_separator(separator).build();
                assert!(built.is_ok(), "{s:?}");
            }
        }
    }

    #[test]
    fn glob_syntax_still_compiles_to_a_glob() {
        for word in [
            "*sh*", "--force*", "x?", "[ab]", "{a,b}", r"\*", "a]", "!x", "a,b",
        ] {
            assert!(
                matches!(WordGlob::new(word), Ok(WordGlob::Glob(_))),
                "{word}"
            );
        }
        for malformed in ["{a", "[a", "b}", "a\\", "[z-a]", "{a,{b}"] {
            assert!(!cannot_fail(malformed), "{malformed}");
        }
        for malformed in ["{a", "[a", "b}"] {
            assert!(
                WordGlob::new(malformed).is_err(),
                "{malformed} is still rejected"
            );
        }
    }
}