Skip to content

Commit 6b1ccc5

Browse files
committed
fixup! fix(@angular/ssr): support all X-Forwarded-* headers when trustProxyHeaders is true
1 parent 0aeebba commit 6b1ccc5

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

packages/angular/ssr/src/utils/validation.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* Internal sentinel string representing a wildcard rule to trust all proxy headers.
1111
*/
12-
const TRUST_ALL_PROXY_HEADERS = 'ɵ*';
12+
const TRUST_ALL_PROXY_HEADERS = '*';
1313

1414
/**
1515
* The set of headers that should be validated for host header injection attacks.
@@ -251,5 +251,12 @@ export function normalizeTrustProxyHeaders(
251251
return new Set([TRUST_ALL_PROXY_HEADERS]);
252252
}
253253

254-
return new Set(trustProxyHeaders.map((h) => h.toLowerCase()));
254+
const normalizedTrustedProxyHeaders = new Set(trustProxyHeaders.map((h) => h.toLowerCase()));
255+
if (normalizedTrustedProxyHeaders.has(TRUST_ALL_PROXY_HEADERS)) {
256+
throw new Error(
257+
`"${TRUST_ALL_PROXY_HEADERS}" is not allowed as a value for the "trustProxyHeaders" option.`,
258+
);
259+
}
260+
261+
return normalizedTrustedProxyHeaders;
255262
}

packages/angular/ssr/test/utils/validation_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ describe('Validation Utils', () => {
3838
});
3939
});
4040

41+
describe('normalizeTrustProxyHeaders', () => {
42+
it('should return an empty set when input is undefined', () => {
43+
expect(normalizeTrustProxyHeaders(undefined)).toEqual(new Set());
44+
});
45+
46+
it('should return an empty set when input is false', () => {
47+
expect(normalizeTrustProxyHeaders(false)).toEqual(new Set());
48+
});
49+
50+
it('should return a set containing "*" when input is true', () => {
51+
expect(normalizeTrustProxyHeaders(true)).toEqual(new Set(['*']));
52+
});
53+
54+
it('should return a set of lowercased header names when input is an array of strings', () => {
55+
expect(normalizeTrustProxyHeaders(['X-Forwarded-Host', 'X-Forwarded-Proto'])).toEqual(
56+
new Set(['x-forwarded-host', 'x-forwarded-proto']),
57+
);
58+
});
59+
60+
it('should throw an error if input array contains "*"', () => {
61+
expect(() => normalizeTrustProxyHeaders(['*'])).toThrowError(
62+
'"*" is not allowed as a value for the "trustProxyHeaders" option.',
63+
);
64+
expect(() => normalizeTrustProxyHeaders(['X-Forwarded-Host', '*'])).toThrowError(
65+
'"*" is not allowed as a value for the "trustProxyHeaders" option.',
66+
);
67+
});
68+
});
69+
4170
describe('validateUrl', () => {
4271
const allowedHosts = new Set(['example.com', '*.google.com']);
4372

0 commit comments

Comments
 (0)