Repository navigation
Sanitize authorization code response payloads consistently across Node clients #8959
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
Changes from 3 commits
0077b31
400e0cd
39e2371
5fe6737
c084090
d5f15ff
96e3401
37006f0
665750b
92fea25
b8fbc22
6a84164
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "Validate authority and region inputs and honor instance-aware configuration during authorization code redemption [#8959](https://github.com/AzureAD/microsoft-authentication-library-for-js/pull/8959)", | ||
| "packageName": "@azure/msal-common", | ||
| "email": "nugetaad@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "Sanitize authorization code response payloads consistently across Node clients [#8959](https://github.com/AzureAD/microsoft-authentication-library-for-js/pull/8959)", | ||
| "packageName": "@azure/msal-node", | ||
| "email": "nugetaad@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,20 @@ import * as PerformanceEvents from "../telemetry/performance/PerformanceEvents.j | |
| import { invokeAsync } from "../utils/FunctionWrappers.js"; | ||
| import { Logger } from "../logger/Logger.js"; | ||
|
|
||
| const REGION_NAME_PATTERN = /^[a-z][a-z0-9-]*$/i; | ||
|
|
||
| /** | ||
| * Whether a region can safely be used as a single DNS label. | ||
| */ | ||
| export function isValidRegion(region: string): boolean { | ||
| return ( | ||
| typeof region === "string" && | ||
|
tnorling marked this conversation as resolved.
Resolved
|
||
| region.length <= 63 && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why 63? Seems kind of random and arbitrary, a comment explaining this would help
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The region becomes a DNS label, whose maximum length is 63 octets under RFC 1035 §2.3.4. Since we allow only ASCII, that equals 63 characters. Added a named constant and a comment explaining the limit. |
||
| REGION_NAME_PATTERN.test(region) && | ||
| !region.endsWith("-") | ||
|
tnorling marked this conversation as resolved.
Resolved
|
||
| ); | ||
| } | ||
|
|
||
| export class RegionDiscovery { | ||
| // Network interface to make requests with. | ||
| protected networkInterface: INetworkModule; | ||
|
|
@@ -125,13 +139,20 @@ export class RegionDiscovery { | |
| Constants.RegionDiscoverySources.ENVIRONMENT_VARIABLE; | ||
| } | ||
|
|
||
| // If no region was auto detected from the environment or from the IMDS endpoint, mark the attempt as a FAILED_AUTO_DETECTION | ||
| if (!autodetectedRegionName) { | ||
| // Untrusted discovery values must be a single DNS label before becoming a host prefix. | ||
| if (!autodetectedRegionName || !isValidRegion(autodetectedRegionName)) { | ||
| if (autodetectedRegionName) { | ||
| this.logger.warning( | ||
| "Region discovery returned an invalid region. Using the global authority.", | ||
| this.correlationId | ||
| ); | ||
| } | ||
| regionDiscoveryMetadata.region_source = | ||
| Constants.RegionDiscoverySources.FAILED_AUTO_DETECTION; | ||
| return null; | ||
| } | ||
|
|
||
| return autodetectedRegionName || null; | ||
| return autodetectedRegionName; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,12 @@ import { StringUtils } from "../utils/StringUtils.js"; | |
| import { IUri } from "./IUri.js"; | ||
| import * as Constants from "../utils/Constants.js"; | ||
|
|
||
| const UNSAFE_AUTHORITY = /[\x00-\x20\x7f\\@]/; | ||
|
tnorling marked this conversation as resolved.
Resolved
|
||
| const DEFAULT_PORTS: Record<string, string> = { | ||
| "https:": ":443", | ||
| "http:": ":80", | ||
| }; | ||
|
|
||
| /** | ||
| * Url object class which can perform various transformations on url strings. | ||
| */ | ||
|
|
@@ -169,6 +175,34 @@ export class UrlString { | |
| QueryString: match[7], | ||
| } as IUri; | ||
|
|
||
| let parsedUrl: URL; | ||
| try { | ||
| parsedUrl = new URL(this.urlString); | ||
| } catch { | ||
| throw createClientConfigurationError( | ||
| ClientConfigurationErrorCodes.urlParseError, | ||
| this.correlationId | ||
| ); | ||
| } | ||
|
|
||
| const host = urlComponents.HostNameAndPort?.toLowerCase(); | ||
| const schemeAndAuthority = `${urlComponents.Protocol}//${host}`; | ||
| const defaultPort = DEFAULT_PORTS[parsedUrl.protocol] ?? ""; | ||
| const hostMatches = | ||
|
tnorling marked this conversation as resolved.
Resolved
|
||
| host === parsedUrl.host || | ||
| host === `${parsedUrl.host}${defaultPort}` || | ||
| (/^\[[0-9a-f:.]+\](?::[0-9]+)?$/i.test(host ?? "") && | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this regex instead. The earlier |
||
| new URL(`${urlComponents.Protocol}//${host}`).host === | ||
| parsedUrl.host); | ||
|
|
||
| // Check authority ambiguity without restricting resource paths, queries, or fragments. | ||
| if (UNSAFE_AUTHORITY.test(schemeAndAuthority) || !hostMatches) { | ||
| throw createClientConfigurationError( | ||
| ClientConfigurationErrorCodes.urlParseError, | ||
| this.correlationId | ||
| ); | ||
| } | ||
|
|
||
| let pathSegments = urlComponents.AbsolutePath.split("/"); | ||
| pathSegments = pathSegments.filter((val) => val && val.length > 0); // remove empty elements | ||
| urlComponents.PathSegments = pathSegments; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.