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