Skip to content

Commit 199a66a

Browse files
committed
feat: use new URL instead of url.parse
1 parent 18704b6 commit 199a66a

1 file changed

Lines changed: 25 additions & 21 deletions

File tree

lib/Server.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,27 @@ class Server {
30503050
return false;
30513051
}
30523052

3053+
/**
3054+
* Extracts and normalizes the hostname from a header, removing brackets for IPv6.
3055+
* @param {string} header header value
3056+
* @returns {string|null} hostname or null
3057+
*/
3058+
#parseHostnameFromHeader = function (header) {
3059+
if (!header) return null;
3060+
try {
3061+
// If the header does not have a scheme, prepend // so URL can parse it
3062+
const url = new URL(/^(.+:)?\/\//.test(header) ? header : `//${header}`);
3063+
let hostname = url.hostname;
3064+
// Normalize IPv6: remove brackets if present
3065+
if (hostname.startsWith("[") && hostname.endsWith("]")) {
3066+
hostname = hostname.slice(1, -1);
3067+
}
3068+
return hostname;
3069+
} catch {
3070+
return null;
3071+
}
3072+
};
3073+
30533074
/**
30543075
* @private
30553076
* @param {{ [key: string]: string | undefined }} headers headers
@@ -3074,15 +3095,7 @@ class Server {
30743095
return true;
30753096
}
30763097

3077-
// use the node url-parser to retrieve the hostname from the host-header.
3078-
// TODO resolve me in the next major release
3079-
// eslint-disable-next-line n/no-deprecated-api
3080-
const { hostname } = url.parse(
3081-
// if header doesn't have scheme, add // for parsing.
3082-
/^(.+:)?\/\//.test(header) ? header : `//${header}`,
3083-
false,
3084-
true,
3085-
);
3098+
const hostname = this.#parseHostnameFromHeader(header);
30863099

30873100
if (hostname === null) {
30883101
return false;
@@ -3096,8 +3109,7 @@ class Server {
30963109
// A note on IPv6 addresses:
30973110
// header will always contain the brackets denoting
30983111
// an IPv6-address in URLs,
3099-
// these are removed from the hostname in url.parse(),
3100-
// so we have the pure IPv6-address in hostname.
3112+
// these aren't removed from the hostname in new URL(),
31013113
// For convenience, always allow localhost (hostname === 'localhost')
31023114
// and its subdomains (hostname.endsWith(".localhost")).
31033115
// allow hostname of listening address (hostname === this.options.host)
@@ -3132,9 +3144,7 @@ class Server {
31323144
return true;
31333145
}
31343146

3135-
// TODO resolve me in the next major release
3136-
// eslint-disable-next-line n/no-deprecated-api
3137-
const origin = url.parse(originHeader, false, true).hostname;
3147+
const origin = this.#parseHostnameFromHeader(originHeader);
31383148

31393149
if (origin === null) {
31403150
return false;
@@ -3154,13 +3164,7 @@ class Server {
31543164
return true;
31553165
}
31563166

3157-
// eslint-disable-next-line n/no-deprecated-api
3158-
const host = url.parse(
3159-
// if hostHeader doesn't have scheme, add // for parsing.
3160-
/^(.+:)?\/\//.test(hostHeader) ? hostHeader : `//${hostHeader}`,
3161-
false,
3162-
true,
3163-
).hostname;
3167+
const host = this.#parseHostnameFromHeader(hostHeader);
31643168

31653169
if (host === null) {
31663170
return false;

0 commit comments

Comments
 (0)