Skip to content

Commit 92c45a0

Browse files
committed
fix: use relative URLs when base_url not explictly set to ensure app is not vulnerable to host header attacks
1 parent b385e53 commit 92c45a0

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

lib/pact_broker/doc/controllers/app.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def resource_exists? rel_name, context = nil
4646
private
4747

4848
def base_url
49-
PactBroker.configuration.base_url || request.base_url
49+
# Using the X-Forwarded headers in the UI can leave the app vulnerable
50+
# https://www.acunetix.com/blog/articles/automated-detection-of-host-header-attacks/
51+
# Either use the explicitly configured base url or an empty string,
52+
# rather than request.base_url, which uses the X-Forwarded headers.
53+
PactBroker.configuration.base_url || ''
5054
end
5155
end
5256
end

lib/pact_broker/ui/controllers/base_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Base < Padrino::Application
1212
set :dump_errors, false # The padrino logger logs these for us. If this is enabled we get duplicate logging.
1313

1414
def base_url
15-
PactBroker.configuration.base_url || request.base_url
15+
PactBroker.configuration.base_url || ''
1616
end
1717
end
1818
end

lib/pact_broker/ui/views/index/_navbar.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
%a{href: "#{base_url}?tags=true"}
88
Show latest tags
99
- else
10-
%a{href: "#{base_url}"}
10+
%a{href: "#{base_url}/"}
1111
Hide latest tags
1212

1313
%a{href: "#{base_url}/hal-browser/browser.html"}

spec/integration/ui/index_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,21 @@
3232
expect(subject.body.scan('<tr').to_a.count).to eq 3
3333
end
3434
end
35+
36+
context "with the base_url not set" do
37+
it "returns relative links" do
38+
expect(subject.body).to include "href='/stylesheets"
39+
end
40+
end
41+
42+
context "with the base_url set" do
43+
before do
44+
allow(PactBroker.configuration).to receive(:base_url).and_return('http://example.org/pact-broker')
45+
end
46+
47+
it "returns absolute links" do
48+
expect(subject.body).to include "href='http://example.org/pact-broker/stylesheets"
49+
end
50+
end
3551
end
3652
end

spec/lib/pact_broker/doc/controllers/app_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ module Controllers
2626
subject
2727
expect(last_response.body).to include "<html>"
2828
end
29+
30+
context "with the base_url not set" do
31+
it "returns relative links" do
32+
expect(subject.body).to include "href='/css"
33+
end
34+
end
35+
36+
context "with the base_url set" do
37+
before do
38+
allow(PactBroker.configuration).to receive(:base_url).and_return('http://example.org/pact-broker')
39+
end
40+
41+
it "returns absolute links" do
42+
expect(subject.body).to include "href='http://example.org/pact-broker/css"
43+
end
44+
end
2945
end
3046

3147
context "when the resource does not exist" do

0 commit comments

Comments
 (0)