Skip to content

Commit

Permalink
fix: only catch our invalid uri errors
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 15, 2024
1 parent f820087 commit 438a208
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/rack/canonical_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +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
rescue Addressable::URI::InvalidURIError
[400, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "0" }, []]

app.call(env)
end

protected
Expand Down
14 changes: 14 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 Down

0 comments on commit 438a208

Please sign in to comment.