1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.UrlMatchValidator = void 0;
|
---|
4 | var regex_lib_1 = require("../regex-lib");
|
---|
5 | /**
|
---|
6 | * @private
|
---|
7 | * @class Autolinker.matcher.UrlMatchValidator
|
---|
8 | * @singleton
|
---|
9 | *
|
---|
10 | * Used by Autolinker to filter out false URL positives from the
|
---|
11 | * {@link Autolinker.matcher.Url UrlMatcher}.
|
---|
12 | *
|
---|
13 | * Due to the limitations of regular expressions (including the missing feature
|
---|
14 | * of look-behinds in JS regular expressions), we cannot always determine the
|
---|
15 | * validity of a given match. This class applies a bit of additional logic to
|
---|
16 | * filter out any false positives that have been matched by the
|
---|
17 | * {@link Autolinker.matcher.Url UrlMatcher}.
|
---|
18 | */
|
---|
19 | var UrlMatchValidator = /** @class */ (function () {
|
---|
20 | function UrlMatchValidator() {
|
---|
21 | }
|
---|
22 | /**
|
---|
23 | * Determines if a given URL match found by the {@link Autolinker.matcher.Url UrlMatcher}
|
---|
24 | * is valid. Will return `false` for:
|
---|
25 | *
|
---|
26 | * 1) URL matches which do not have at least have one period ('.') in the
|
---|
27 | * domain name (effectively skipping over matches like "abc:def").
|
---|
28 | * However, URL matches with a protocol will be allowed (ex: 'http://localhost')
|
---|
29 | * 2) URL matches which do not have at least one word character in the
|
---|
30 | * domain name (effectively skipping over matches like "git:1.0").
|
---|
31 | * However, URL matches with a protocol will be allowed (ex: 'intra-net://271219.76')
|
---|
32 | * 3) A protocol-relative url match (a URL beginning with '//') whose
|
---|
33 | * previous character is a word character (effectively skipping over
|
---|
34 | * strings like "abc//google.com")
|
---|
35 | *
|
---|
36 | * Otherwise, returns `true`.
|
---|
37 | *
|
---|
38 | * @param {String} urlMatch The matched URL, if there was one. Will be an
|
---|
39 | * empty string if the match is not a URL match.
|
---|
40 | * @param {String} protocolUrlMatch The match URL string for a protocol
|
---|
41 | * match. Ex: 'http://yahoo.com'. This is used to match something like
|
---|
42 | * 'http://localhost', where we won't double check that the domain name
|
---|
43 | * has at least one '.' in it.
|
---|
44 | * @return {Boolean} `true` if the match given is valid and should be
|
---|
45 | * processed, or `false` if the match is invalid and/or should just not be
|
---|
46 | * processed.
|
---|
47 | */
|
---|
48 | UrlMatchValidator.isValid = function (urlMatch, protocolUrlMatch) {
|
---|
49 | if ((protocolUrlMatch && !this.isValidUriScheme(protocolUrlMatch)) ||
|
---|
50 | this.urlMatchDoesNotHaveProtocolOrDot(urlMatch, protocolUrlMatch) || // At least one period ('.') must exist in the URL match for us to consider it an actual URL, *unless* it was a full protocol match (like 'http://localhost')
|
---|
51 | (this.urlMatchDoesNotHaveAtLeastOneWordChar(urlMatch, protocolUrlMatch) && // At least one letter character must exist in the domain name after a protocol match. Ex: skip over something like "git:1.0"
|
---|
52 | !this.isValidIpAddress(urlMatch)) || // Except if it's an IP address
|
---|
53 | this.containsMultipleDots(urlMatch)) {
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 | return true;
|
---|
57 | };
|
---|
58 | UrlMatchValidator.isValidIpAddress = function (uriSchemeMatch) {
|
---|
59 | var newRegex = new RegExp(this.hasFullProtocolRegex.source + this.ipRegex.source);
|
---|
60 | var uriScheme = uriSchemeMatch.match(newRegex);
|
---|
61 | return uriScheme !== null;
|
---|
62 | };
|
---|
63 | UrlMatchValidator.containsMultipleDots = function (urlMatch) {
|
---|
64 | var stringBeforeSlash = urlMatch;
|
---|
65 | if (this.hasFullProtocolRegex.test(urlMatch)) {
|
---|
66 | stringBeforeSlash = urlMatch.split('://')[1];
|
---|
67 | }
|
---|
68 | return stringBeforeSlash.split('/')[0].indexOf('..') > -1;
|
---|
69 | };
|
---|
70 | /**
|
---|
71 | * Determines if the URI scheme is a valid scheme to be autolinked. Returns
|
---|
72 | * `false` if the scheme is 'javascript:' or 'vbscript:'
|
---|
73 | *
|
---|
74 | * @private
|
---|
75 | * @param {String} uriSchemeMatch The match URL string for a full URI scheme
|
---|
76 | * match. Ex: 'http://yahoo.com' or 'mailto:a@a.com'.
|
---|
77 | * @return {Boolean} `true` if the scheme is a valid one, `false` otherwise.
|
---|
78 | */
|
---|
79 | UrlMatchValidator.isValidUriScheme = function (uriSchemeMatch) {
|
---|
80 | var uriSchemeMatchArr = uriSchemeMatch.match(this.uriSchemeRegex), uriScheme = uriSchemeMatchArr && uriSchemeMatchArr[0].toLowerCase();
|
---|
81 | return uriScheme !== 'javascript:' && uriScheme !== 'vbscript:';
|
---|
82 | };
|
---|
83 | /**
|
---|
84 | * Determines if a URL match does not have either:
|
---|
85 | *
|
---|
86 | * a) a full protocol (i.e. 'http://'), or
|
---|
87 | * b) at least one dot ('.') in the domain name (for a non-full-protocol
|
---|
88 | * match).
|
---|
89 | *
|
---|
90 | * Either situation is considered an invalid URL (ex: 'git:d' does not have
|
---|
91 | * either the '://' part, or at least one dot in the domain name. If the
|
---|
92 | * match was 'git:abc.com', we would consider this valid.)
|
---|
93 | *
|
---|
94 | * @private
|
---|
95 | * @param {String} urlMatch The matched URL, if there was one. Will be an
|
---|
96 | * empty string if the match is not a URL match.
|
---|
97 | * @param {String} protocolUrlMatch The match URL string for a protocol
|
---|
98 | * match. Ex: 'http://yahoo.com'. This is used to match something like
|
---|
99 | * 'http://localhost', where we won't double check that the domain name
|
---|
100 | * has at least one '.' in it.
|
---|
101 | * @return {Boolean} `true` if the URL match does not have a full protocol,
|
---|
102 | * or at least one dot ('.') in a non-full-protocol match.
|
---|
103 | */
|
---|
104 | UrlMatchValidator.urlMatchDoesNotHaveProtocolOrDot = function (urlMatch, protocolUrlMatch) {
|
---|
105 | return (!!urlMatch &&
|
---|
106 | (!protocolUrlMatch || !this.hasFullProtocolRegex.test(protocolUrlMatch)) &&
|
---|
107 | urlMatch.indexOf('.') === -1);
|
---|
108 | };
|
---|
109 | /**
|
---|
110 | * Determines if a URL match does not have either:
|
---|
111 | *
|
---|
112 | * a) a full protocol (i.e. 'http://'), or
|
---|
113 | * b) at least one word character after the protocol (i.e. in the domain name)
|
---|
114 | *
|
---|
115 | * At least one letter character must exist in the domain name after a
|
---|
116 | * protocol match. Ex: skip over something like "git:1.0"
|
---|
117 | *
|
---|
118 | * @private
|
---|
119 | * @param {String} urlMatch The matched URL, if there was one. Will be an
|
---|
120 | * empty string if the match is not a URL match.
|
---|
121 | * @param {String} protocolUrlMatch The match URL string for a protocol
|
---|
122 | * match. Ex: 'http://yahoo.com'. This is used to know whether or not we
|
---|
123 | * have a protocol in the URL string, in order to check for a word
|
---|
124 | * character after the protocol separator (':').
|
---|
125 | * @return {Boolean} `true` if the URL match does not have a full protocol, or
|
---|
126 | * at least one word character in it, `false` otherwise.
|
---|
127 | */
|
---|
128 | UrlMatchValidator.urlMatchDoesNotHaveAtLeastOneWordChar = function (urlMatch, protocolUrlMatch) {
|
---|
129 | if (urlMatch && protocolUrlMatch) {
|
---|
130 | return (!this.hasFullProtocolRegex.test(protocolUrlMatch) &&
|
---|
131 | !this.hasWordCharAfterProtocolRegex.test(urlMatch));
|
---|
132 | }
|
---|
133 | else {
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | };
|
---|
137 | /**
|
---|
138 | * Regex to test for a full protocol, with the two trailing slashes. Ex: 'http://'
|
---|
139 | *
|
---|
140 | * @private
|
---|
141 | * @property {RegExp} hasFullProtocolRegex
|
---|
142 | */
|
---|
143 | UrlMatchValidator.hasFullProtocolRegex = /^[A-Za-z][-.+A-Za-z0-9]*:\/\//;
|
---|
144 | /**
|
---|
145 | * Regex to find the URI scheme, such as 'mailto:'.
|
---|
146 | *
|
---|
147 | * This is used to filter out 'javascript:' and 'vbscript:' schemes.
|
---|
148 | *
|
---|
149 | * @private
|
---|
150 | * @property {RegExp} uriSchemeRegex
|
---|
151 | */
|
---|
152 | UrlMatchValidator.uriSchemeRegex = /^[A-Za-z][-.+A-Za-z0-9]*:/;
|
---|
153 | /**
|
---|
154 | * Regex to determine if at least one word char exists after the protocol (i.e. after the ':')
|
---|
155 | *
|
---|
156 | * @private
|
---|
157 | * @property {RegExp} hasWordCharAfterProtocolRegex
|
---|
158 | */
|
---|
159 | UrlMatchValidator.hasWordCharAfterProtocolRegex = new RegExp(':[^\\s]*?[' + regex_lib_1.alphaCharsStr + ']');
|
---|
160 | /**
|
---|
161 | * Regex to determine if the string is a valid IP address
|
---|
162 | *
|
---|
163 | * @private
|
---|
164 | * @property {RegExp} ipRegex
|
---|
165 | */
|
---|
166 | UrlMatchValidator.ipRegex = /[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?(:[0-9]*)?\/?$/;
|
---|
167 | return UrlMatchValidator;
|
---|
168 | }());
|
---|
169 | exports.UrlMatchValidator = UrlMatchValidator;
|
---|
170 | //# sourceMappingURL=url-match-validator.js.map |
---|