Skip to content

Commit

Permalink
Merge pull request #2130 from seek4science/fix-examine-url-error-hand…
Browse files Browse the repository at this point in the history
…ling

Handle exceptions when examining URLs
  • Loading branch information
fbacall authored Feb 3, 2025
2 parents 44bf9a5 + 6f82575 commit 9f4f384
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ gem 'request_store'

gem 'bundler', '>= 1.8.4'

gem 'ro-crate', '~> 0.5.2'
gem 'ro-crate', '~> 0.5.3'

gem 'rugged'
gem 'i18n-js'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ GEM
json (~> 2.3.0)
ucf (~> 2.0.2)
uuid (~> 2.3)
ro-crate (0.5.2)
ro-crate (0.5.3)
addressable (>= 2.7, < 2.9)
rubyzip (~> 2.0.0)
rsolr (2.5.0)
Expand Down Expand Up @@ -1069,7 +1069,7 @@ DEPENDENCIES
rfc-822
rmagick (= 5.3.0)
ro-bundle (~> 0.3.0)
ro-crate (~> 0.5.2)
ro-crate (~> 0.5.3)
rspec-rails (~> 5.1)
rubocop
ruby-prof
Expand Down
27 changes: 12 additions & 15 deletions lib/seek/upload_handling/examine_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ def examine_url
@type = 'warning'
@warning_msg = "Unhandled URL scheme: #{uri.scheme}. The given URL will be presented as a clickable link."
end
rescue URI::InvalidURIError
@type = 'override'
@error_msg = 'The URL appears to be invalid.'
rescue OpenSSL::OpenSSLError
@type = 'error'
@error_msg = 'SSL connection to the URL failed - Please check the certificate is valid.'
rescue StandardError => e
handle_exception_response(e)
raise e if Rails.application.config.consider_all_requests_local
exception_notification(500, e)
@type = 'error'
@error_msg = 'An unexpected error occurred whilst accessing the URL.'
end

respond_to do |format|
Expand Down Expand Up @@ -75,26 +84,14 @@ def handle_bad_http_response(code)
@error_msg = "We can't find out information about this URL - Method not allowed response."
when 404
@type = 'override'
@error_msg = 'Nothing can be found at that URL. Please check the address and try again'
when 400
@type = 'override'
@error_msg = 'The URL appears to be invalid'
@error_msg = 'Nothing can be found at that URL. Please check the address and try again.'
when 490
@error_msg = 'That URL is inaccessible. Please check the address and try again'
@error_msg = 'That URL is inaccessible. Please check the address and try again.'
else
@error_msg = "We can't find out information about this URL - unhandled response code: #{code}"
end
end

def handle_exception_response(exception)
case exception
when URI::InvalidURIError
handle_bad_http_response(400)
else
fail exception
end
end

def is_myexperiment_url?(url)
URI uri = URI(url)
uri.hostname.include?('myexperiment.org') && uri.path.end_with?('.html')
Expand Down
21 changes: 21 additions & 0 deletions test/functional/content_blobs_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,27 @@ def setup
assert assigns(:warning_msg)
end

test 'examine url bad cert' do
stub_request(:head, 'https://iuseaselfsigned.cert').to_raise(OpenSSL::SSL::SSLError)
get :examine_url, xhr: true, params: { data_url: 'https://iuseaselfsigned.cert' }
assert_response 400
assert @response.body.include?('SSL connection to the URL failed')
assert_equal 'error', assigns(:type)
assert assigns(:error_msg)
end

test 'examine url unhandled exception' do
Rails.application.config.consider_all_requests_local = false
stub_request(:head, 'https://somethingeterrible').to_raise(NoMethodError)
get :examine_url, xhr: true, params: { data_url: 'https://somethingeterrible' }
assert_response 400
assert @response.body.include?('An unexpected error occurred')
assert_equal 'error', assigns(:type)
assert assigns(:error_msg)
ensure
Rails.application.config.consider_all_requests_local = true
end

test 'examine url localhost' do
begin
# Need to allow the request through so that `private_address_check` can catch it.
Expand Down

0 comments on commit 9f4f384

Please sign in to comment.