diff --git a/.cspell.json b/.cspell.json index 6b2e04fec4d..8a5df1dc4d1 100644 --- a/.cspell.json +++ b/.cspell.json @@ -186,6 +186,9 @@ "overengineered", "callfopen", "throughs", + "msteiger", + "fseeki", + "ftelli", // Json parser "Jsonize", "CJSON", diff --git a/src/aws-cpp-sdk-core/source/endpoint/BuiltInParameters.cpp b/src/aws-cpp-sdk-core/source/endpoint/BuiltInParameters.cpp index 8c6f3200c9c..c5a48686775 100644 --- a/src/aws-cpp-sdk-core/source/endpoint/BuiltInParameters.cpp +++ b/src/aws-cpp-sdk-core/source/endpoint/BuiltInParameters.cpp @@ -4,6 +4,7 @@ */ #include +#include #include #include @@ -41,7 +42,9 @@ namespace Endpoint if (!config.region.empty()) { static const char* FIPS_PREFIX = "fips-"; static const char* FIPS_SUFFIX = "-fips"; - if (config.region.rfind(FIPS_PREFIX, 0) == 0) { + if (!Aws::Utils::IsValidDnsLabel(config.region)) { + AWS_LOGSTREAM_ERROR(ENDPOINT_BUILTIN_LOG_TAG, "Invalid region name: " << config.region); + } else if (config.region.rfind(FIPS_PREFIX, 0) == 0) { // Backward compatibility layer for code hacking previous SDK version Aws::String regionOverride = config.region.substr(strlen(FIPS_PREFIX)); forceFIPS = true; diff --git a/tests/aws-cpp-sdk-core-tests/endpoint/endpointParamTest.cpp b/tests/aws-cpp-sdk-core-tests/endpoint/endpointParamTest.cpp index edca8957b2d..503bb9225c2 100644 --- a/tests/aws-cpp-sdk-core-tests/endpoint/endpointParamTest.cpp +++ b/tests/aws-cpp-sdk-core-tests/endpoint/endpointParamTest.cpp @@ -4,7 +4,7 @@ #include #include -namespace EndpointParamTest{ +namespace { const size_t RulesBlobSize = 498; using RulesBlobT = Aws::Array; @@ -31,21 +31,89 @@ static constexpr RulesBlobT RulesBlob = {{ 's','e','t','"',',','"','t','y','p','e','"',':','"','e','r','r','o','r','"','}',']','}','\0' }}; +/** +We store endpoint rules json as a character array. This is the pretty print +of the following test rules set +{ + "version": "1.0", + "parameters": { + "Region": { + "builtIn": "AWS::Region", + "required": false, + "documentation": "The AWS region used to dispatch the request.", + "type": "string" + } + }, + "rules": [ + { + "documentation": "Test region blob", + "conditions": [ + { + "fn": "isSet", + "argv": [ + { + "ref": "Region" + } + ] + } + ], + "endpoint": { + "url": "https://tartarus.{Region}.amazon.aws.com/" + }, + "type": "endpoint" + }, + { + "conditions": [], + "documentation": "error fallthrough", + "error": "no array values set", + "type": "error" + } + ] } + */ +const size_t RegionRulesBlobSize = 447; +using RegionRulesBlobT = Aws::Array; +static constexpr RegionRulesBlobT RegionRulesBlob = { + {'{', '"', 'v', 'e', 'r', 's', 'i', 'o', 'n', '"', ':', '"', '1', '.', '0', '"', ',', '"', 'p', 'a', 'r', 'a', 'm', 'e', 't', 'e', 'r', + 's', '"', ':', '{', '"', 'R', 'e', 'g', 'i', 'o', 'n', '"', ':', '{', '"', 'b', 'u', 'i', 'l', 't', 'I', 'n', '"', ':', '"', 'A', 'W', + 'S', ':', ':', 'R', 'e', 'g', 'i', 'o', 'n', '"', ',', '"', 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd', '"', ':', 'f', 'a', 'l', 's', 'e', + ',', '"', 'd', 'o', 'c', 'u', 'm', 'e', 'n', 't', 'a', 't', 'i', 'o', 'n', '"', ':', '"', 'T', 'h', 'e', ' ', 'A', 'W', 'S', ' ', 'r', + 'e', 'g', 'i', 'o', 'n', ' ', 'u', 's', 'e', 'd', ' ', 't', 'o', ' ', 'd', 'i', 's', 'p', 'a', 't', 'c', 'h', ' ', 't', 'h', 'e', ' ', + 'r', 'e', 'q', 'u', 'e', 's', 't', '.', '"', ',', '"', 't', 'y', 'p', 'e', '"', ':', '"', 's', 't', 'r', 'i', 'n', 'g', '"', '}', '}', + ',', '"', 'r', 'u', 'l', 'e', 's', '"', ':', '[', '{', '"', 'd', 'o', 'c', 'u', 'm', 'e', 'n', 't', 'a', 't', 'i', 'o', 'n', '"', ':', + '"', 'T', 'e', 's', 't', ' ', 'r', 'e', 'g', 'i', 'o', 'n', ' ', 'b', 'l', 'o', 'b', '"', ',', '"', 'c', 'o', 'n', 'd', 'i', 't', 'i', + 'o', 'n', 's', '"', ':', '[', '{', '"', 'f', 'n', '"', ':', '"', 'i', 's', 'S', 'e', 't', '"', ',', '"', 'a', 'r', 'g', 'v', '"', ':', + '[', '{', '"', 'r', 'e', 'f', '"', ':', '"', 'R', 'e', 'g', 'i', 'o', 'n', '"', '}', ']', '}', ']', ',', '"', 'e', 'n', 'd', 'p', 'o', + 'i', 'n', 't', '"', ':', '{', '"', 'u', 'r', 'l', '"', ':', '"', 'h', 't', 't', 'p', 's', ':', '/', '/', 't', 'a', 'r', 't', 'a', 'r', + 'u', 's', '.', '{', 'R', 'e', 'g', 'i', 'o', 'n', '}', '.', 'a', 'm', 'a', 'z', 'o', 'n', '.', 'a', 'w', 's', '.', 'c', 'o', 'm', '/', + '"', '}', ',', '"', 't', 'y', 'p', 'e', '"', ':', '"', 'e', 'n', 'd', 'p', 'o', 'i', 'n', 't', '"', '}', ',', '{', '"', 'c', 'o', 'n', + 'd', 'i', 't', 'i', 'o', 'n', 's', '"', ':', '[', ']', ',', '"', 'd', 'o', 'c', 'u', 'm', 'e', 'n', 't', 'a', 't', 'i', 'o', 'n', '"', + ':', '"', 'e', 'r', 'r', 'o', 'r', ' ', 'f', 'a', 'l', 'l', 't', 'h', 'r', 'o', 'u', 'g', 'h', '"', ',', '"', 'e', 'r', 'r', 'o', 'r', + '"', ':', '"', 'n', 'o', ' ', 'a', 'r', 'r', 'a', 'y', ' ', 'v', 'a', 'l', 'u', 'e', 's', ' ', 's', 'e', 't', '"', ',', '"', 't', 'y', + 'p', 'e', '"', ':', '"', 'e', 'r', 'r', 'o', 'r', '"', '}', ']', '}', '\0'}}; + +class RegionEndpointProviderTest : public Aws::Endpoint::DefaultEndpointProvider<> { + public: + using RegionEndpointResolveEndpointOutcome = Aws::Endpoint::ResolveEndpointOutcome; + + RegionEndpointProviderTest() : Aws::Endpoint::DefaultEndpointProvider<>(RegionRulesBlob.data(), RegionRulesBlobSize) {} + + ~RegionEndpointProviderTest() override = default; +}; -class TestServiceEndpointProviderTest : public Aws::Endpoint::DefaultEndpointProvider<> -{ +class TestServiceEndpointProviderTest : public Aws::Endpoint::DefaultEndpointProvider<> { public: using TestServiceResolveEndpointOutcome = Aws::Endpoint::ResolveEndpointOutcome; TestServiceEndpointProviderTest() - : Aws::Endpoint::DefaultEndpointProvider<>(EndpointParamTest::RulesBlob.data(), EndpointParamTest::RulesBlobSize) + : Aws::Endpoint::DefaultEndpointProvider<>(RulesBlob.data(), RulesBlobSize) {} ~TestServiceEndpointProviderTest() { } }; +} // namespace class EndpointTest : public Aws::Testing::AwsCppSdkGTestSuite { protected: @@ -82,3 +150,23 @@ TEST_F(EndpointTest, testStringArrayParamError) { EXPECT_FALSE(res.IsSuccess()); } + +TEST_F(EndpointTest, validRegionShouldBeUsed) { + RegionEndpointProviderTest regionEndpointProvider{}; + Aws::Client::ClientConfiguration configuration{}; + configuration.region = "us-east-1"; + regionEndpointProvider.InitBuiltInParameters(configuration); + const auto result = regionEndpointProvider.ResolveEndpoint({}); + EXPECT_TRUE(result.IsSuccess()); + EXPECT_EQ(result.GetResult().GetURL(), "https://tartarus.us-east-1.amazon.aws.com"); +} + +TEST_F(EndpointTest, invalidRegionShouldNotBeUsed) { + RegionEndpointProviderTest regionEndpointProvider{}; + Aws::Client::ClientConfiguration configuration{}; + configuration.region = "i-am-thou-thou-art-i/"; + regionEndpointProvider.InitBuiltInParameters(configuration); + const auto result = regionEndpointProvider.ResolveEndpoint({}); + EXPECT_FALSE(result.IsSuccess()); + EXPECT_EQ(result.GetError().GetErrorType(), Aws::Client::CoreErrors::INVALID_PARAMETER_COMBINATION); +}