Skip to content

Commit f7bfd6e

Browse files
committed
add endpoint check.
1 parent 9b01520 commit f7bfd6e

File tree

7 files changed

+92
-2
lines changed

7 files changed

+92
-2
lines changed

sdk/src/OssClientImpl.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ OssClientImpl::OssClientImpl(const std::string &endpoint, const std::shared_ptr<
4848
endpoint_(endpoint),
4949
credentialsProvider_(credentialsProvider),
5050
signer_(std::make_shared<HmacSha1Signer>()),
51-
executor_(configuration.executor ? configuration.executor :std::make_shared<ThreadExecutor>())
51+
executor_(configuration.executor ? configuration.executor :std::make_shared<ThreadExecutor>()),
52+
isValidEndpoint_(IsValidEndpoint(endpoint))
5253
{
5354
}
5455

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

323+
if (!isValidEndpoint_) {
324+
return OssOutcome(OssError("ValidateError", "The endpoint is invalid."));
325+
}
326+
322327
auto outcome = BASE::AttemptRequest(endpoint_, request, method);
323328
if (outcome.isSuccess()) {
324329
return OssOutcome(buildResult(request, outcome.result()));

sdk/src/OssClientImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ namespace OSS
189189
std::shared_ptr<CredentialsProvider> credentialsProvider_;
190190
std::shared_ptr<Signer> signer_;
191191
std::shared_ptr<Executor> executor_;
192+
bool isValidEndpoint_;
192193
};
193194
}
194195
}

sdk/src/utils/Utils.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,27 @@ bool AlibabaCloud::OSS::IsValidBucketName(const std::string &bucketName)
658658
return value.size() <= TagValueLengthLimit;
659659
}
660660

661+
bool AlibabaCloud::OSS::IsValidEndpoint(const std::string &value)
662+
{
663+
auto host = Url(value).host();
664+
665+
if (host.empty())
666+
return false;
667+
668+
for (const auto c : host) {
669+
if (!((c >= 'a' && c <= 'z') ||
670+
(c >= '0' && c <= '9') ||
671+
(c >= 'A' && c <= 'Z') ||
672+
(c == '_') ||
673+
(c == '-') ||
674+
(c == '.'))) {
675+
return false;
676+
}
677+
}
678+
679+
return true;
680+
}
681+
661682
const std::string& AlibabaCloud::OSS::LookupMimeType(const std::string &name)
662683
{
663684
const static std::map<std::string, std::string> mimeType = {

sdk/src/utils/Utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ namespace OSS
7171
bool IsValidPlayListName(const std::string &playListName);
7272
bool IsValidTagKey(const std::string &key);
7373
bool IsValidTagValue(const std::string &value);
74+
bool IsValidEndpoint(const std::string &value);
7475

7576
const std::string &LookupMimeType(const std::string& name);
7677
std::string CombineHostString(const std::string &endpoint, const std::string &bucket, bool isCname);

test/src/AccessKey/AccessKeyTest.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,53 @@ TEST_F(AccessKeyTest, GenerateRTMPSignatureUrlCredentialsProviderTest)
177177
#endif
178178
}
179179

180+
TEST_F(AccessKeyTest, EndpointTest)
181+
{
182+
ClientConfiguration conf;
183+
auto request = ListBucketsRequest();
184+
request.setMaxKeys(1);
185+
186+
auto endpoint = Config::Endpoint;
187+
auto client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
188+
auto outcome = client->ListBuckets(request);
189+
EXPECT_EQ(outcome.isSuccess(), true);
190+
191+
endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
192+
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
193+
outcome = client->ListBuckets(request);
194+
EXPECT_EQ(outcome.isSuccess(), true);
195+
196+
endpoint = "http://oss-cn-hangzhou.aliyuncs.com:80";
197+
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
198+
outcome = client->ListBuckets(request);
199+
EXPECT_EQ(outcome.isSuccess(), true);
200+
201+
endpoint = "http://oss-cn-hangzhou.aliyuncs.com:80/?test=123";
202+
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
203+
outcome = client->ListBuckets(request);
204+
EXPECT_EQ(outcome.isSuccess(), true);
205+
206+
endpoint = "www.test-inc.com\\oss-cn-hangzhou.aliyuncs.com";
207+
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
208+
outcome = client->ListBuckets(request);
209+
EXPECT_EQ(outcome.isSuccess(), false);
210+
EXPECT_EQ(outcome.error().Code(), "ValidateError");
211+
EXPECT_EQ(outcome.error().Message(), "The endpoint is invalid.");
212+
213+
endpoint = "www.test-inc*test.com";
214+
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
215+
outcome = client->ListBuckets(request);
216+
EXPECT_EQ(outcome.isSuccess(), false);
217+
EXPECT_EQ(outcome.error().Code(), "ValidateError");
218+
EXPECT_EQ(outcome.error().Message(), "The endpoint is invalid.");
219+
220+
endpoint = "";
221+
client = std::make_shared<OssClient>(endpoint, Config::AccessKeyId, Config::AccessKeySecret, conf);
222+
outcome = client->ListBuckets(request);
223+
EXPECT_EQ(outcome.isSuccess(), false);
224+
EXPECT_EQ(outcome.error().Code(), "ValidateError");
225+
EXPECT_EQ(outcome.error().Message(), "The endpoint is invalid.");
226+
}
227+
180228
}
181229
}

test/src/Object/ObjectBasicOperationTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ TEST_F(ObjectBasicOperationTest, ListObjectsV2Test)
18071807
EXPECT_EQ(obj.Size(), 100LL);
18081808
i++;
18091809
}
1810-
EXPECT_EQ(i, 23UL);
1810+
EXPECT_EQ(i, 23L);
18111811

18121812
//list object with prefix
18131813
request = ListObjectsV2Request(bucketName);

test/src/Other/UtilsFunctionTest.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,5 +1004,19 @@ TEST_F(UtilsFunctionTest, MapToJsonStringTest)
10041004
EXPECT_EQ(json1, json2);
10051005
}
10061006

1007+
TEST_F(UtilsFunctionTest, IsValidEndpointTest)
1008+
{
1009+
EXPECT_EQ(IsValidEndpoint("192.168.1.1"), true);
1010+
EXPECT_EQ(IsValidEndpoint("192.168.1.1:80"), true);
1011+
EXPECT_EQ(IsValidEndpoint("www.test-inc.com"), true);
1012+
EXPECT_EQ(IsValidEndpoint("WWW.test-inc_CN.com"), true);
1013+
EXPECT_EQ(IsValidEndpoint("http://www.test-inc.com"), true);
1014+
EXPECT_EQ(IsValidEndpoint("http://www.test-inc_test.com:80"), true);
1015+
EXPECT_EQ(IsValidEndpoint("https://www.test-inc_test.com:80/test?123=x"), true);
1016+
EXPECT_EQ(IsValidEndpoint("www.test-inc*test.com"), false);
1017+
EXPECT_EQ(IsValidEndpoint("www.test-inc.com\\oss-cn-hangzhou.aliyuncs.com"), false);
1018+
EXPECT_EQ(IsValidEndpoint(""), false);
1019+
}
1020+
10071021
}
10081022
}

0 commit comments

Comments
 (0)