Skip to content

Commit dfabd99

Browse files
committed
Add restriction and validation for download urls
1 parent 5dd816a commit dfabd99

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ jobs:
369369
- name: C:/msys64/mingw64/bin/gcc.exe not installed
370370
run: ruby -e "abort if File.exist?('C:/msys64/mingw64/bin/gcc.exe')"
371371

372+
validate-windows-versions:
373+
runs-on: ubuntu-latest
374+
steps:
375+
- uses: actions/checkout@v6
376+
- uses: ./
377+
with:
378+
ruby-version: ruby
379+
- run: ruby generate-windows-versions.rb
380+
- name: Check generated files are up to date
381+
run: git diff --exit-code
382+
372383
lint:
373384
runs-on: ubuntu-22.04
374385
steps:

generate-windows-versions.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,31 @@
22
require 'yaml'
33
require 'json'
44

5+
# General rules:
6+
# - All the static parts of the expected URL are checked literally.
7+
# - Don't forget to escape dot (`.`) and other special characters when used literally.
8+
# - Each path component must begin with [\w], or a even more restrictive character set.
9+
# - Percent (`%`) shall not be allowed to avoid any percent encoding.
10+
WINDOWS_VERSIONS_URLS_REGEXPS = [
11+
%r{^https://github\.com/oneclick/rubyinstaller2?/releases/download/\w[\w.-]*/\w[\w.-]*$},
12+
%r{^https://github\.com/MSP-Greg/ruby-loco/releases/download/\w[\w.-]*/\w[\w.-]*$}
13+
].freeze
14+
15+
WINDOWS_TOOLCHAIN_VERSIONS_URLS_REGEXPS = [
16+
%r{^https://github\.com/oneclick/rubyinstaller/releases/download/devkit-4\.7\.2/DevKit-mingw64-64-4\.7\.2-20130224-1432-sfx\.exe$},
17+
%r{^https://github\.com/ruby/setup-msys2-gcc/releases/download/\w[\w.-]*/\w[\w@.-]*$},
18+
%r{^https://github\.com/ruby/setup-msys2-gcc/releases/latest/download/\w[\w@.-]*$}
19+
].freeze
20+
21+
# Validate all the URLs in the versions json
22+
def validate(versions, allowed_urls_regexps)
23+
versions.values.flat_map(&:values).each do |url|
24+
if allowed_urls_regexps.none? { |regexp| regexp =~ url }
25+
raise SecurityError, "Unexpected URL: #{url}"
26+
end
27+
end
28+
end
29+
530
min_requirements = ['~> 2.0.0', '~> 2.1.9', '>= 2.2.6'].map { |req| Gem::Requirement.new(req) }
631

732
url = 'https://raw.githubusercontent.com/oneclick/rubyinstaller.org-website/master/_data/downloads.yaml'
@@ -48,6 +73,7 @@
4873
'x64' => 'https://github.com/MSP-Greg/ruby-loco/releases/download/ruby-master/ruby-ucrt.7z'
4974
}
5075

76+
validate(versions, WINDOWS_VERSIONS_URLS_REGEXPS)
5177
File.binwrite 'windows-versions.json', "#{JSON.pretty_generate(versions)}\n"
5278

5379
base_url = 'https://github.com/ruby/setup-msys2-gcc/releases/latest/download/windows-toolchain.json'
@@ -90,4 +116,5 @@
90116
end
91117
end
92118

119+
validate(versions, WINDOWS_TOOLCHAIN_VERSIONS_URLS_REGEXPS)
93120
File.binwrite 'windows-toolchain-versions.json', "#{JSON.pretty_generate(versions)}\n"

0 commit comments

Comments
 (0)