|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
18 | | -import url = require('url'); |
19 | | - |
20 | 18 | /** |
21 | 19 | * Validates that a value is a byte buffer. |
22 | 20 | * |
@@ -234,33 +232,38 @@ export function isURL(urlStr: any): boolean { |
234 | 232 | return false; |
235 | 233 | } |
236 | 234 | try { |
237 | | - const uri = url.parse(urlStr); |
| 235 | + const uri = new URL(urlStr); |
238 | 236 | const scheme = uri.protocol; |
239 | | - const slashes = uri.slashes; |
240 | | - const hostname = uri.hostname; |
241 | | - const pathname = uri.pathname; |
242 | | - if ((scheme !== 'http:' && scheme !== 'https:') || !slashes) { |
| 237 | + if (scheme !== 'http:' && scheme !== 'https:') { |
243 | 238 | return false; |
244 | 239 | } |
245 | | - // Validate hostname: Can contain letters, numbers, underscore and dashes separated by a dot. |
246 | | - // Each zone must not start with a hyphen or underscore. |
247 | | - if (!hostname || !/^[a-zA-Z0-9]+[\w-]*([.]?[a-zA-Z0-9]+[\w-]*)*$/.test(hostname)) { |
248 | | - return false; |
| 240 | + const hostname = uri.hostname; |
| 241 | + // Validate hostname strictly to match previous behavior and prevent weak/invalid domains. |
| 242 | + // Must be alphanumeric with optional dashes/underscores, separated by dots. |
| 243 | + // Cannot start/end with dot or dash (mostly). |
| 244 | + // This regex is safe (no nested quantifiers with overlap). |
| 245 | + if (!/^[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]*[a-zA-Z0-9])?)*$/.test(hostname)) { |
| 246 | + // Check for IPv6 literals which are valid but behave differently. |
| 247 | + // Node 'new URL' keeps brackets for IPv6: [::1] -> [::1] |
| 248 | + // Check for IPv6 address (simple check for brackets) |
| 249 | + if (!/^\[[a-fA-F0-9:.]+\]$/.test(hostname)) { |
| 250 | + return false; |
| 251 | + } |
249 | 252 | } |
250 | | - // Allow for pathnames: (/chars+)*/? |
| 253 | + // Restore strict pathname validation: (/chars+)*/? |
251 | 254 | // Where chars can be a combination of: a-z A-Z 0-9 - _ . ~ ! $ & ' ( ) * + , ; = : @ % |
252 | 255 | const pathnameRe = /^(\/[\w\-.~!$'()*+,;=:@%]+)*\/?$/; |
253 | 256 | // Validate pathname. |
| 257 | + const pathname = uri.pathname; |
254 | 258 | if (pathname && |
255 | | - pathname !== '/' && |
256 | | - !pathnameRe.test(pathname)) { |
| 259 | + pathname !== '/' && |
| 260 | + !pathnameRe.test(pathname)) { |
257 | 261 | return false; |
258 | 262 | } |
259 | | - // Allow any query string and hash as long as no invalid character is used. |
| 263 | + return true; |
260 | 264 | } catch (e) { |
261 | 265 | return false; |
262 | 266 | } |
263 | | - return true; |
264 | 267 | } |
265 | 268 |
|
266 | 269 |
|
|
0 commit comments