-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use biome_deserialize_macros::Deserializable; | ||
|
||
/// A restricted glov pattern only supports the following syntaxes: | ||
/// | ||
/// - star `*` that matches zero or more character inside a path segment | ||
/// - globstar `**` that matches zero or more path segments | ||
/// - Use `\*` to escape `*` | ||
/// - `?`, `[`, `]`, `{`, and `}` must be escaped using `\`. | ||
/// These characters are reserved for future use. | ||
/// - `!` must be escaped if it is the first characrter of the pattern | ||
/// | ||
/// A path segment is delimited by path separator `/` or the start/end of the path. | ||
#[derive(Clone, Debug, Deserializable, serde::Deserialize, serde::Serialize)] | ||
#[serde(try_from = "String", into = "String")] | ||
pub struct RestrictedGlob(globset::GlobMatcher); | ||
|
||
impl std::ops::Deref for RestrictedGlob { | ||
type Target = globset::GlobMatcher; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl std::fmt::Display for RestrictedGlob { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let repr = self.0.glob().to_string(); | ||
f.write_str(&repr) | ||
} | ||
} | ||
|
||
impl From<RestrictedGlob> for String { | ||
fn from(value: RestrictedGlob) -> Self { | ||
value.to_string() | ||
} | ||
} | ||
|
||
impl std::str::FromStr for RestrictedGlob { | ||
type Err = globset::ErrorKind; | ||
|
||
fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
is_restricted_glob(value)?; | ||
let mut glob_builder = globset::GlobBuilder::new(value); | ||
// Allow escaping with `\` on all platforms. | ||
glob_builder.backslash_escape(true); | ||
// Only `**` can match `/` | ||
glob_builder.literal_separator(true); | ||
match glob_builder.build() { | ||
Ok(glob) => Ok(RestrictedGlob(glob.compile_matcher())), | ||
Err(error) => Err(error.kind().clone()), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<String> for RestrictedGlob { | ||
type Error = globset::ErrorKind; | ||
|
||
fn try_from(value: String) -> Result<Self, Self::Error> { | ||
value.parse() | ||
} | ||
} | ||
|
||
#[cfg(feature = "schemars")] | ||
impl schemars::JsonSchema for RestrictedGlob { | ||
fn schema_name() -> String { | ||
"Regex".to_string() | ||
} | ||
|
||
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { | ||
String::json_schema(gen) | ||
} | ||
} | ||
|
||
/// Returns an error if `pattern` doesn't follow the restricted glob syntax. | ||
fn is_restricted_glob(pattern: &str) -> Result<(), globset::ErrorKind> { | ||
let mut it = pattern.bytes().enumerate(); | ||
while let Some((i, c)) = it.next() { | ||
match c { | ||
b'!' if i == 0 => { | ||
return Err(globset::ErrorKind::Regex( | ||
r"Negated globs `!` are not supported. Use `\!` to escape the character." | ||
.to_string(), | ||
)); | ||
} | ||
b'\\' => { | ||
// Accept a restrictive set of escape sequence | ||
if let Some((_, c)) = it.next() { | ||
if !matches!(c, b'!' | b'*' | b'?' | b'{' | b'}' | b'[' | b']' | b'\\') { | ||
// SAFETY: safe because of the match | ||
let c = unsafe { char::from_u32_unchecked(c as u32) }; | ||
// Escape sequences https://docs.rs/regex/latest/regex/#escape-sequences | ||
// and Perl char classes https://docs.rs/regex/latest/regex/#perl-character-classes-unicode-friendly | ||
return Err(globset::ErrorKind::Regex(format!( | ||
"Escape sequence \\{c} is not supported." | ||
))); | ||
} | ||
} else { | ||
return Err(globset::ErrorKind::DanglingEscape); | ||
} | ||
} | ||
b'?' => { | ||
return Err(globset::ErrorKind::Regex( | ||
r"`?` matcher is not supported. Use `\?` to escape the character.".to_string(), | ||
)); | ||
} | ||
b'[' | b']' => { | ||
return Err(globset::ErrorKind::Regex( | ||
r"Character class `[]` are not supported. Use `\[` and `\]` to escape the characters." | ||
.to_string(), | ||
)); | ||
} | ||
b'{' | b'}' => { | ||
return Err(globset::ErrorKind::Regex( | ||
r"Alternates `{}` are not supported. Use `\{` and `\}` to escape the characters.".to_string(), | ||
)); | ||
} | ||
_ => {} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_is_restricted_glob() { | ||
assert!(is_restricted_glob("!*.js").is_err()); | ||
assert!(is_restricted_glob("*.[jt]s").is_err()); | ||
assert!(is_restricted_glob("*.{js,ts}").is_err()); | ||
assert!(is_restricted_glob("?*.js").is_err()); | ||
assert!(is_restricted_glob(r"\").is_err()); | ||
assert!(is_restricted_glob("!").is_err()); | ||
|
||
assert!(is_restricted_glob("*.js").is_ok()); | ||
assert!(is_restricted_glob("**/*.js").is_ok()); | ||
assert!(is_restricted_glob(r"\*").is_ok()); | ||
assert!(is_restricted_glob(r"\!").is_ok()); | ||
} | ||
|
||
#[test] | ||
fn test_restricted_regex() { | ||
assert!(!"*.js" | ||
.parse::<RestrictedGlob>() | ||
.unwrap() | ||
.is_match("file/path.js")); | ||
|
||
assert!("**/*.js" | ||
.parse::<RestrictedGlob>() | ||
.unwrap() | ||
.is_match("file/path.js")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters