|
1 | 1 | """Endpoint utilities for BedrockAgentCore services.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import re |
| 5 | +from urllib.parse import urlparse |
4 | 6 |
|
5 | 7 | # Environment-configurable constants with fallback defaults |
6 | 8 | DP_ENDPOINT_OVERRIDE = os.getenv("BEDROCK_AGENTCORE_DP_ENDPOINT") |
7 | 9 | CP_ENDPOINT_OVERRIDE = os.getenv("BEDROCK_AGENTCORE_CP_ENDPOINT") |
8 | 10 | DEFAULT_REGION = os.getenv("AWS_REGION", "us-west-2") |
9 | 11 |
|
| 12 | +# Regex for valid AWS region names (e.g., us-east-1, eu-west-2, cn-north-1, us-gov-west-1). |
| 13 | +# Uses \A and \Z anchors to prevent newline injection bypass that $ allows. |
| 14 | +_VALID_REGION_PATTERN = re.compile(r"\A[a-z]{2}(-[a-z]+)+-\d+\Z") |
| 15 | + |
| 16 | + |
| 17 | +class InvalidRegionError(ValueError): |
| 18 | + """Raised when an invalid AWS region string is provided. |
| 19 | +
|
| 20 | + This prevents SSRF attacks where a crafted region value |
| 21 | + (e.g., ``x@attacker.com:443/#``) could redirect SDK API calls |
| 22 | + to non-AWS hosts. |
| 23 | + """ |
| 24 | + |
| 25 | + |
| 26 | +def validate_region(region: str) -> str: |
| 27 | + """Validate that a region string is a well-formed AWS region name. |
| 28 | +
|
| 29 | + Args: |
| 30 | + region: The region string to validate. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + The validated region string (unchanged). |
| 34 | +
|
| 35 | + Raises: |
| 36 | + InvalidRegionError: If the region does not match the expected pattern. |
| 37 | + """ |
| 38 | + if not isinstance(region, str) or not _VALID_REGION_PATTERN.match(region): |
| 39 | + raise InvalidRegionError( |
| 40 | + f"Invalid AWS region: {region!r}. Region must match pattern like 'us-east-1', 'eu-west-2', 'cn-north-1'." |
| 41 | + ) |
| 42 | + return region |
| 43 | + |
| 44 | + |
| 45 | +def _validate_endpoint_url(url: str) -> str: |
| 46 | + """Validate that a constructed endpoint URL resolves to an AWS host. |
| 47 | +
|
| 48 | + This is a defense-in-depth check that catches URL manipulation even if |
| 49 | + the region regex is somehow bypassed. |
| 50 | +
|
| 51 | + Args: |
| 52 | + url: The constructed endpoint URL. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + The validated URL (unchanged). |
| 56 | +
|
| 57 | + Raises: |
| 58 | + InvalidRegionError: If the URL hostname does not end with an AWS domain. |
| 59 | + """ |
| 60 | + parsed = urlparse(url) |
| 61 | + hostname = parsed.hostname or "" |
| 62 | + _AWS_DOMAINS = (".amazonaws.com", ".amazonaws.com.cn", ".api.aws") |
| 63 | + if not any(hostname.endswith(d) for d in _AWS_DOMAINS): |
| 64 | + raise InvalidRegionError(f"Constructed endpoint resolves to non-AWS host: {hostname!r}") |
| 65 | + return url |
| 66 | + |
10 | 67 |
|
11 | 68 | def get_data_plane_endpoint(region: str = DEFAULT_REGION) -> str: |
12 | | - return DP_ENDPOINT_OVERRIDE or f"https://bedrock-agentcore.{region}.amazonaws.com" |
| 69 | + if DP_ENDPOINT_OVERRIDE: |
| 70 | + return _validate_endpoint_url(DP_ENDPOINT_OVERRIDE) |
| 71 | + validate_region(region) |
| 72 | + url = f"https://bedrock-agentcore.{region}.amazonaws.com" |
| 73 | + return _validate_endpoint_url(url) |
13 | 74 |
|
14 | 75 |
|
15 | 76 | def get_control_plane_endpoint(region: str = DEFAULT_REGION) -> str: |
16 | | - return CP_ENDPOINT_OVERRIDE or f"https://bedrock-agentcore-control.{region}.amazonaws.com" |
| 77 | + if CP_ENDPOINT_OVERRIDE: |
| 78 | + return _validate_endpoint_url(CP_ENDPOINT_OVERRIDE) |
| 79 | + validate_region(region) |
| 80 | + url = f"https://bedrock-agentcore-control.{region}.amazonaws.com" |
| 81 | + return _validate_endpoint_url(url) |
0 commit comments