|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Unit tests for region_validation module.""" |
| 14 | +from __future__ import absolute_import |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from sagemaker.core.region_validation import ( |
| 19 | + InvalidRegionError, |
| 20 | + validate_region, |
| 21 | + validate_endpoint_url, |
| 22 | +) |
| 23 | + |
| 24 | +# All known AWS regions as of 2026. This list ensures the regex pattern |
| 25 | +# does not accidentally reject any legitimate region string. |
| 26 | +ALL_AWS_REGIONS = [ |
| 27 | + # US East |
| 28 | + "us-east-1", |
| 29 | + "us-east-2", |
| 30 | + # US West |
| 31 | + "us-west-1", |
| 32 | + "us-west-2", |
| 33 | + # Africa |
| 34 | + "af-south-1", |
| 35 | + # Asia Pacific |
| 36 | + "ap-east-1", |
| 37 | + "ap-south-1", |
| 38 | + "ap-south-2", |
| 39 | + "ap-southeast-1", |
| 40 | + "ap-southeast-2", |
| 41 | + "ap-southeast-3", |
| 42 | + "ap-southeast-4", |
| 43 | + "ap-southeast-5", |
| 44 | + "ap-northeast-1", |
| 45 | + "ap-northeast-2", |
| 46 | + "ap-northeast-3", |
| 47 | + # Canada |
| 48 | + "ca-central-1", |
| 49 | + "ca-west-1", |
| 50 | + # Europe |
| 51 | + "eu-central-1", |
| 52 | + "eu-central-2", |
| 53 | + "eu-west-1", |
| 54 | + "eu-west-2", |
| 55 | + "eu-west-3", |
| 56 | + "eu-south-1", |
| 57 | + "eu-south-2", |
| 58 | + "eu-north-1", |
| 59 | + # Israel |
| 60 | + "il-central-1", |
| 61 | + # Middle East |
| 62 | + "me-south-1", |
| 63 | + "me-central-1", |
| 64 | + # South America |
| 65 | + "sa-east-1", |
| 66 | + # China |
| 67 | + "cn-north-1", |
| 68 | + "cn-northwest-1", |
| 69 | + # GovCloud |
| 70 | + "us-gov-west-1", |
| 71 | + "us-gov-east-1", |
| 72 | + # ISO / ISOB partitions |
| 73 | + "us-iso-east-1", |
| 74 | + "us-iso-west-1", |
| 75 | + "us-isob-east-1", |
| 76 | + # Mexico |
| 77 | + "mx-central-1", |
| 78 | + # Asia Pacific (Malaysia / Thailand) |
| 79 | + "ap-southeast-7", |
| 80 | +] |
| 81 | + |
| 82 | + |
| 83 | +class TestValidateRegionAcceptsAllAwsRegions: |
| 84 | + """Ensure validate_region passes for every known AWS region.""" |
| 85 | + |
| 86 | + @pytest.mark.parametrize("region", ALL_AWS_REGIONS) |
| 87 | + def test_valid_region(self, region): |
| 88 | + assert validate_region(region) == region |
| 89 | + |
| 90 | + |
| 91 | +class TestValidateRegionRejectsInvalidInputs: |
| 92 | + """Ensure validate_region rejects malicious or malformed region strings.""" |
| 93 | + |
| 94 | + @pytest.mark.parametrize( |
| 95 | + "invalid_region", |
| 96 | + [ |
| 97 | + # SSRF payloads |
| 98 | + "x@attacker.com:443/#", |
| 99 | + "us-east-1.attacker.com", |
| 100 | + "us-east-1\n.attacker.com", |
| 101 | + # Empty / whitespace |
| 102 | + "", |
| 103 | + " ", |
| 104 | + # Missing components |
| 105 | + "useast1", |
| 106 | + "us-east", |
| 107 | + "us-1", |
| 108 | + # Uppercase |
| 109 | + "US-EAST-1", |
| 110 | + "Us-East-1", |
| 111 | + # Special characters |
| 112 | + "us-east-1; rm -rf /", |
| 113 | + "us-east-1/../../etc/passwd", |
| 114 | + # Non-string types |
| 115 | + None, |
| 116 | + 123, |
| 117 | + ["us-east-1"], |
| 118 | + # Trailing/leading whitespace |
| 119 | + " us-east-1", |
| 120 | + "us-east-1 ", |
| 121 | + # Newline injection |
| 122 | + "us-east-1\n", |
| 123 | + "us-east-1\r\n", |
| 124 | + # URL-like |
| 125 | + "https://us-east-1", |
| 126 | + # Simple fake region (no digit suffix) |
| 127 | + "testregion", |
| 128 | + ], |
| 129 | + ) |
| 130 | + def test_invalid_region(self, invalid_region): |
| 131 | + with pytest.raises(InvalidRegionError): |
| 132 | + validate_region(invalid_region) |
| 133 | + |
| 134 | + |
| 135 | +class TestValidateEndpointUrl: |
| 136 | + """Ensure validate_endpoint_url accepts AWS domains and rejects others.""" |
| 137 | + |
| 138 | + @pytest.mark.parametrize( |
| 139 | + "url", |
| 140 | + [ |
| 141 | + "https://sagemaker.us-east-1.amazonaws.com", |
| 142 | + "https://api.sagemaker.us-west-2.amazonaws.com", |
| 143 | + "https://runtime.sagemaker.eu-west-1.amazonaws.com", |
| 144 | + "https://sagemaker.cn-north-1.amazonaws.com.cn", |
| 145 | + "https://domain.studio.us-west-2.sagemaker.aws", |
| 146 | + ], |
| 147 | + ) |
| 148 | + def test_valid_endpoint(self, url): |
| 149 | + assert validate_endpoint_url(url) == url |
| 150 | + |
| 151 | + @pytest.mark.parametrize( |
| 152 | + "url", |
| 153 | + [ |
| 154 | + "https://attacker.com", |
| 155 | + "https://sagemaker.us-east-1.attacker.com", |
| 156 | + "https://amazonaws.com.attacker.com", |
| 157 | + ], |
| 158 | + ) |
| 159 | + def test_invalid_endpoint(self, url): |
| 160 | + with pytest.raises(InvalidRegionError): |
| 161 | + validate_endpoint_url(url) |
0 commit comments