Skip to content

Commit

Permalink
fix allowing _ as hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Aug 9, 2017
1 parent a62b4ea commit 9b05d56
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
URI.escapeQuerySpace = true;
// static properties
URI.protocol_expression = /^[a-z][a-z0-9.+-]*$/i;
URI.idn_expression = /[^a-z0-9\.-]/i;
URI.idn_expression = /[^a-z0-9\._-]/i;
URI.punycode_expression = /(xn--)/i;
// well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
Expand Down
28 changes: 23 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname !== undefined, 'host undefined');
});
test('function URI(string) with complex hostname', function() {
var u = new URI('http://some.complex_host-name123.org/');
ok(u instanceof URI, 'instanceof URI');
ok(u._parts.hostname !== undefined, 'host undefined');
});
test('function URI(string) with invalid port "port" throws', function () {
raises(function () {
new URI('http://example.org:port');
Expand Down Expand Up @@ -248,13 +243,18 @@
equal(u.hostname(), 'abc.foobar.lala', 'hostname changed');
equal(u+'', 'http://abc.foobar.lala/foo.html', 'hostname changed url');

u.hostname('some_where.exa_mple.org');
equal(u.hostname(), 'some_where.exa_mple.org', 'hostname changed');
equal(u+'', 'http://some_where.exa_mple.org/foo.html', 'hostname changed url');

raises(function() {
u.hostname('foo\\bar.com');
}, TypeError, 'Failing backslash detection in hostname');

raises(function() {
u.hostname('');
}, TypeError, "Trying to set an empty hostname with http(s) protocol throws a TypeError");

raises(function() {
u.hostname(null);
}, TypeError, "Trying to set hostname to null with http(s) protocol throws a TypeError");
Expand Down Expand Up @@ -400,6 +400,11 @@
equal(u.port(), '', 'host removed port');
equal(u+'', 'http://some-domain.com/foo.html', 'host modified url');

u.host('some_where.exa_mple.org:44');
equal(u.hostname(), 'some_where.exa_mple.org', 'host modified hostname #2');
equal(u.port(), '44', 'port restored');
equal(u+'', 'http://some_where.exa_mple.org:44/foo.html', 'host modified url #2');

raises(function() {
u.host('foo\\bar.com');
}, TypeError, 'Failing backslash detection in host');
Expand Down Expand Up @@ -529,6 +534,11 @@
equal(u.hostname(), 'foo.example.org', 'changed subdomain foo.');
equal(u+'', 'http://foo.example.org/foo.html', 'changed url foo.');


u.subdomain('foo_bar');
equal(u.hostname(), 'foo_bar.example.org', 'changed subdomain foo_bar');
equal(u+'', 'http://foo_bar.example.org/foo.html', 'changed url foo_bar');

});
test('domain', function() {
var u = new URI('http://www.example.org/foo.html');
Expand Down Expand Up @@ -559,6 +569,14 @@

u.subdomain('foo');
equal(u.href(), 'http://foo.test/', 'subdomain set on (dot-less)');

u.subdomain('bar');
equal(u.href(), 'http://bar.foo.test/', 'subdomain set on foo.test');

u.domain('exam_ple.org');
equal(u.domain(), 'exam_ple.org', 'domain after changed domain exam_ple.org');
equal(u+'', 'http://bar.exam_ple.org/', 'url after changed domain exam_ple.org');

});
test('tld', function() {
var u = new URI('http://www.example.org/foo.html');
Expand Down
50 changes: 49 additions & 1 deletion test/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,55 @@ var urls = [{
idn: false,
punycode: false
}
}, {
// https://github.com/medialize/URI.js/issues/347
name: 'Underscore in domain',
url: 'http://user:pass@some_where.exa_mple.org:123/some/directory/file.html?query=string#fragment',
parts: {
protocol: 'http',
username: 'user',
password: 'pass',
hostname: 'some_where.exa_mple.org',
port: '123',
path: '/some/directory/file.html',
query: 'query=string',
fragment: 'fragment'
},
accessors: {
protocol: 'http',
username: 'user',
password: 'pass',
port: '123',
path: '/some/directory/file.html',
query: 'query=string',
fragment: 'fragment',
resource: '/some/directory/file.html?query=string#fragment',
authority: 'user:pass@some_where.exa_mple.org:123',
origin: 'http://user:pass@some_where.exa_mple.org:123',
userinfo: 'user:pass',
subdomain: 'some_where',
domain: 'exa_mple.org',
tld: 'org',
directory: '/some/directory',
filename: 'file.html',
suffix: 'html',
hash: '#fragment',
search: '?query=string',
host: 'some_where.exa_mple.org:123',
hostname: 'some_where.exa_mple.org'
},
is: {
urn: false,
url: true,
relative: false,
name: true,
sld: false,
ip: false,
ip4: false,
ip6: false,
idn: false,
punycode: false
}
}, {
name: 'IDN (punycode)',
url: 'http://user:pass@xn--exmple-cua.org:123/some/directory/file.html?query=string#fragment',
Expand Down Expand Up @@ -1889,4 +1938,3 @@ var urls = [{
}
}
];

0 comments on commit 9b05d56

Please sign in to comment.