Permalink
Browse files

Added some rudimentary validity checking to host component.

* Proper verification of the host is difficult because older versions
  of Ruby do not support Unicode natively. This makes IRI support
  difficult when trying to validate.
  • Loading branch information...
sporkmonger committed Jun 3, 2012
1 parent 8f17659 commit 8fb39d682a80454cc1fb1185b85fd8fedac026fc
Showing with 84 additions and 0 deletions.
  1. +8 −0 lib/addressable/uri.rb
  2. +76 −0 spec/addressable/uri_spec.rb
View
@@ -1019,6 +1019,14 @@ def host=(new_host)
end
@host = new_host ? new_host.to_str : nil
+ unreserved = CharacterClasses::UNRESERVED
+ sub_delims = CharacterClasses::SUB_DELIMS
+ if @host != nil && (@host =~ /[<>{}\/\?\#\@]/ ||
+ (@host[/^\[(.*)\]$/, 1] != nil && @host[/^\[(.*)\]$/, 1] !~
+ Regexp.new("^[#{unreserved}#{sub_delims}:]*$")))
+ raise InvalidURIError, "Invalid character in host: '#{@host.to_s}'"
+ end
+
# Reset dependant values
@authority = nil
@normalized_host = nil
@@ -150,6 +150,14 @@ def to_s
end
end
+describe Addressable::URI, "when created with an invalid host" do
+ it "should raise an error" do
+ (lambda do
+ Addressable::URI.new(:host => "<invalid>")
+ end).should raise_error(Addressable::URI::InvalidURIError)
+ end
+end
+
describe Addressable::URI, "when created from nil components" do
before do
@uri = Addressable::URI.new
@@ -1135,6 +1143,74 @@ def to_s
end
end
+describe Addressable::URI, "when parsing IPv6 addresses" do
+ it "should not raise an error for " +
+ "'http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'" do
+ Addressable::URI.parse("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[fe80:0:0:0:200:f8ff:fe21:67cf]/'" do
+ Addressable::URI.parse("http://[fe80:0:0:0:200:f8ff:fe21:67cf]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[fe80::200:f8ff:fe21:67cf]/'" do
+ Addressable::URI.parse("http://[fe80::200:f8ff:fe21:67cf]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[::1]/'" do
+ Addressable::URI.parse("http://[::1]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[fe80::1]/'" do
+ Addressable::URI.parse("http://[fe80::1]/")
+ end
+
+ it "should raise an error for " +
+ "'http://[<invalid>]/'" do
+ (lambda do
+ Addressable::URI.parse("http://[<invalid>]/")
+ end).should raise_error(Addressable::URI::InvalidURIError)
+ end
+end
+
+describe Addressable::URI, "when parsing IPvFuture addresses" do
+ it "should not raise an error for " +
+ "'http://[v9.3ffe:1900:4545:3:200:f8ff:fe21:67cf]/'" do
+ Addressable::URI.parse("http://[v9.3ffe:1900:4545:3:200:f8ff:fe21:67cf]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[vff.fe80:0:0:0:200:f8ff:fe21:67cf]/'" do
+ Addressable::URI.parse("http://[vff.fe80:0:0:0:200:f8ff:fe21:67cf]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[v12.fe80::200:f8ff:fe21:67cf]/'" do
+ Addressable::URI.parse("http://[v12.fe80::200:f8ff:fe21:67cf]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[va0.::1]/'" do
+ Addressable::URI.parse("http://[va0.::1]/")
+ end
+
+ it "should not raise an error for " +
+ "'http://[v255.fe80::1]/'" do
+ Addressable::URI.parse("http://[v255.fe80::1]/")
+ end
+
+ it "should raise an error for " +
+ "'http://[v0.<invalid>]/'" do
+ (lambda do
+ Addressable::URI.parse("http://[v0.<invalid>]/")
+ end).should raise_error(Addressable::URI::InvalidURIError)
+ end
+end
+
describe Addressable::URI, "when parsed from " +
"'http://example.com/'" do
before do

0 comments on commit 8fb39d6

Please sign in to comment.