Skip to content

Commit

Permalink
fix: use relative URLs when base_url not explictly set to ensure app …
Browse files Browse the repository at this point in the history
…is not vulnerable to host header attacks
  • Loading branch information
bethesque committed Jun 1, 2020
1 parent b385e53 commit 92c45a0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/pact_broker/doc/controllers/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def resource_exists? rel_name, context = nil
private

def base_url
PactBroker.configuration.base_url || request.base_url
# Using the X-Forwarded headers in the UI can leave the app vulnerable
# https://www.acunetix.com/blog/articles/automated-detection-of-host-header-attacks/
# Either use the explicitly configured base url or an empty string,
# rather than request.base_url, which uses the X-Forwarded headers.
PactBroker.configuration.base_url || ''
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/ui/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Base < Padrino::Application
set :dump_errors, false # The padrino logger logs these for us. If this is enabled we get duplicate logging.

def base_url
PactBroker.configuration.base_url || request.base_url
PactBroker.configuration.base_url || ''
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/ui/views/index/_navbar.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
%a{href: "#{base_url}?tags=true"}
Show latest tags
- else
%a{href: "#{base_url}"}
%a{href: "#{base_url}/"}
Hide latest tags

%a{href: "#{base_url}/hal-browser/browser.html"}
Expand Down
16 changes: 16 additions & 0 deletions spec/integration/ui/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,21 @@
expect(subject.body.scan('<tr').to_a.count).to eq 3
end
end

context "with the base_url not set" do
it "returns relative links" do
expect(subject.body).to include "href='/stylesheets"
end
end

context "with the base_url set" do
before do
allow(PactBroker.configuration).to receive(:base_url).and_return('http://example.org/pact-broker')
end

it "returns absolute links" do
expect(subject.body).to include "href='http://example.org/pact-broker/stylesheets"
end
end
end
end
16 changes: 16 additions & 0 deletions spec/lib/pact_broker/doc/controllers/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ module Controllers
subject
expect(last_response.body).to include "<html>"
end

context "with the base_url not set" do
it "returns relative links" do
expect(subject.body).to include "href='/css"
end
end

context "with the base_url set" do
before do
allow(PactBroker.configuration).to receive(:base_url).and_return('http://example.org/pact-broker')
end

it "returns absolute links" do
expect(subject.body).to include "href='http://example.org/pact-broker/css"
end
end
end

context "when the resource does not exist" do
Expand Down

0 comments on commit 92c45a0

Please sign in to comment.