|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use regex::{Regex, RegexSet}; |
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +#[derive(Clone, Debug, Default)] |
| 9 | +pub struct TargetAllowList { |
| 10 | + pub functions: AllowList, |
| 11 | + pub modules: AllowList, |
| 12 | + pub source_files: AllowList, |
| 13 | +} |
| 14 | + |
| 15 | +impl TargetAllowList { |
| 16 | + pub fn new(modules: AllowList, source_files: AllowList) -> Self { |
| 17 | + // Allow all. |
| 18 | + let functions = AllowList::default(); |
| 19 | + |
| 20 | + Self { |
| 21 | + functions, |
| 22 | + modules, |
| 23 | + source_files, |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Debug)] |
| 29 | +pub struct AllowList { |
| 30 | + allow: RegexSet, |
| 31 | + deny: RegexSet, |
| 32 | +} |
| 33 | + |
| 34 | +impl AllowList { |
| 35 | + pub fn new(allow: RegexSet, deny: RegexSet) -> Self { |
| 36 | + Self { allow, deny } |
| 37 | + } |
| 38 | + |
| 39 | + pub fn load(path: impl AsRef<Path>) -> Result<Self> { |
| 40 | + let path = path.as_ref(); |
| 41 | + let text = std::fs::read_to_string(path)?; |
| 42 | + Self::parse(&text) |
| 43 | + } |
| 44 | + |
| 45 | + pub fn parse(text: &str) -> Result<Self> { |
| 46 | + use std::io::{BufRead, BufReader}; |
| 47 | + |
| 48 | + let reader = BufReader::new(text.as_bytes()); |
| 49 | + |
| 50 | + let mut allow = vec![]; |
| 51 | + let mut deny = vec![]; |
| 52 | + |
| 53 | + // We could just collect and pass to the `RegexSet` ctor. |
| 54 | + // |
| 55 | + // Instead, check each rule individually for diagnostic purposes. |
| 56 | + for (index, line) in reader.lines().enumerate() { |
| 57 | + let line = line?; |
| 58 | + |
| 59 | + match AllowListLine::parse(&line) { |
| 60 | + Ok(valid) => { |
| 61 | + use AllowListLine::*; |
| 62 | + |
| 63 | + match valid { |
| 64 | + Blank | Comment => { |
| 65 | + // Ignore. |
| 66 | + } |
| 67 | + Allow(re) => { |
| 68 | + allow.push(re); |
| 69 | + } |
| 70 | + Deny(re) => { |
| 71 | + deny.push(re); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + Err(err) => { |
| 76 | + // Ignore invalid lines, but warn. |
| 77 | + let line_number = index + 1; |
| 78 | + warn!("error at line {}: {}", line_number, err); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + let allow = RegexSet::new(allow.iter().map(|re| re.as_str()))?; |
| 84 | + let deny = RegexSet::new(deny.iter().map(|re| re.as_str()))?; |
| 85 | + let allowlist = AllowList::new(allow, deny); |
| 86 | + |
| 87 | + Ok(allowlist) |
| 88 | + } |
| 89 | + |
| 90 | + pub fn is_allowed(&self, path: impl AsRef<str>) -> bool { |
| 91 | + let path = path.as_ref(); |
| 92 | + |
| 93 | + // Allowed if rule-allowed but not excluded by a negative (deny) rule. |
| 94 | + self.allow.is_match(path) && !self.deny.is_match(path) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl Default for AllowList { |
| 99 | + fn default() -> Self { |
| 100 | + // Unwrap-safe due to valid constant expr. |
| 101 | + let allow = RegexSet::new([".*"]).unwrap(); |
| 102 | + let deny = RegexSet::empty(); |
| 103 | + |
| 104 | + AllowList::new(allow, deny) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +pub enum AllowListLine { |
| 109 | + Blank, |
| 110 | + Comment, |
| 111 | + Allow(Regex), |
| 112 | + Deny(Regex), |
| 113 | +} |
| 114 | + |
| 115 | +impl AllowListLine { |
| 116 | + pub fn parse(line: &str) -> Result<Self> { |
| 117 | + let line = line.trim(); |
| 118 | + |
| 119 | + // Allow and ignore blank lines. |
| 120 | + if line.is_empty() { |
| 121 | + return Ok(Self::Blank); |
| 122 | + } |
| 123 | + |
| 124 | + // Support comments of the form `# <comment>`. |
| 125 | + if line.starts_with("# ") { |
| 126 | + return Ok(Self::Comment); |
| 127 | + } |
| 128 | + |
| 129 | + // Deny rules are of the form `! <rule>`. |
| 130 | + if let Some(expr) = line.strip_prefix("! ") { |
| 131 | + let re = glob_to_regex(expr)?; |
| 132 | + return Ok(Self::Deny(re)); |
| 133 | + } |
| 134 | + |
| 135 | + // Try to interpret as allow rule. |
| 136 | + let re = glob_to_regex(line)?; |
| 137 | + Ok(Self::Allow(re)) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +#[allow(clippy::single_char_pattern)] |
| 142 | +fn glob_to_regex(expr: &str) -> Result<Regex> { |
| 143 | + // Don't make users escape Windows path separators. |
| 144 | + let expr = expr.replace(r"\", r"\\"); |
| 145 | + |
| 146 | + // Translate glob wildcards into quantified regexes. |
| 147 | + let expr = expr.replace("*", ".*"); |
| 148 | + |
| 149 | + // Anchor to line start and end. |
| 150 | + let expr = format!("^{expr}$"); |
| 151 | + |
| 152 | + Ok(Regex::new(&expr)?) |
| 153 | +} |
| 154 | + |
| 155 | +#[cfg(test)] |
| 156 | +mod tests; |
0 commit comments