Skip to content

Commit c41a3b7

Browse files
committed
Decode URL-encoded path segments before canonicalization
Prevents URL-encoded path traversal attacks.
1 parent dd2f06c commit c41a3b7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

automerge-check.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ def check_json_urls_from_content(filename, content, allowed_prefixes)
9090

9191
def canonicalize_url(url)
9292
uri = URI.parse(url)
93-
# Normalize path to resolve . and .. segments
94-
uri.path = File.expand_path(uri.path) if uri.path
93+
if uri.path
94+
decoded_path = URI.decode_www_form_component(uri.path)
95+
uri.path = File.expand_path(decoded_path)
96+
end
9597
uri.to_s
9698
end
9799

@@ -235,10 +237,10 @@ def test_check_json_urls_from_content_with_invalid_urls
235237
end
236238

237239
def test_path_traversal_urls_are_rejected
238-
# These URLs pass the prefix check but resolve to different locations after canonicalization
239240
malicious_urls = [
240241
"https://github.com/ruby/setup-msys2-gcc/releases/../../evil-repo/releases/download/malware.exe",
241242
"https://github.com/ruby/setup-msys2-gcc/releases/./../../evil-repo/releases/download/malware.exe",
243+
"https://github.com/ruby/setup-msys2-gcc/releases/%2E%2E/%2E%2E/evil-repo/releases/download/malware.exe",
242244
]
243245
malicious_urls.each do |url|
244246
checker = AutomergeCheck.new("master")

0 commit comments

Comments
 (0)