catch URI::Error instead of URI::InvalidURIError

test plan:
* using "mailto:example@example.com," in a link should
 not break course copies (or anything else) because it
 doesn't know how to rescue a URI::InvalidComponentError

closes #CNVS-22120

Change-Id: Iaac362a5bf33cd5fde05b01a578a1325f4126c6e
Reviewed-on: https://gerrit.instructure.com/59213
Tested-by: Jenkins
Reviewed-by: Jeremy Stanley <jeremy@instructure.com>
QA-Review: Clare Strong <clare@instructure.com>
Product-Review: James Williams  <jamesw@instructure.com>
This commit is contained in:
James Williams 2015-07-24 09:32:11 -06:00
parent 5d165fe8c8
commit 2818ecdb1a
13 changed files with 17 additions and 16 deletions

View File

@ -48,7 +48,7 @@ class ExternalContentController < ApplicationController
uri = URI.parse(value)
end
@retrieved_data[:url] = uri.to_s
rescue URI::InvalidURIError
rescue URI::Error
@retrieved_data[:url] = nil
end
end

View File

@ -1025,7 +1025,7 @@ class Account < ActiveRecord::Base
begin
value, uri = CanvasHttp.validate_url(self.settings[:auth_discovery_url])
self.auth_discovery_url = value
rescue URI::InvalidURIError, ArgumentError
rescue URI::Error, ArgumentError
errors.add(:discovery_url, t('errors.invalid_discovery_url', "The discovery URL is not valid" ))
end
end
@ -1483,7 +1483,7 @@ class Account < ActiveRecord::Base
def format_referer(referer_url)
begin
referer = URI(referer_url || '')
rescue URI::InvalidURIError
rescue URI::Error
return
end
return unless referer.host

View File

@ -1410,7 +1410,7 @@ class Attachment < ActiveRecord::Base
self.upload_error_message = t :upload_error_invalid_response_code, "Invalid response code, expected 200 got %{code}", :code => e.code
when CanvasHttp::RelativeUriError
self.upload_error_message = t :upload_error_relative_uri, "No host provided for the URL: %{url}", :url => url
when URI::InvalidURIError, ArgumentError
when URI::Error, ArgumentError
# assigning all ArgumentError to InvalidUri may be incorrect
self.upload_error_message = t :upload_error_invalid_url, "Could not parse the URL: %{url}", :url => url
when Timeout::Error

View File

@ -184,7 +184,7 @@ class ContextExternalTool < ActiveRecord::Base
begin
value, uri = CanvasHttp.validate_url(self.vendor_help_link)
self.vendor_help_link = uri.to_s
rescue URI::InvalidURIError, ArgumentError
rescue URI::Error, ArgumentError
self.vendor_help_link = nil
end
end
@ -225,7 +225,7 @@ class ContextExternalTool < ActiveRecord::Base
converter = CC::Importer::BLTIConverter.new
tool_hash = if config_type == 'by_url'
uri = URI.parse(config_url)
raise URI::InvalidURIError unless uri.host && uri.port
raise URI::Error unless uri.host && uri.port
converter.retrieve_and_convert_blti_url(config_url)
else
converter.convert_blti_xml(config_xml)
@ -240,7 +240,7 @@ class ContextExternalTool < ActiveRecord::Base
self.name = real_name unless real_name.blank?
rescue CC::Importer::BLTIConverter::CCImportError => e
@config_errors << [error_field, e.message]
rescue URI::InvalidURIError
rescue URI::Error
@config_errors << [:config_url, "Invalid URL"]
rescue ActiveRecord::RecordInvalid => e
@config_errors += Array(e.record.errors)

View File

@ -78,7 +78,7 @@ class DeveloperKey < ActiveRecord::Base
self_domain = URI.parse(self.redirect_uri).host
other_domain = URI.parse(redirect_uri).host
return self_domain.present? && other_domain.present? && (self_domain == other_domain || other_domain.end_with?(".#{self_domain}"))
rescue URI::InvalidURIError
rescue URI::Error
return false
end

View File

@ -69,7 +69,7 @@ module HtmlTextHelper
if src
begin
src = URI.join(opts[:base_url], src) if opts[:base_url]
rescue URI::InvalidURIError
rescue URI::Error
# do nothing, let src pass through as is
end
node['alt'] ? "[#{node['alt']}](#{src})" : src
@ -86,7 +86,7 @@ module HtmlTextHelper
if href
begin
href = URI.join(opts[:base_url], href) if opts[:base_url]
rescue URI::InvalidURIError
rescue URI::Error
# do nothing, let href pass through as is
end
href == subtext ? subtext : "[#{subtext}](#{href})"

View File

@ -235,7 +235,7 @@ module AuthenticationMethods
return nil if url.blank?
begin
uri = URI.parse(url)
rescue URI::InvalidURIError
rescue URI::Error
return nil
end
return nil unless uri.path[0] == '/'

View File

@ -90,7 +90,7 @@ module Canvas::Plugins::TicketingSystem
become_user_uri.query = (Hash[*(become_user_uri.query || '').
split('&').map {|part| part.split('=') }.flatten]).
merge({'become_user_id' => user_id}).to_query
rescue URI::InvalidURIError => e
rescue URI::Error => e
become_user_uri = "unable to parse uri: #{url}"
end
become_user_uri.to_s

View File

@ -156,7 +156,7 @@ module CC::Importer::Standard
end
end
end
rescue URI::InvalidURIError
rescue URI::Error
Rails.logger.warn "attempting to translate invalid url: #{val}"
end
end

View File

@ -183,7 +183,7 @@ class CourseLinkValidator
result = :unreachable
end
end
rescue URI::InvalidURIError
rescue URI::Error
result = :unparsable
end
result ||= :success

View File

@ -27,7 +27,7 @@ module CustomValidations
value, uri = CanvasHttp.validate_url(value)
record.send("#{attr}=", value)
rescue URI::InvalidURIError, ArgumentError
rescue URI::Error, ArgumentError
record.errors.add attr, 'is not a valid URL'
end
end

View File

@ -71,7 +71,7 @@ class ImportedHtmlConverter
def self.relative_url?(url)
URI.parse(url).relative? && !url.to_s.start_with?("//")
rescue URI::InvalidURIError
rescue URI::Error
# leave the url as it was
Rails.logger.warn "attempting to translate invalid url: #{url}"
false

View File

@ -221,6 +221,7 @@ describe ImportedHtmlConverter do
it "should not error on invalid urls" do
expect(ImportedHtmlConverter.relative_url?("stupid &^%$ url")).to be_falsey
expect(ImportedHtmlConverter.relative_url?("mailto:jfarnsworth@instructure.com,")).to be_falsey
end
end