url: restrict file paths regex to one slash

This restricts the valid path prefixes to prevent false matches caused
by literal dot.

Signed-off-by: Weizhao Ouyang <o451686892@gmail.com>
This commit is contained in:
Weizhao Ouyang
2025-05-14 23:05:36 +08:00
parent 6984b1ec48
commit 528814da79

View File

@@ -26,7 +26,7 @@ pub const regex =
"(?:" ++ url_schemes ++ "(?:" ++ url_schemes ++
\\)(?: \\)(?:
++ ipv6_url_pattern ++ ++ ipv6_url_pattern ++
\\|[\w\-.~:/?#@!$&*+,;=%]+(?:[\(\[]\w*[\)\]])?)+(?<![,.])|(?:\.\.\/|\.\/*|\/)[\w\-.~:\/?#@!$&*+,;=%]+(?:\/[\w\-.~:\/?#@!$&*+,;=%]*)* \\|[\w\-.~:/?#@!$&*+,;=%]+(?:[\(\[]\w*[\)\]])?)+(?<![,.])|(?:\.\.\/|\.\/|\/)[\w\-.~:\/?#@!$&*+,;=%]+(?:\/[\w\-.~:\/?#@!$&*+,;=%]*)*
; ;
const url_schemes = const url_schemes =
\\https?://|mailto:|ftp://|file:|ssh:|git://|ssh://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news: \\https?://|mailto:|ftp://|file:|ssh:|git://|ssh://|tel:|magnet:|ipfs://|ipns://|gemini://|gopher://|news:
@@ -112,6 +112,10 @@ test "url regex" {
.input = "url with dashes [mode 2027](https://github.com/contour-terminal/terminal-unicode-core) for better unicode support", .input = "url with dashes [mode 2027](https://github.com/contour-terminal/terminal-unicode-core) for better unicode support",
.expect = "https://github.com/contour-terminal/terminal-unicode-core", .expect = "https://github.com/contour-terminal/terminal-unicode-core",
}, },
.{
.input = "dot.http://example.com",
.expect = "http://example.com",
},
// weird characters in URL // weird characters in URL
.{ .{
.input = "weird characters https://example.com/~user/?query=1&other=2#hash and more", .input = "weird characters https://example.com/~user/?query=1&other=2#hash and more",