fix(@angular/build): add host validation to internal SSR middleware - #33674
Closed
deprrous wants to merge 1 commit into
Closed
fix(@angular/build): add host validation to internal SSR middleware#33674deprrous wants to merge 1 commit into
deprrous wants to merge 1 commit into
Conversation
The internal SSR middleware (createAngularSsrInternalMiddleware) calls AngularServerApp.handle() directly without validating the request hostname against the configured allowedHosts. The external middleware correctly propagates the allowedHosts check via AngularAppEngine.ɵdisableAllowedHostsCheck, but the internal path has no equivalent validation. When allowedHosts is configured (not set to true), validate the request URL hostname before passing the request to the SSR engine. This prevents hostname hijacking via DNS rebinding when the dev server is exposed on 0.0.0.0.
There was a problem hiding this comment.
Code Review
This pull request introduces host validation in the Angular SSR middleware by checking incoming requests against Vite's allowedHosts configuration. The feedback identifies a flaw in the host matching logic where subdomains and leading dots are not handled correctly, potentially leading to failed matches or over-permissive access. A code suggestion is provided to resolve this issue.
Comment on lines
+71
to
+73
| const isAllowed = allowedHosts.some( | ||
| (host) => hostname === host || hostname.endsWith(`.${host}`), | ||
| ); |
There was a problem hiding this comment.
The current host matching logic has two issues:
- If an allowed host starts with a dot (e.g.,
.example.comto allow subdomains),hostname.endsWith(.${host})evaluates tohostname.endsWith('..example.com'), which will fail to match subdomains. - If an allowed host does not start with a dot (e.g.,
example.com),hostname.endsWith(.${host})will match subdomains (e.g.,sub.example.com), which is over-permissive and violates standard host matching behavior where subdomains are only allowed if a leading dot is explicitly specified.
We should update the matching logic to correctly handle leading dots, consistent with Vite and webpack-dev-server standards.
const isAllowed = allowedHosts.some((host) => {
if (host.startsWith('.')) {
return hostname === host.slice(1) || hostname.endsWith(host);
}
return hostname === host;
});
Collaborator
|
The steps above cannot reproduce this issue. The request will be blocked. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
createAngularSsrInternalMiddlewarecallsAngularServerApp.handle()directly without validating the request hostname against the configuredallowedHosts.The external middleware correctly propagates the check:
But the internal middleware has no equivalent validation. When
allowedHostsis configured as an array (nottrue), the internal SSR path still processes requests with arbitraryHostheaders.Impact
With
host: "0.0.0.0"+allowedHosts: ["myapp.com"], a DNS rebinding attack can setHost: attacker.comand the internal SSR middleware will:HttpClientrequests againstattacker.comThis is the same SSRF class as GHSA-rfh7-fxqc-q52v but in the dev server's internal middleware path.
Reproduction
Fix
Validate the request URL hostname against
allowedHostsbefore callingangularServerApp.handle(), consistent with the external middleware's behavior.