Skip to content

Commit

Permalink
Merge pull request #69 from G-Rath/handle-invalid-urls
Browse files Browse the repository at this point in the history
Handle invalid URL with 400 response
  • Loading branch information
tylerhunt authored Mar 15, 2024
2 parents 3d00779 + 438a208 commit 9752e9d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/rack/canonical_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ def call(env)
host = evaluate_host(env)
redirect = Redirect.new(env, host, options)

if redirect.canonical?
app.call(env)
else
redirect.response
begin
return redirect.response unless redirect.canonical?
rescue Addressable::URI::InvalidURIError
return [400, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0" }, []]
end

app.call(env)
end

protected
Expand Down
29 changes: 29 additions & 0 deletions spec/rack/canonical_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ def call_app
end
end


context 'when the app happens to have an invalid uri error' do
before do
allow(inner_app)
.to receive(:call)
.with(env)
.and_raise(Addressable::URI::InvalidURIError)
end

it 'explodes as expected' do
expect { call_app }.to raise_error(Addressable::URI::InvalidURIError)
end
end

context 'with an X-Forwarded-Host' do
let(:url) { 'http://proxy.test/full/path' }

Expand All @@ -98,6 +112,21 @@ def call_app

it_behaves_like 'a non-matching request'
end

context 'which is an invalid uri' do
let(:headers) { { 'HTTP_X_FORWARDED_HOST' => '[${jndi:ldap://172.16.26.190:52314/nessus}]/' } }

it { should_not be_redirect }

it { expect(response[0]).to be 400 }

it 'does not call the inner app' do
expect(inner_app).to_not receive(:call)
call_app
end

it { expect(response).to_not have_header('cache-control') }
end
end

context 'without a host' do
Expand Down

0 comments on commit 9752e9d

Please sign in to comment.