-
Notifications
You must be signed in to change notification settings - Fork 108
fix: validate region parameter to prevent SSRF request redirection #417
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,81 @@ | ||
| """Endpoint utilities for BedrockAgentCore services.""" | ||
|
|
||
| import os | ||
| import re | ||
| from urllib.parse import urlparse | ||
|
|
||
| # Environment-configurable constants with fallback defaults | ||
| DP_ENDPOINT_OVERRIDE = os.getenv("BEDROCK_AGENTCORE_DP_ENDPOINT") | ||
| CP_ENDPOINT_OVERRIDE = os.getenv("BEDROCK_AGENTCORE_CP_ENDPOINT") | ||
| DEFAULT_REGION = os.getenv("AWS_REGION", "us-west-2") | ||
|
|
||
| # Regex for valid AWS region names (e.g., us-east-1, eu-west-2, cn-north-1, us-gov-west-1). | ||
| # Uses \A and \Z anchors to prevent newline injection bypass that $ allows. | ||
| _VALID_REGION_PATTERN = re.compile(r"\A[a-z]{2}(-[a-z]+)+-\d+\Z") | ||
|
|
||
|
|
||
| class InvalidRegionError(ValueError): | ||
| """Raised when an invalid AWS region string is provided. | ||
|
|
||
| This prevents SSRF attacks where a crafted region value | ||
| (e.g., ``x@attacker.com:443/#``) could redirect SDK API calls | ||
| to non-AWS hosts. | ||
| """ | ||
|
|
||
|
|
||
| def validate_region(region: str) -> str: | ||
| """Validate that a region string is a well-formed AWS region name. | ||
|
|
||
| Args: | ||
| region: The region string to validate. | ||
|
|
||
| Returns: | ||
| The validated region string (unchanged). | ||
|
|
||
| Raises: | ||
| InvalidRegionError: If the region does not match the expected pattern. | ||
| """ | ||
| if not isinstance(region, str) or not _VALID_REGION_PATTERN.match(region): | ||
| raise InvalidRegionError( | ||
| f"Invalid AWS region: {region!r}. Region must match pattern like 'us-east-1', 'eu-west-2', 'cn-north-1'." | ||
| ) | ||
| return region | ||
|
|
||
|
|
||
| def _validate_endpoint_url(url: str) -> str: | ||
| """Validate that a constructed endpoint URL resolves to an AWS host. | ||
|
|
||
| This is a defense-in-depth check that catches URL manipulation even if | ||
| the region regex is somehow bypassed. | ||
|
|
||
| Args: | ||
| url: The constructed endpoint URL. | ||
|
|
||
| Returns: | ||
| The validated URL (unchanged). | ||
|
|
||
| Raises: | ||
| InvalidRegionError: If the URL hostname does not end with an AWS domain. | ||
| """ | ||
| parsed = urlparse(url) | ||
| hostname = parsed.hostname or "" | ||
| _AWS_DOMAINS = (".amazonaws.com", ".amazonaws.com.cn", ".api.aws") | ||
| if not any(hostname.endswith(d) for d in _AWS_DOMAINS): | ||
| raise InvalidRegionError(f"Constructed endpoint resolves to non-AWS host: {hostname!r}") | ||
| return url | ||
|
|
||
|
|
||
| def get_data_plane_endpoint(region: str = DEFAULT_REGION) -> str: | ||
| return DP_ENDPOINT_OVERRIDE or f"https://bedrock-agentcore.{region}.amazonaws.com" | ||
| if DP_ENDPOINT_OVERRIDE: | ||
| return _validate_endpoint_url(DP_ENDPOINT_OVERRIDE) | ||
| validate_region(region) | ||
| url = f"https://bedrock-agentcore.{region}.amazonaws.com" | ||
| return _validate_endpoint_url(url) | ||
|
|
||
|
|
||
| def get_control_plane_endpoint(region: str = DEFAULT_REGION) -> str: | ||
| return CP_ENDPOINT_OVERRIDE or f"https://bedrock-agentcore-control.{region}.amazonaws.com" | ||
| if CP_ENDPOINT_OVERRIDE: | ||
| return _validate_endpoint_url(CP_ENDPOINT_OVERRIDE) | ||
| validate_region(region) | ||
| url = f"https://bedrock-agentcore-control.{region}.amazonaws.com" | ||
| return _validate_endpoint_url(url) |
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
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
Oops, something went wrong.
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.