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
13 changes: 8 additions & 5 deletions packages/angular/ssr/node/src/app-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AngularAppEngine } from '@angular/ssr';
import type { IncomingMessage } from 'node:http';
import type { Http2ServerRequest } from 'node:http2';
import { AngularAppEngineOptions } from '../../src/app-engine';
import { getAllowedHostsFromEnv } from './environment-options';
import { getAllowedHostsFromEnv, getTrustProxyHeadersFromEnv } from './environment-options';
import { createWebRequestFromNodeRequest } from './request';

/**
Expand All @@ -37,11 +37,14 @@ export class AngularNodeAppEngine {
* @param options Options for the Angular Node.js server application engine.
*/
constructor(options?: AngularNodeAppEngineOptions) {
this.angularAppEngine = new AngularAppEngine({
const appEngineOptions: AngularAppEngineOptions = {
...options,
allowedHosts: [...getAllowedHostsFromEnv(), ...(options?.allowedHosts ?? [])],
});
this.trustProxyHeaders = options?.trustProxyHeaders;
allowedHosts: getAllowedHostsFromEnv() ?? options?.allowedHosts,
trustProxyHeaders: getTrustProxyHeadersFromEnv() ?? options?.trustProxyHeaders,
};
Comment thread
dgp1130 marked this conversation as resolved.

this.angularAppEngine = new AngularAppEngine(appEngineOptions);
this.trustProxyHeaders = appEngineOptions.trustProxyHeaders;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions packages/angular/ssr/node/src/common-engine/common-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ export class CommonEngine {
private readonly allowedHosts: ReadonlySet<string>;

constructor(private options?: CommonEngineOptions) {
this.allowedHosts = new Set([
...getAllowedHostsFromEnv(),
...(this.options?.allowedHosts ?? []),
]);
this.allowedHosts = new Set(getAllowedHostsFromEnv() ?? this.options?.allowedHosts ?? []);
Comment thread
dgp1130 marked this conversation as resolved.
}

/**
Expand Down
31 changes: 21 additions & 10 deletions packages/angular/ssr/node/src/environment-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@
* Retrieves the list of allowed hosts from the environment variable `NG_ALLOWED_HOSTS`.
* @returns An array of allowed hosts.
*/
export function getAllowedHostsFromEnv(): ReadonlyArray<string> {
const allowedHosts: string[] = [];
const envNgAllowedHosts = process.env['NG_ALLOWED_HOSTS'];
if (!envNgAllowedHosts) {
return allowedHosts;
export function getAllowedHostsFromEnv(): ReadonlyArray<string> | undefined {
return getArrayFromEnv('NG_ALLOWED_HOSTS');
}

/**
* Retrieves the list of trusted proxy headers from the environment variable `NG_TRUST_PROXY_HEADERS`.
* @returns An array of trusted proxy headers.
*/
export function getTrustProxyHeadersFromEnv(): ReadonlyArray<string> | undefined {
return getArrayFromEnv('NG_TRUST_PROXY_HEADERS');
}
Comment thread
dgp1130 marked this conversation as resolved.

function getArrayFromEnv(envName: string): ReadonlyArray<string> | undefined {
const envValue = process.env[envName];
if (!envValue) {
return undefined;
}

const hosts = envNgAllowedHosts.split(',');
for (const host of hosts) {
const trimmed = host.trim();
const values: string[] = [];
for (const value of envValue.split(',')) {
const trimmed = value.trim();
if (trimmed.length > 0) {
allowedHosts.push(trimmed);
values.push(trimmed);
}
}

return allowedHosts;
return values;
}
Loading