Skip to content

Commit

Permalink
add endpoint check.
Browse files Browse the repository at this point in the history
  • Loading branch information
huiguangjun committed Aug 19, 2020
1 parent 9b01520 commit f7bfd6e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
7 changes: 6 additions & 1 deletion sdk/src/OssClientImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ OssClientImpl::OssClientImpl(const std::string &endpoint, const std::shared_ptr<
endpoint_(endpoint),
credentialsProvider_(credentialsProvider),
signer_(std::make_shared<HmacSha1Signer>()),
executor_(configuration.executor ? configuration.executor :std::make_shared<ThreadExecutor>())
executor_(configuration.executor ? configuration.executor :std::make_shared<ThreadExecutor>()),
isValidEndpoint_(IsValidEndpoint(endpoint))
{
}

Expand Down Expand Up @@ -319,6 +320,10 @@ OssOutcome OssClientImpl::MakeRequest(const OssRequest &request, Http::Method me
return OssOutcome(OssError("ValidateError", request.validateMessage(ret)));
}

if (!isValidEndpoint_) {
return OssOutcome(OssError("ValidateError", "The endpoint is invalid."));
}

auto outcome = BASE::AttemptRequest(endpoint_, request, method);
if (outcome.isSuccess()) {
return OssOutcome(buildResult(request, outcome.result()));
Expand Down
1 change: 1 addition & 0 deletions sdk/src/OssClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ namespace OSS
std::shared_ptr<CredentialsProvider> credentialsProvider_;
std::shared_ptr<Signer> signer_;
std::shared_ptr<Executor> executor_;
bool isValidEndpoint_;
};
}
}
Expand Down
21 changes: 21 additions & 0 deletions sdk/src/utils/Utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,27 @@ bool AlibabaCloud::OSS::IsValidBucketName(const std::string &bucketName)
return value.size() <= TagValueLengthLimit;
}

bool AlibabaCloud::OSS::IsValidEndpoint(const std::string &value)
{
auto host = Url(value).host();

if (host.empty())
return false;

for (const auto c : host) {
if (!((c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c == '_') ||
(c == '-') ||
(c == '.'))) {
return false;
}
}

return true;
}

const std::string& AlibabaCloud::OSS::LookupMimeType(const std::string &name)
{
const static std::map<std::string, std::string> mimeType = {
Expand Down
1 change: 1 addition & 0 deletions sdk/src/utils/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace OSS
bool IsValidPlayListName(const std::string &playListName);
bool IsValidTagKey(const std::string &key);
bool IsValidTagValue(const std::string &value);
bool IsValidEndpoint(const std::string &value);

const std::string &LookupMimeType(const std::string& name);
std::string CombineHostString(const std::string &endpoint, const std::string &bucket, bool isCname);
Expand Down
48 changes: 48 additions & 0 deletions test/src/AccessKey/AccessKeyTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,53 @@ TEST_F(AccessKeyTest, GenerateRTMPSignatureUrlCredentialsProviderTest)
#endif
}

TEST_F(AccessKeyTest, EndpointTest)
{
ClientConfiguration conf;
auto request = ListBucketsRequest();
request.setMaxKeys(1);

auto endpoint = Config::Endpoint;
auto client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
auto outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), true);

endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), true);

endpoint = "http://oss-cn-hangzhou.aliyuncs.com:80";
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), true);

endpoint = "http://oss-cn-hangzhou.aliyuncs.com:80/?test=123";
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), true);

endpoint = "www.test-inc.com\\oss-cn-hangzhou.aliyuncs.com";
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), false);
EXPECT_EQ(outcome.error().Code(), "ValidateError");
EXPECT_EQ(outcome.error().Message(), "The endpoint is invalid.");

endpoint = "www.test-inc*test.com";
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), false);
EXPECT_EQ(outcome.error().Code(), "ValidateError");
EXPECT_EQ(outcome.error().Message(), "The endpoint is invalid.");

endpoint = "";
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
outcome = client->ListBuckets(request);
EXPECT_EQ(outcome.isSuccess(), false);
EXPECT_EQ(outcome.error().Code(), "ValidateError");
EXPECT_EQ(outcome.error().Message(), "The endpoint is invalid.");
}

}
}
2 changes: 1 addition & 1 deletion test/src/Object/ObjectBasicOperationTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ TEST_F(ObjectBasicOperationTest, ListObjectsV2Test)
EXPECT_EQ(obj.Size(), 100LL);
i++;
}
EXPECT_EQ(i, 23UL);
EXPECT_EQ(i, 23L);

//list object with prefix
request = ListObjectsV2Request(bucketName);
Expand Down
14 changes: 14 additions & 0 deletions test/src/Other/UtilsFunctionTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1004,5 +1004,19 @@ TEST_F(UtilsFunctionTest, MapToJsonStringTest)
EXPECT_EQ(json1, json2);
}

TEST_F(UtilsFunctionTest, IsValidEndpointTest)
{
EXPECT_EQ(IsValidEndpoint("192.168.1.1"), true);
EXPECT_EQ(IsValidEndpoint("192.168.1.1:80"), true);
EXPECT_EQ(IsValidEndpoint("www.test-inc.com"), true);
EXPECT_EQ(IsValidEndpoint("WWW.test-inc_CN.com"), true);
EXPECT_EQ(IsValidEndpoint("http://www.test-inc.com"), true);
EXPECT_EQ(IsValidEndpoint("http://www.test-inc_test.com:80"), true);
EXPECT_EQ(IsValidEndpoint("https://www.test-inc_test.com:80/test?123=x"), true);
EXPECT_EQ(IsValidEndpoint("www.test-inc*test.com"), false);
EXPECT_EQ(IsValidEndpoint("www.test-inc.com\\oss-cn-hangzhou.aliyuncs.com"), false);
EXPECT_EQ(IsValidEndpoint(""), false);
}

}
}

0 comments on commit f7bfd6e

Please sign in to comment.