Skip to content

Commit 9d7113f

Browse files
alan-agius4dgp1130
authored andcommitted
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. (cherry picked from commit 126b19b)
1 parent 9640d1f commit 9d7113f

3 files changed

Lines changed: 30 additions & 19 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 { createWebRequestFromNodeRequest } from './request';
1515

1616
/**
@@ -37,11 +37,14 @@ export class AngularNodeAppEngine {
3737
* @param options Options for the Angular Node.js server application engine.
3838
*/
3939
constructor(options?: AngularNodeAppEngineOptions) {
40-
this.angularAppEngine = new AngularAppEngine({
40+
const appEngineOptions: AngularAppEngineOptions = {
4141
...options,
42-
allowedHosts: [...getAllowedHostsFromEnv(), ...(options?.allowedHosts ?? [])],
43-
});
44-
this.trustProxyHeaders = options?.trustProxyHeaders;
42+
allowedHosts: getAllowedHostsFromEnv() ?? options?.allowedHosts,
43+
trustProxyHeaders: getTrustProxyHeadersFromEnv() ?? options?.trustProxyHeaders,
44+
};
45+
46+
this.angularAppEngine = new AngularAppEngine(appEngineOptions);
47+
this.trustProxyHeaders = appEngineOptions.trustProxyHeaders;
4548
}
4649

4750
/**

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ export class CommonEngine {
7171
private readonly allowedHosts: ReadonlySet<string>;
7272

7373
constructor(private options?: CommonEngineOptions) {
74-
this.allowedHosts = new Set([
75-
...getAllowedHostsFromEnv(),
76-
...(this.options?.allowedHosts ?? []),
77-
]);
74+
this.allowedHosts = new Set(getAllowedHostsFromEnv() ?? this.options?.allowedHosts ?? []);
7875
}
7976

8077
/**

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)