Skip to content

Commit 8223c4d

Browse files
Merge branch 'main' into dependabot/npm_and_yarn/lodash-4.17.23
2 parents 221756a + 1bf436d commit 8223c4d

1 file changed

Lines changed: 19 additions & 16 deletions

File tree

src/utils/validator.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import url = require('url');
19-
2018
/**
2119
* Validates that a value is a byte buffer.
2220
*
@@ -234,33 +232,38 @@ export function isURL(urlStr: any): boolean {
234232
return false;
235233
}
236234
try {
237-
const uri = url.parse(urlStr);
235+
const uri = new URL(urlStr);
238236
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:') {
243238
return false;
244239
}
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+
}
249252
}
250-
// Allow for pathnames: (/chars+)*/?
253+
// Restore strict pathname validation: (/chars+)*/?
251254
// Where chars can be a combination of: a-z A-Z 0-9 - _ . ~ ! $ & ' ( ) * + , ; = : @ %
252255
const pathnameRe = /^(\/[\w\-.~!$'()*+,;=:@%]+)*\/?$/;
253256
// Validate pathname.
257+
const pathname = uri.pathname;
254258
if (pathname &&
255-
pathname !== '/' &&
256-
!pathnameRe.test(pathname)) {
259+
pathname !== '/' &&
260+
!pathnameRe.test(pathname)) {
257261
return false;
258262
}
259-
// Allow any query string and hash as long as no invalid character is used.
263+
return true;
260264
} catch (e) {
261265
return false;
262266
}
263-
return true;
264267
}
265268

266269

0 commit comments

Comments
 (0)