Skip to content

Commit 566bd7d

Browse files
liyanzhang505huiguangjun
authored andcommitted
add endpoint verification.
1 parent 8b9ea69 commit 566bd7d

File tree

7 files changed

+142
-2
lines changed

7 files changed

+142
-2
lines changed

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ public synchronized void setEndpoint(String endpoint) {
245245
URI uri = toURI(endpoint);
246246
this.endpoint = uri;
247247

248+
OSSUtils.ensureEndpointValid(uri.getHost());
249+
248250
if (isIpOrLocalhost(uri)) {
249251
serviceClient.getClientConfiguration().setSLDEnabled(true);
250252
}

src/main/java/com/aliyun/oss/internal/OSSUtils.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ public class OSSUtils {
5353
public static final ResourceManager COMMON_RESOURCE_MANAGER = ResourceManager.getInstance(RESOURCE_NAME_COMMON);
5454

5555
private static final String BUCKET_NAMING_REGEX = "^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$";
56+
private static final String ENDPOINT_REGEX = "^[a-zA-Z0-9._-]+$";
57+
58+
/**
59+
* Validate endpoint.
60+
*/
61+
public static boolean validateEndpoint(String endpoint) {
62+
if (endpoint == null) {
63+
return false;
64+
}
65+
return endpoint.matches(ENDPOINT_REGEX);
66+
}
67+
68+
public static void ensureEndpointValid(String endpoint) {
69+
if (!validateEndpoint(endpoint)) {
70+
throw new IllegalArgumentException(
71+
OSS_RESOURCE_MANAGER.getFormattedString("EndpointInvalid", endpoint));
72+
}
73+
}
5674

5775
/**
5876
* Validate bucket name.
@@ -483,5 +501,4 @@ public static void checkChecksum(Long clientChecksum, Long serverChecksum, Strin
483501
throw new InconsistentException(clientChecksum, serverChecksum, requestId);
484502
}
485503
}
486-
487504
}

src/main/resources/oss.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ PartNumberMarkerOutOfRange=The part number mark should be in the range of [1-100
1919
PartNumberOutOfRange=The part number should be in the range of [1-10000].
2020
NoSuchKey=The specified key does not exist.
2121
FailedToParseResponse=Response cannot be recognized correctly : "{0}".
22+
EndpointInvalid=The endpoint "{0}" is invalid, please check it.

src/main/resources/oss_zh_CN.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ PartNumberMarkerOutOfRange=Part number marker\u5FC5\u987B\u57281-10000\u4E4B\u95
1919
PartNumberOutOfRange=Part number\u5FC5\u987B\u57281-10000\u4E4B\u95F4\uFF08\u95ED\u533A\u95F4\uFF09\u3002
2020
NoSuchKey=Key\u4E0D\u5B58\u5728\u3002
2121
FailedToParseResponse=\u8FD4\u56DE\u7ED3\u679C\u65E0\u6CD5\u6B63\u786E\u89E3\u6790\u3002 : "{0}"
22+
EndpointInvalid=The endpoint "{0}" is invalid, please check it.

src/test/java/com/aliyun/oss/OSSClientArgCheckTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.ByteArrayInputStream;
2727
import java.io.InputStream;
2828
import java.io.UnsupportedEncodingException;
29+
import java.net.MalformedURLException;
30+
import java.net.URL;
2931

3032
import org.junit.Assert;
3133
import org.junit.Test;
@@ -73,6 +75,47 @@ public void testValidBucketName() {
7375
Assert.assertFalse(OSSUtils.validateBucketName(bucketName + "\\dd"));
7476
}
7577

78+
@Test
79+
public void testValidEndpoint() {
80+
URL url1 = null;
81+
URL url2 = null;
82+
URL url3 = null;
83+
84+
try {
85+
url1 = new URL("https://www.test.com\\www.aliyun.com?x=123");
86+
url2 = new URL("http://www.test.com#www.aliyun.com?x=123");
87+
url3 = new URL("http://www.aliyun.com?x=123");
88+
} catch (MalformedURLException e) {
89+
e.printStackTrace();
90+
}
91+
92+
Assert.assertNotNull(url1);
93+
Assert.assertNotNull(url2);
94+
Assert.assertNotNull(url3);
95+
96+
Assert.assertFalse(OSSUtils.validateEndpoint(url1.getHost()));
97+
Assert.assertFalse(OSSUtils.validateEndpoint(url2.getHost()));
98+
Assert.assertTrue(OSSUtils.validateEndpoint(url3.getHost()));
99+
100+
Assert.assertTrue(OSSUtils.validateEndpoint("oss-cn-shenzhen.aliyuncs.com"));
101+
Assert.assertTrue(OSSUtils.validateEndpoint("abc_123"));
102+
Assert.assertTrue(OSSUtils.validateEndpoint("abc_123.adf-"));
103+
Assert.assertTrue(OSSUtils.validateEndpoint("192.168.1.1"));
104+
Assert.assertFalse(OSSUtils.validateEndpoint("www.test.com\\www.aliyun.com"));
105+
Assert.assertFalse(OSSUtils.validateEndpoint("www.test.com#www.aliyun.com"));
106+
107+
try {
108+
OSSUtils.ensureEndpointValid("www.test.com\\www.aliyun.com");
109+
Assert.fail("should not here.");
110+
}
111+
catch (IllegalArgumentException e) {
112+
Assert.assertTrue(true);
113+
}
114+
catch (Exception e) {
115+
Assert.fail(e.getMessage());
116+
}
117+
}
118+
76119
@Test
77120
public void testBucketArgChecking() throws Exception{
78121

src/test/java/com/aliyun/oss/common/comm/OSSClientTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.aliyun.oss.*;
3030
import com.aliyun.oss.internal.OSSConstants;
31+
import junit.framework.Assert;
3132
import org.junit.Ignore;
3233
import org.junit.Test;
3334
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
@@ -279,5 +280,67 @@ public void testDeprecationFunction() {
279280
client.isBucketExist("bucketName");
280281
} catch (Exception e){}
281282
}
283+
284+
@Test
285+
public void testValidateEndpoint() {
286+
final String endpoint = "oss-cn-shenzhen.aliyuncs.com";
287+
288+
// true
289+
try {
290+
OSS client = new OSSClientBuilder().build(endpoint, "id", "key");
291+
} catch (Exception e) {
292+
Assert.fail(e.getMessage());
293+
}
294+
295+
// true
296+
try {
297+
OSS client = new OSSClientBuilder().build("http://" + endpoint, "id", "key");
298+
} catch (Exception e) {
299+
Assert.fail(e.getMessage());
300+
}
301+
302+
// true
303+
try {
304+
OSS client = new OSSClientBuilder().build("https://" + endpoint, "id", "key");
305+
} catch (Exception e) {
306+
Assert.fail(e.getMessage());
307+
}
308+
309+
// true
310+
try {
311+
OSS client = new OSSClientBuilder().build("11.11.11.11", "id", "key");
312+
} catch (Exception e) {
313+
Assert.fail(e.getMessage());
314+
}
315+
316+
// true
317+
try {
318+
OSS client = new OSSClientBuilder().build("http://11.11.11.11", "id", "key");
319+
} catch (Exception e) {
320+
Assert.fail(e.getMessage());
321+
}
322+
323+
// true
324+
try {
325+
OSS client = new OSSClientBuilder().build("https://11.11.11.11", "id", "key");
326+
} catch (Exception e) {
327+
Assert.fail(e.getMessage());
328+
}
329+
330+
// false
331+
try {
332+
OSS client = new OSSClientBuilder().build("https://www.alibabacloud.com\\www.aliyun.com", "id", "key");
333+
Assert.fail("should be failed here.");
334+
} catch (IllegalArgumentException e) {
335+
}
336+
337+
// false
338+
try {
339+
OSS client = new OSSClientBuilder().build("https://www.alibabacloud.com#www.aliyun.com", "id", "key");
340+
Assert.fail("should be failed here.");
341+
} catch (IllegalArgumentException e) {
342+
}
343+
}
344+
282345
}
283346

src/test/java/com/aliyun/oss/integrationtests/ClientBuilderTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,20 @@ public void testClientBuilderSpecialEpochTicks() {
220220
} finally {
221221
client.shutdown();
222222
}
223+
}
223224

224-
225+
@Test
226+
public void testClientBuilderWithInvalidEndpoint() {
227+
OSSClient client = null;
228+
try {
229+
OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(
230+
"http://oss-cn-hangzhou.aliyuncs.com\\oss-cn-shenzhen.aliyuncs.com?test=123",
231+
new DefaultCredentialProvider(TestConfig.OSS_TEST_ACCESS_KEY_ID,
232+
TestConfig.OSS_TEST_ACCESS_KEY_SECRET),
233+
new ClientBuilderConfiguration());
234+
Assert.fail("should not here.");
235+
} catch (Exception e) {
236+
Assert.assertTrue(true);
237+
}
225238
}
226239
}

0 commit comments

Comments
 (0)