Skip to content

Commit d7fb4a5

Browse files
committed
fix(@angular/ssr): add support for configuring trusted proxy headers via environment variable
Adds support for configuring trusted proxy headers via the `NG_TRUST_PROXY_HEADERS` environment variable in `AngularNodeAppEngine`. This allows users to specify which proxy headers (such as `X-Forwarded-Host`) should be trusted when running the server-side application behind a reverse proxy, without needing to modify the application code. The environment variable accepts a comma-separated list of header names. If the `NG_TRUST_PROXY_HEADERS` environment variable is set and contains non-empty values, it will take precedence over the `trustProxyHeaders` option provided programmatically in the `AngularNodeAppEngine` constructor options.
1 parent 5adc925 commit d7fb4a5

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

packages/angular/ssr/node/src/app-engine.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AngularAppEngine } from '@angular/ssr';
1010
import type { IncomingMessage } from 'node:http';
1111
import type { Http2ServerRequest } from 'node:http2';
1212
import { AngularAppEngineOptions } from '../../src/app-engine';
13-
import { getAllowedHostsFromEnv } from './environment-options';
13+
import { getAllowedHostsFromEnv, getTrustProxyHeadersFromEnv } from './environment-options';
1414
import { attachNodeGlobalErrorHandlers } from './errors';
1515
import { createWebRequestFromNodeRequest } from './request';
1616

@@ -36,11 +36,14 @@ export class AngularNodeAppEngine {
3636
* @param options Options for the Angular Node.js server application engine.
3737
*/
3838
constructor(options?: AngularNodeAppEngineOptions) {
39-
this.angularAppEngine = new AngularAppEngine({
39+
const appEngineOptions: AngularAppEngineOptions = {
4040
...options,
41-
allowedHosts: [...getAllowedHostsFromEnv(), ...(options?.allowedHosts ?? [])],
42-
});
43-
this.trustProxyHeaders = options?.trustProxyHeaders;
41+
allowedHosts: getAllowedHostsFromEnv() ?? options?.allowedHosts,
42+
trustProxyHeaders: getTrustProxyHeadersFromEnv() ?? options?.trustProxyHeaders,
43+
};
44+
45+
this.angularAppEngine = new AngularAppEngine(appEngineOptions);
46+
this.trustProxyHeaders = appEngineOptions.trustProxyHeaders;
4447

4548
attachNodeGlobalErrorHandlers();
4649
}

packages/angular/ssr/node/src/environment-options.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,31 @@
1010
* Retrieves the list of allowed hosts from the environment variable `NG_ALLOWED_HOSTS`.
1111
* @returns An array of allowed hosts.
1212
*/
13-
export function getAllowedHostsFromEnv(): ReadonlyArray<string> {
14-
const allowedHosts: string[] = [];
15-
const envNgAllowedHosts = process.env['NG_ALLOWED_HOSTS'];
16-
if (!envNgAllowedHosts) {
17-
return allowedHosts;
13+
export function getAllowedHostsFromEnv(): ReadonlyArray<string> | undefined {
14+
return getArrayFromEnv('NG_ALLOWED_HOSTS');
15+
}
16+
17+
/**
18+
* Retrieves the list of trusted proxy headers from the environment variable `NG_TRUST_PROXY_HEADERS`.
19+
* @returns An array of trusted proxy headers.
20+
*/
21+
export function getTrustProxyHeadersFromEnv(): ReadonlyArray<string> | undefined {
22+
return getArrayFromEnv('NG_TRUST_PROXY_HEADERS');
23+
}
24+
25+
function getArrayFromEnv(envName: string): ReadonlyArray<string> | undefined {
26+
const envValue = process.env[envName];
27+
if (!envValue) {
28+
return undefined;
1829
}
1930

20-
const hosts = envNgAllowedHosts.split(',');
21-
for (const host of hosts) {
22-
const trimmed = host.trim();
31+
const values: string[] = [];
32+
for (const value of envValue.split(',')) {
33+
const trimmed = value.trim();
2334
if (trimmed.length > 0) {
24-
allowedHosts.push(trimmed);
35+
values.push(trimmed);
2536
}
2637
}
2738

28-
return allowedHosts;
39+
return values;
2940
}

0 commit comments

Comments
 (0)