Skip to content

Commit 16caa29

Browse files
committed
fix(ngInput): change URL_REGEXP to better match RFC3987
The URL_REGEXP in use to perform validation in ngInput is too restrictive and fails to follow RFC3987. In particular, it only accepts ftp, http, and https scheme components and rejects perfectly valid schemes such as "file", "mailto", "chrome-extension", etc. The regex also requires the scheme to be followed by two "/" but the RFC says 0 to n are acceptable. This change fixes both of these issues to better align to the standard. Closes angular#11341
1 parent bea99e3 commit 16caa29

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/ng/directive/input.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
// Regex code is obtained from SO: https://stackoverflow.com/questions/3143070/javascript-regex-iso-datetime#answer-3143231
1313
var ISO_DATE_REGEXP = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/;
14-
var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
14+
// See valid URLs in RFC3987 (http://tools.ietf.org/html/rfc3987)
15+
var URL_REGEXP = /^([0-9]+|\w+|\.|\+|\-)+:\/?\/?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
1516
var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i;
1617
var NUMBER_REGEXP = /^\s*(\-|\+)?(\d+|(\d*(\.\d*)))\s*$/;
1718
var DATE_REGEXP = /^(\d{4})-(\d{2})-(\d{2})$/;
@@ -1286,7 +1287,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
12861287
}
12871288
}
12881289

1289-
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser) {
1290+
function urlInputType(scope, element, attr, ctrl, $sniffer, $browser, $$$sanitizeUriProvider) {
12901291
// Note: no badInputChecker here by purpose as `url` is only a validation
12911292
// in browsers, i.e. we can always read out input.value even if it is not valid!
12921293
baseInputType(scope, element, attr, ctrl, $sniffer, $browser);

test/ng/directive/inputSpec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,8 +2387,16 @@ describe('input', function() {
23872387
describe('URL_REGEXP', function() {
23882388
/* global URL_REGEXP: false */
23892389
it('should validate url', function() {
2390+
// See valid URLs in RFC3987 (http://tools.ietf.org/html/rfc3987)
23902391
expect(URL_REGEXP.test('http://server:123/path')).toBe(true);
2392+
expect(URL_REGEXP.test('https://server:123/path')).toBe(true);
2393+
expect(URL_REGEXP.test('file:///home/user')).toBe(true);
2394+
expect(URL_REGEXP.test('mailto:[email protected]?subject=Foo')).toBe(true);
2395+
expect(URL_REGEXP.test('r2-d2.c3-p0://localhost/foo')).toBe(true);
2396+
expect(URL_REGEXP.test('abc:/foo')).toBe(true);
2397+
expect(URL_REGEXP.test('http:')).toBe(false);
23912398
expect(URL_REGEXP.test('[email protected]')).toBe(false);
2399+
expect(URL_REGEXP.test('a_B.c')).toBe(false);
23922400
});
23932401
});
23942402
});

0 commit comments

Comments
 (0)