Skip to content

A platform for community discussion. Free, open, simple.

Treat non-ascii URLs in `UrlValidator`.

Guo Xiang Tan4b51871f6a90
diff --git a/lib/validators/url_validator.rb b/lib/validators/url_validator.rb
index 7c91bf206ba..7edfe431f3c 100644
--- a/lib/validators/url_validator.rb
+++ b/lib/validators/url_validator.rb
@@ -5,7 +5,12 @@ class UrlValidator < ActiveModel::EachValidator
begin
uri = URI.parse(value)
uri.is_a?(URI::HTTP) && !uri.host.nil? && uri.host.include?(".")
- rescue
+ rescue URI::InvalidURIError => e
+ if (e.message =~ /URI must be ascii only/)
+ value = URI.encode(value)
+ retry
+ end
+
nil
end
diff --git a/spec/components/validators/url_validator_spec.rb b/spec/components/validators/url_validator_spec.rb
index 5554700b774..4278fc2ecbc 100644
--- a/spec/components/validators/url_validator_spec.rb
+++ b/spec/components/validators/url_validator_spec.rb
@@ -23,6 +23,8 @@ RSpec.describe UrlValidator do
[
"http://discourse.productions",
"https://google.com",
+ 'http://xn--nw2a.xn--j6w193g/',
+ "http://見.香港/",
].each do |valid_url|
it "#{valid_url} should be valid" do
record.website = valid_url