Skip to content

Commit f44d1b8

Browse files
authored
Merge pull request #169 from github/protocol-pinning
Pin redir_protocols
2 parents bc11646 + c4a85f4 commit f44d1b8

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ group :development do
88
gem "pry", "~> 0.10"
99
gem "pry-byebug"
1010
gem "rspec", "~> 3.0"
11+
gem "rspec-retry", "~> 0.6"
1112
gem "rubocop", "~> 0.52"
1213
gem "webmock", "~> 3.8"
1314
end

lib/github-pages-health-check.rb

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def self.typhoeus_options
6666

6767
@typhoeus_options = {
6868
:followlocation => true,
69+
:redir_protocols => %i[http https], # don't allow non-http protocols on redirections
6970
:timeout => TIMEOUT,
7071
:accept_encoding => "gzip",
7172
:method => :head,

lib/github-pages-health-check/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module GitHubPages
44
module HealthCheck
5-
VERSION = "1.18.3"
5+
VERSION = "1.18.4"
66
end
77
end

spec/github_pages_health_check/domain_spec.rb

+72
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,78 @@
671671
end
672672
end
673673

674+
context "Protocol redirections" do
675+
before do
676+
@out = []
677+
678+
class SmallServer
679+
def initialize(location, out)
680+
@server = TCPServer.new(0)
681+
@port = @server.addr[1]
682+
@location = location
683+
@out = out
684+
end
685+
686+
attr_reader :port
687+
688+
def start
689+
loop do
690+
client = @server.accept
691+
692+
# Log
693+
@out << "HIT #{@port}"
694+
695+
# Continue with HTTP redirect
696+
if @location != "STOP"
697+
request = client.gets
698+
if request
699+
response = <<~RESPONSE
700+
HTTP/1.1 301 Moved Permanently
701+
Location: #{@location}
702+
RESPONSE
703+
client.print response
704+
end
705+
end
706+
client.close
707+
end
708+
end
709+
710+
def stop
711+
@server.close
712+
end
713+
end
714+
715+
@servers = []
716+
@servers << SmallServer.new("STOP", @out)
717+
@servers << SmallServer.new("ftp://localhost:#{@servers[0].port}/", @out)
718+
@servers.each do |server|
719+
Thread.new { server.start }
720+
end
721+
end
722+
723+
after do
724+
@servers.each(&:stop)
725+
end
726+
727+
it "it does not follow anything other than http/https by default", :retry => 3 do
728+
Typhoeus.get(
729+
"http://localhost:#{@servers[1].port}",
730+
GitHubPages::HealthCheck.typhoeus_options
731+
)
732+
expect(@out).to include("HIT #{@servers[1].port}")
733+
expect(@out).to_not include("HIT #{@servers[0].port}")
734+
end
735+
736+
it "it follows ftp if requested (negative test)", :retry => 3 do
737+
Typhoeus.get(
738+
"http://localhost:#{@servers[1].port}",
739+
GitHubPages::HealthCheck.typhoeus_options.merge(:redir_protocols => %i[http https ftp])
740+
)
741+
expect(@out).to include("HIT #{@servers[1].port}")
742+
expect(@out).to include("HIT #{@servers[0].port}")
743+
end
744+
end
745+
674746
context "served by pages" do
675747
let(:domain) { "http://choosealicense.com" }
676748
let(:status) { 200 }

spec/spec_helper.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
require "bundler/setup"
44
require "webmock/rspec"
55
require "pry-byebug"
6+
require "rspec/retry"
7+
68
require_relative "../lib/github-pages-health-check"
79

8-
WebMock.disable_net_connect!
10+
WebMock.disable_net_connect!(:allow => "localhost")
911

1012
RSpec.configure do |config|
1113
config.raise_errors_for_deprecations!

0 commit comments

Comments
 (0)