Skip to content

fix(@angular/ssr): introduce trustProxyHeaders option to safely validate and sanitize proxy headers#33031

Open
alan-agius4 wants to merge 1 commit intoangular:21.2.xfrom
alan-agius4:x-forward-prefix-validation-patch
Open

fix(@angular/ssr): introduce trustProxyHeaders option to safely validate and sanitize proxy headers#33031
alan-agius4 wants to merge 1 commit intoangular:21.2.xfrom
alan-agius4:x-forward-prefix-validation-patch

Conversation

@alan-agius4
Copy link
Copy Markdown
Collaborator


This commit adds the trustProxyHeaders option to AngularAppEngineOptions and AngularNodeAppEngineOptions to configure, validate, and sanitize X-Forwarded-* headers.

  • When trustProxyHeaders is undefined (default):
    • Allows X-Forwarded-Host and X-Forwarded-Proto.
    • Intercepts X-Forwarded-Prefix and triggers a dynamic CSR deoptimization to skip SSR if present.
    • Logs an informative message when receiving any other X-Forwarded-* headers.
  • When false:
    • Ignores and strips all proxy headers from the request.
  • When true:
    • Trusts all proxy headers.
  • When a string array:
    • Allows only the proxy headers provided inside the array.

Example:

const engine = new AngularAppEngine({
  // Allow all proxy headers
  trustProxyHeaders: true,
});

// Or explicitly allow specific headers:
const engine = new AngularAppEngine({
  trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-prefix'],
});

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the trustProxyHeaders configuration to AngularAppEngine, allowing for explicit control over which X-Forwarded-* headers are trusted during SSR. It replaces the previous header-patching approach with a more efficient sanitization process and adds a mechanism to deoptimize to CSR when certain proxy headers are present but untrusted. Review feedback suggests that the new VALID_PREFIX_REGEX is overly restrictive regarding dots in path segments and that the warning for unconfigured proxy headers should be adjusted to reduce log noise for default allowed headers.

Comment thread packages/angular/ssr/src/utils/validation.ts
Comment thread packages/angular/ssr/src/utils/validation.ts
…ate and sanitize proxy headers

This commit adds the `trustProxyHeaders` option to `AngularAppEngineOptions` and `AngularNodeAppEngineOptions` to configure, validate, and sanitize `X-Forwarded-*` headers.
- When `trustProxyHeaders` is `undefined` (default):
  - Allows `X-Forwarded-Host` and `X-Forwarded-Proto`.
  - Intercepts `X-Forwarded-Prefix` and triggers a dynamic CSR deoptimization to skip SSR if present.
  - Logs an informative message when receiving any other `X-Forwarded-*` headers.
- When `false`:
  - Ignores and strips all proxy headers from the request.
- When `true`:
  - Trusts all proxy headers.
- When a string array:
  - Allows only the proxy headers provided inside the array.

Example:
```ts
const engine = new AngularAppEngine({
  // Allow all proxy headers
  trustProxyHeaders: true,
});

// Or explicitly allow specific headers:
const engine = new AngularAppEngine({
  trustProxyHeaders: ['x-forwarded-host', 'x-forwarded-prefix'],
});
```
@alan-agius4 alan-agius4 force-pushed the x-forward-prefix-validation-patch branch from d22ebae to e769eae Compare April 22, 2026 08:03
@alan-agius4 alan-agius4 added the action: review The PR is still awaiting reviews from at least one requested reviewer label Apr 22, 2026
@alan-agius4 alan-agius4 requested a review from dgp1130 April 22, 2026 09:29
@alan-agius4 alan-agius4 added the target: patch This PR is targeted for the next patch release label Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/ssr target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant