Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export function createAngularSsrInternalMiddleware(
): Connect.NextHandleFunction {
let cachedAngularServerApp: ReturnType<typeof getOrCreateAngularServerApp> | undefined;

const { allowedHosts } = server.config.server;
const disableAllowedHostsCheck = allowedHosts === true;

return function angularSsrMiddleware(
req: Connect.IncomingMessage,
res: ServerResponse,
Expand Down Expand Up @@ -62,6 +65,20 @@ export function createAngularSsrInternalMiddleware(
const webReq = new Request(createWebRequestFromNodeRequest(req), {
signal: AbortSignal.timeout(30_000),
});

if (!disableAllowedHostsCheck && Array.isArray(allowedHosts) && allowedHosts.length > 0) {
const { hostname } = new URL(webReq.url);
const isAllowed = allowedHosts.some(
(host) => hostname === host || hostname.endsWith(`.${host}`),
);
Comment on lines +71 to +73

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The current host matching logic has two issues:

  1. If an allowed host starts with a dot (e.g., .example.com to allow subdomains), hostname.endsWith(.${host}) evaluates to hostname.endsWith('..example.com'), which will fail to match subdomains.
  2. If an allowed host does not start with a dot (e.g., example.com), hostname.endsWith(.${host}) will match subdomains (e.g., sub.example.com), which is over-permissive and violates standard host matching behavior where subdomains are only allowed if a leading dot is explicitly specified.

We should update the matching logic to correctly handle leading dots, consistent with Vite and webpack-dev-server standards.

        const isAllowed = allowedHosts.some((host) => {
          if (host.startsWith('.')) {
            return hostname === host.slice(1) || hostname.endsWith(host);
          }
          return hostname === host;
        });

if (!isAllowed) {
res.statusCode = 400;
res.end('Bad Request: Host not allowed.');

return;
}
}

const webRes = await angularServerApp.handle(webReq);
if (!webRes) {
return next();
Expand Down
Loading