Repository navigation
refactor(@angular/ssr): reuse ɵisHostAllowed from @angular/platform-server #34793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alan-agius4
wants to merge
1
commit into
angular:main
Choose a base branch
from
alan-agius4:refactor/ssr-use-platform-server-is-host-allowed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−252
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| /** | ||
| * Internal sentinel string representing a wildcard rule to trust all proxy headers. | ||
| */ | ||
| const TRUST_ALL_PROXY_HEADERS = '*'; | ||
|
|
||
| /** | ||
| * Extracts the first value from a multi-value header string. | ||
| * | ||
| * @param value - A string or an array of strings representing the header values. | ||
| * If it's a string, values are expected to be comma-separated. | ||
| * @returns The first trimmed value from the multi-value header, or `undefined` if the input is invalid or empty. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * getFirstHeaderValue("value1, value2, value3"); // "value1" | ||
| * getFirstHeaderValue(["value1", "value2"]); // "value1" | ||
| * getFirstHeaderValue(undefined); // undefined | ||
| * ``` | ||
| */ | ||
| export function getFirstHeaderValue( | ||
| value: string | string[] | undefined | null, | ||
| ): string | undefined { | ||
| return value?.toString().split(',', 1)[0]?.trim(); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a specific proxy header is allowed. | ||
| * | ||
| * @param headerName - The name of the proxy header to check. | ||
| * @param trustProxyHeaders - A set of allowed proxy headers. | ||
| * @returns `true` if the header is allowed, `false` otherwise. | ||
| */ | ||
| export function isProxyHeaderAllowed( | ||
| headerName: string, | ||
| trustProxyHeaders: ReadonlySet<string>, | ||
| ): boolean { | ||
| return ( | ||
| trustProxyHeaders.has(TRUST_ALL_PROXY_HEADERS) || | ||
| trustProxyHeaders.has(headerName.toLowerCase()) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes the `trustProxyHeaders` option to a consistent representation. | ||
| * @param trustProxyHeaders The input `trustProxyHeaders` value. | ||
| * @returns A `Set<string>` of normalized header names. | ||
| */ | ||
| export function normalizeTrustProxyHeaders( | ||
| trustProxyHeaders: boolean | readonly string[] | undefined, | ||
| ): ReadonlySet<string> { | ||
| if (!trustProxyHeaders) { | ||
| return new Set(); | ||
| } | ||
|
|
||
| if (trustProxyHeaders === true) { | ||
| return new Set([TRUST_ALL_PROXY_HEADERS]); | ||
| } | ||
|
|
||
| const normalizedTrustedProxyHeaders = new Set<string>(); | ||
| for (const header of trustProxyHeaders) { | ||
| const lowerHeader = header.toLowerCase(); | ||
| if (lowerHeader === TRUST_ALL_PROXY_HEADERS) { | ||
| throw new Error( | ||
| `"${TRUST_ALL_PROXY_HEADERS}" is not allowed as a value for the "trustProxyHeaders" option.`, | ||
| ); | ||
| } | ||
| const isValid = lowerHeader === 'forwarded' || lowerHeader.startsWith('x-forwarded-'); | ||
| if (!isValid) { | ||
| throw new Error( | ||
| `"${header}" is not a valid proxy header. Trusted proxy headers must be "forwarded" or start with "x-forwarded-".`, | ||
| ); | ||
| } | ||
| normalizedTrustedProxyHeaders.add(lowerHeader); | ||
| } | ||
|
|
||
| return normalizedTrustedProxyHeaders; | ||
| } | ||
|
|
||
| /** | ||
| * Parses the standard `Forwarded` header (RFC 7239). | ||
| * It extracts the parameters from the first (leftmost) element in the header. | ||
| * | ||
| * @param headerValue - The value of the `Forwarded` header. | ||
| * @returns A record of lowercase parameter names to their values. | ||
| */ | ||
| export function parseForwardedHeader( | ||
| headerValue: string | null | undefined, | ||
| ): Record<string, string> { | ||
| if (!headerValue) { | ||
| return {}; | ||
| } | ||
|
|
||
| const params: Record<string, string> = {}; | ||
| let inQuotes = false; | ||
| let escaped = false; | ||
| let currentKey = ''; | ||
| let currentValue = ''; | ||
| let isParsingValue = false; | ||
| let isKeyEnded = false; | ||
| let isParsingValueEnded = false; | ||
|
|
||
| for (const char of headerValue) { | ||
| if (escaped) { | ||
| escaped = false; | ||
| if (isParsingValue) { | ||
| currentValue += char; | ||
| } else { | ||
| currentKey += char; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (char === '\\') { | ||
| if (inQuotes) { | ||
| escaped = true; | ||
| } else if (isParsingValue) { | ||
| currentValue += char; | ||
| } else { | ||
| currentKey += char; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (char === '"') { | ||
| inQuotes = !inQuotes; | ||
| continue; | ||
| } | ||
|
|
||
| if (inQuotes) { | ||
| if (isParsingValue) { | ||
| currentValue += char; | ||
| } else { | ||
| currentKey += char; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (char === ',') { | ||
| addParam(currentKey, currentValue, isParsingValue, params); | ||
| break; | ||
| } | ||
|
|
||
| if (char === ';') { | ||
| addParam(currentKey, currentValue, isParsingValue, params); | ||
| currentKey = ''; | ||
| currentValue = ''; | ||
| isParsingValue = false; | ||
| isKeyEnded = false; | ||
| isParsingValueEnded = false; | ||
| continue; | ||
| } | ||
|
|
||
| if (char === '=') { | ||
| if (!isParsingValue) { | ||
| isParsingValue = true; | ||
| } else { | ||
| currentValue += char; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (char === ' ' || char === '\t') { | ||
| if (isParsingValue) { | ||
| if (currentValue.length > 0) { | ||
| isParsingValueEnded = true; | ||
| } | ||
| } else if (currentKey.length > 0) { | ||
| isKeyEnded = true; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (isParsingValue) { | ||
| if (!isParsingValueEnded) { | ||
| currentValue += char; | ||
| } | ||
| } else if (isKeyEnded) { | ||
| currentKey = char; | ||
| isKeyEnded = false; | ||
| } else { | ||
| currentKey += char; | ||
| } | ||
| } | ||
|
|
||
| if (currentKey || currentValue || isParsingValue) { | ||
| addParam(currentKey, currentValue, isParsingValue, params); | ||
| } | ||
|
|
||
| return params; | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to add a parameter to the params object. | ||
| * @param key - The key to add. | ||
| * @param value - The value to add. | ||
| * @param hasValue - Whether the parameter has a value. | ||
| * @param params - The params object to add the parameter to. | ||
| */ | ||
| function addParam( | ||
| key: string, | ||
| value: string, | ||
| hasValue: boolean, | ||
| params: Record<string, string>, | ||
| ): void { | ||
| if (!hasValue) { | ||
| return; | ||
| } | ||
|
|
||
| const trimmedKey = key.trim().toLowerCase(); | ||
| if (trimmedKey) { | ||
| params[trimmedKey] = value; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.