Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3050,6 +3050,32 @@ class Server {
return false;
}

/**
* Extracts and normalizes the hostname from a header, removing brackets for IPv6.
* @param {string} header header value
* @returns {string|null} hostname or null
*/
#parseHostnameFromHeader = function (header) {
if (!header) return null;
try {
// If the header does not have a scheme, prepend // so URL can parse it
const parseUrl = new URL(
/^(.+:)?\/\//.test(header) ? header : `//${header}`,
"http://localhost/",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only for a fallback.

);

let hostname = parseUrl.hostname;
// Normalize IPv6: remove brackets if present
if (hostname.startsWith("[") && hostname.endsWith("]")) {
hostname = hostname.slice(1, -1);
}

return hostname;
} catch {
return null;
}
};

/**
* @private
* @param {{ [key: string]: string | undefined }} headers headers
Expand All @@ -3074,15 +3100,7 @@ class Server {
return true;
}

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

if (hostname === null) {
return false;
Expand All @@ -3096,8 +3114,7 @@ class Server {
// A note on IPv6 addresses:
// header will always contain the brackets denoting
// an IPv6-address in URLs,
// these are removed from the hostname in url.parse(),
// so we have the pure IPv6-address in hostname.
// these aren't removed from the hostname in new URL(),
// For convenience, always allow localhost (hostname === 'localhost')
// and its subdomains (hostname.endsWith(".localhost")).
// allow hostname of listening address (hostname === this.options.host)
Expand Down Expand Up @@ -3132,9 +3149,7 @@ class Server {
return true;
}

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

if (origin === null) {
return false;
Expand All @@ -3154,13 +3169,7 @@ class Server {
return true;
}

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

if (host === null) {
return false;
Expand Down
1 change: 1 addition & 0 deletions types/lib/Server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,7 @@ declare class Server<
* @param {((err?: Error) => void)=} callback callback
*/
stopCallback(callback?: ((err?: Error) => void) | undefined): void;
#private;
}
declare namespace Server {
export {
Expand Down