|
| 1 | +import assertString from './util/assertString'; |
| 2 | +import isIP from './isIP'; |
| 3 | + |
| 4 | +const MULTIPLE_SPACES_REGEX = / {2,}/g; |
| 5 | +const INVALID_PERCENT_REGEX = /%(?![0-9A-Fa-f]{2})/; |
| 6 | +const SCHEME_REGEX = /^[A-Za-z][A-Za-z0-9+.-]*$/; |
| 7 | +const BACKSLASH_REGEX = /\\/; |
| 8 | +const DISALLOWED_ASCII_REGEX = /["<>^`{}|]/; |
| 9 | +const OPEN_BRACKET_PLACEHOLDER = '__VALIDATOR_OPEN_BRACKET__'; |
| 10 | +const CLOSE_BRACKET_PLACEHOLDER = '__VALIDATOR_CLOSE_BRACKET__'; |
| 11 | + |
| 12 | +const HEX_DIGIT = '[0-9A-Fa-f]'; |
| 13 | +const PCT_ENCODED = `%${HEX_DIGIT}{2}`; |
| 14 | +const UNRESERVED = 'A-Za-z0-9\\-._~'; |
| 15 | +const SUB_DELIMS = "!$&'()*+,;="; |
| 16 | +const PCHAR = `(?:[${UNRESERVED}]|${PCT_ENCODED}|[${SUB_DELIMS}:@])`; |
| 17 | +const SEGMENT = `(?:${PCHAR})*`; |
| 18 | +const SEGMENT_NZ = `(?:${PCHAR})+`; |
| 19 | +const SEGMENT_NZ_NC = `(?:${PCT_ENCODED}|[${UNRESERVED}${SUB_DELIMS}@])+`; |
| 20 | + |
| 21 | +const PATH_ABEMPTY_REGEX = new RegExp(`^(?:/${SEGMENT})*$`); |
| 22 | +const PATH_ABSOLUTE_REGEX = new RegExp(`^/(?:${SEGMENT_NZ}(?:/${SEGMENT})*)?$`); |
| 23 | +const PATH_ROOTLESS_REGEX = new RegExp(`^${SEGMENT_NZ}(?:/${SEGMENT})*$`); |
| 24 | +const PATH_NOSCHEME_REGEX = new RegExp(`^(?:${SEGMENT_NZ_NC})(?:/${SEGMENT})*$`); |
| 25 | +const QUERY_FRAGMENT_REGEX = new RegExp(`^(?:${PCHAR}|[/?])*$`); |
| 26 | +const USERINFO_REGEX = new RegExp(`^(?:${PCT_ENCODED}|[${UNRESERVED}${SUB_DELIMS}:])*$`); |
| 27 | +const REG_NAME_REGEX = new RegExp(`^(?:${PCT_ENCODED}|[${UNRESERVED}${SUB_DELIMS}])*$`); |
| 28 | +const IPV_FUTURE_REGEX = /^v[0-9A-F]+\.[A-Za-z0-9._~!$&'()*+,;=:-]+$/i; |
| 29 | + |
| 30 | +function collapseXmlWhitespace(input) { |
| 31 | + let normalized = ''; |
| 32 | + |
| 33 | + for (let i = 0; i < input.length; i += 1) { |
| 34 | + const code = input.charCodeAt(i); |
| 35 | + |
| 36 | + if (code === 0x09 || code === 0x0a || code === 0x0d) { |
| 37 | + normalized += ' '; |
| 38 | + } else { |
| 39 | + normalized += input[i]; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return normalized.replace(MULTIPLE_SPACES_REGEX, ' ').trim(); |
| 44 | +} |
| 45 | + |
| 46 | +function containsForbiddenControl(value) { |
| 47 | + for (let i = 0; i < value.length; i += 1) { |
| 48 | + const code = value.charCodeAt(i); |
| 49 | + |
| 50 | + if ( |
| 51 | + (code >= 0x00 && code <= 0x08) || |
| 52 | + code === 0x0b || |
| 53 | + code === 0x0c || |
| 54 | + (code >= 0x0e && code <= 0x1f) || |
| 55 | + code === 0x7f |
| 56 | + ) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +function hasInvalidPercentEncoding(input) { |
| 65 | + return INVALID_PERCENT_REGEX.test(input); |
| 66 | +} |
| 67 | + |
| 68 | +function isIPvFuture(address) { |
| 69 | + return IPV_FUTURE_REGEX.test(address); |
| 70 | +} |
| 71 | + |
| 72 | +function isValidAuthority(authority, options) { |
| 73 | + const allowEmptyAuthority = Boolean(options && options.allowEmptyAuthority); |
| 74 | + if (authority === '') { |
| 75 | + return !!allowEmptyAuthority; |
| 76 | + } |
| 77 | + |
| 78 | + let hostPort = authority; |
| 79 | + let userinfo = ''; |
| 80 | + const atIndex = authority.lastIndexOf('@'); |
| 81 | + |
| 82 | + if (atIndex !== -1) { |
| 83 | + userinfo = authority.slice(0, atIndex); |
| 84 | + hostPort = authority.slice(atIndex + 1); |
| 85 | + |
| 86 | + if (!USERINFO_REGEX.test(userinfo)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + let host = hostPort; |
| 92 | + let port = null; |
| 93 | + let hasHost = false; |
| 94 | + |
| 95 | + if (hostPort.startsWith('[')) { |
| 96 | + const closingIndex = hostPort.indexOf(']'); |
| 97 | + |
| 98 | + if (closingIndex === -1) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + const address = hostPort.slice(1, closingIndex); |
| 103 | + |
| 104 | + if (!isIP(address, 6) && !isIPvFuture(address)) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + const remainder = hostPort.slice(closingIndex + 1); |
| 109 | + |
| 110 | + if (remainder) { |
| 111 | + if (!remainder.startsWith(':')) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + port = remainder.slice(1); |
| 116 | + } |
| 117 | + |
| 118 | + host = ''; |
| 119 | + hasHost = true; |
| 120 | + } else { |
| 121 | + const firstColon = hostPort.indexOf(':'); |
| 122 | + const lastColon = hostPort.lastIndexOf(':'); |
| 123 | + |
| 124 | + if (firstColon !== lastColon) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + if (lastColon !== -1) { |
| 129 | + host = hostPort.slice(0, lastColon); |
| 130 | + port = hostPort.slice(lastColon + 1); |
| 131 | + } |
| 132 | + |
| 133 | + if (host) { |
| 134 | + hasHost = true; |
| 135 | + |
| 136 | + if (!isIP(host, 4) && !REG_NAME_REGEX.test(host)) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (!hasHost) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + if (port !== null) { |
| 147 | + if (port === '' || !/^[0-9]+$/.test(port)) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + const portNumber = parseInt(port, 10); |
| 152 | + |
| 153 | + if (Number.isNaN(portNumber) || portNumber > 65535) { |
| 154 | + return false; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return true; |
| 159 | +} |
| 160 | + |
| 161 | +function isValidPath(path, { hasAuthority, hasScheme }) { |
| 162 | + if (hasAuthority) { |
| 163 | + return PATH_ABEMPTY_REGEX.test(path); |
| 164 | + } |
| 165 | + |
| 166 | + if (hasScheme) { |
| 167 | + if (path === '') { |
| 168 | + return true; |
| 169 | + } |
| 170 | + |
| 171 | + if (path.startsWith('/')) { |
| 172 | + return PATH_ABSOLUTE_REGEX.test(path); |
| 173 | + } |
| 174 | + |
| 175 | + return PATH_ROOTLESS_REGEX.test(path); |
| 176 | + } |
| 177 | + |
| 178 | + if (path === '') { |
| 179 | + return true; |
| 180 | + } |
| 181 | + |
| 182 | + if (path.startsWith('/')) { |
| 183 | + return PATH_ABSOLUTE_REGEX.test(path); |
| 184 | + } |
| 185 | + |
| 186 | + return PATH_NOSCHEME_REGEX.test(path); |
| 187 | +} |
| 188 | + |
| 189 | +function isValidQueryOrFragment(value) { |
| 190 | + return value === '' || QUERY_FRAGMENT_REGEX.test(value); |
| 191 | +} |
| 192 | + |
| 193 | +function isValidUriReference(value) { |
| 194 | + let rest = value; |
| 195 | + let scheme = null; |
| 196 | + let hadScheme = false; |
| 197 | + |
| 198 | + const colonIndex = rest.indexOf(':'); |
| 199 | + |
| 200 | + if (colonIndex > 0) { |
| 201 | + const potentialScheme = rest.slice(0, colonIndex); |
| 202 | + |
| 203 | + if (SCHEME_REGEX.test(potentialScheme)) { |
| 204 | + scheme = potentialScheme; |
| 205 | + hadScheme = true; |
| 206 | + rest = rest.slice(colonIndex + 1); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + let fragment = ''; |
| 211 | + const hashIndex = rest.indexOf('#'); |
| 212 | + |
| 213 | + if (hashIndex !== -1) { |
| 214 | + fragment = rest.slice(hashIndex + 1); |
| 215 | + rest = rest.slice(0, hashIndex); |
| 216 | + |
| 217 | + if (!isValidQueryOrFragment(fragment)) { |
| 218 | + return false; |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + let query = ''; |
| 223 | + const questionIndex = rest.indexOf('?'); |
| 224 | + |
| 225 | + if (questionIndex !== -1) { |
| 226 | + query = rest.slice(questionIndex + 1); |
| 227 | + rest = rest.slice(0, questionIndex); |
| 228 | + |
| 229 | + if (!isValidQueryOrFragment(query)) { |
| 230 | + return false; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + let hasAuthority = false; |
| 235 | + let authority = ''; |
| 236 | + let path = rest; |
| 237 | + |
| 238 | + if (rest.startsWith('//')) { |
| 239 | + hasAuthority = true; |
| 240 | + rest = rest.slice(2); |
| 241 | + const nextSlash = rest.indexOf('/'); |
| 242 | + |
| 243 | + if (nextSlash === -1) { |
| 244 | + authority = rest; |
| 245 | + path = ''; |
| 246 | + } else { |
| 247 | + authority = rest.slice(0, nextSlash); |
| 248 | + path = rest.slice(nextSlash); |
| 249 | + } |
| 250 | + |
| 251 | + const allowEmptyAuthority = Boolean(hadScheme && scheme && scheme.toLowerCase() === 'file'); |
| 252 | + const authorityOptions = allowEmptyAuthority |
| 253 | + ? { allowEmptyAuthority: true } |
| 254 | + : undefined; |
| 255 | + |
| 256 | + if (!isValidAuthority(authority, authorityOptions)) { |
| 257 | + return false; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + return isValidPath(path, { hasAuthority, hasScheme: hadScheme }); |
| 262 | +} |
| 263 | + |
| 264 | +export default function isXsdAnyURI(input) { |
| 265 | + assertString(input); |
| 266 | + |
| 267 | + let value = collapseXmlWhitespace(input); |
| 268 | + |
| 269 | + if (value === '') { |
| 270 | + return true; |
| 271 | + } |
| 272 | + |
| 273 | + if ( |
| 274 | + containsForbiddenControl(value) || |
| 275 | + hasInvalidPercentEncoding(value) || |
| 276 | + BACKSLASH_REGEX.test(value) || |
| 277 | + DISALLOWED_ASCII_REGEX.test(value) |
| 278 | + ) { |
| 279 | + return false; |
| 280 | + } |
| 281 | + |
| 282 | + let encoded; |
| 283 | + |
| 284 | + try { |
| 285 | + const bracketSafeValue = value |
| 286 | + .replace(/\[/g, OPEN_BRACKET_PLACEHOLDER) |
| 287 | + .replace(/\]/g, CLOSE_BRACKET_PLACEHOLDER); |
| 288 | + |
| 289 | + const encodedWithPlaceholders = encodeURI(bracketSafeValue); |
| 290 | + |
| 291 | + encoded = encodedWithPlaceholders |
| 292 | + .split(OPEN_BRACKET_PLACEHOLDER) |
| 293 | + .join('[') |
| 294 | + .split(CLOSE_BRACKET_PLACEHOLDER) |
| 295 | + .join(']'); |
| 296 | + } catch (err) { |
| 297 | + return false; |
| 298 | + } |
| 299 | + |
| 300 | + return isValidUriReference(encoded); |
| 301 | +} |
0 commit comments