diff options
author | Kirill Bulatov <mail4score@gmail.com> | 2023-07-22 22:58:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-22 19:58:52 +0000 |
commit | d20cce9401b2d449ee39013da3eb5f0f66a39c98 (patch) | |
tree | aae97f577a8db1a2c0f422e110369145cc48a129 | |
parent | 67a433ceedf3e415d9c989112d1248a7562a1ac5 (diff) | |
download | alacritty-d20cce9401b2d449ee39013da3eb5f0f66a39c98.tar.gz alacritty-d20cce9401b2d449ee39013da3eb5f0f66a39c98.zip |
Make URL_REGEX more strict
-rw-r--r-- | alacritty/src/config/ui_config.rs | 60 | ||||
-rw-r--r-- | extra/man/alacritty.5.scd | 2 |
2 files changed, 60 insertions, 2 deletions
diff --git a/alacritty/src/config/ui_config.rs b/alacritty/src/config/ui_config.rs index a2e8d4e1..d2e8e600 100644 --- a/alacritty/src/config/ui_config.rs +++ b/alacritty/src/config/ui_config.rs @@ -25,7 +25,7 @@ use crate::config::window::WindowConfig; /// Regex used for the default URL hint. #[rustfmt::skip] -const URL_REGEX: &str = "(ipfs:|ipns:|magnet:|mailto:|gemini:|gopher:|https:|http:|news:|file:|git:|ssh:|ftp:)\ +const URL_REGEX: &str = "(ipfs:|ipns:|magnet:|mailto:|gemini://|gopher://|https://|http://|news:|file:|git://|ssh:|ftp://)\ [^\u{0000}-\u{001F}\u{007F}-\u{009F}<>\"\\s{-}\\^⟨⟩`]+"; #[derive(ConfigDeserialize, Clone, Debug, PartialEq)] @@ -547,3 +547,61 @@ impl PartialEq for LazyRegexVariant { } } impl Eq for LazyRegexVariant {} + +#[cfg(test)] +mod tests { + use super::*; + + use alacritty_terminal::term::test::mock_term; + + use crate::display::hint::visible_regex_match_iter; + + #[test] + fn positive_url_parsing_regex_test() { + for regular_url in [ + "ipfs:s0mEhAsh", + "ipns:an0TherHash1234", + "magnet:?xt=urn:btih:L0UDHA5H12", + "mailto:example@example.org", + "gemini://gemini.example.org/", + "gopher://gopher.example.org", + "https://www.example.org", + "http://example.org", + "news:some.news.portal", + "file:///C:/Windows/", + "file:/home/user/whatever", + "git://github.com/user/repo.git", + "ssh:git@github.com:user/repo.git", + "ftp://ftp.example.org", + ] { + let term = mock_term(regular_url); + let regex = RegexSearch::new(URL_REGEX).unwrap(); + let matches = visible_regex_match_iter(&term, ®ex).collect::<Vec<_>>(); + assert_eq!( + matches.len(), + 1, + "Should have exactly one match url {regular_url}, but instead got: {matches:?}" + ) + } + } + + #[test] + fn negative_url_parsing_regex_test() { + for url_like in [ + "http::trace::on_request::log_parameters", + "http//www.example.org", + "/user:example.org", + "mailto: example@example.org", + "http://<script>alert('xss')</script>", + "mailto:", + ] { + let term = mock_term(url_like); + let regex = RegexSearch::new(URL_REGEX).unwrap(); + let matches = visible_regex_match_iter(&term, ®ex).collect::<Vec<_>>(); + assert!( + matches.is_empty(), + "Should not match url in string {url_like}, but instead got: {matches:?}" + ) + } + } +} diff --git a/extra/man/alacritty.5.scd b/extra/man/alacritty.5.scd index f9e82ba3..b6b8d8eb 100644 --- a/extra/man/alacritty.5.scd +++ b/extra/man/alacritty.5.scd @@ -648,7 +648,7 @@ _action_ or a _command_. hovering over the hint text with all _mods_ pressed. Default: _[{ - regex = "(mailto:|gemini:|gopher:|https:|http:|news:|file:|git:|ssh:|ftp:)[^\\u0000-\\u001F\\u007F-\\u009F<>\\"\\\\s{-}\\\\^⟨⟩`]+",++ + regex = "(ipfs:|ipns:|magnet:|mailto:|gemini://|gopher://|https://|http://|news:|file:|git://|ssh:|ftp://)[^\\u0000-\\u001F\\u007F-\\u009F<>\\"\\\\s{-}\\\\^⟨⟩`]+",++ hyperlinks = true,++ post_processing = true,++ persist = false,++ |