Skip to content

Commit 8ee5634

Browse files
authored
Add additional path traversal tests to protect against regressions. (#5133)
1 parent 9fe1db8 commit 8ee5634

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.services.s3;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.BeforeClass;
21+
import org.junit.Test;
22+
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
23+
import software.amazon.awssdk.regions.Region;
24+
import software.amazon.awssdk.testutils.service.http.MockHttpClient;
25+
import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient;
26+
27+
public class PathTraversalTest {
28+
private static S3Client client;
29+
private static MockSyncHttpClient httpClient;
30+
31+
@BeforeClass
32+
public static void setup() {
33+
httpClient = new MockSyncHttpClient();
34+
client = S3Client.builder()
35+
.region(Region.US_WEST_2)
36+
.credentialsProvider(AnonymousCredentialsProvider.create())
37+
.httpClient(httpClient)
38+
.build();
39+
}
40+
41+
@Test
42+
public void clientPreservesLeadingDotSegmentInUriLabel() {
43+
httpClient.stubNextResponse200();
44+
client.getObjectAsBytes(r -> r.bucket("mybucket").key("../key.txt"));
45+
assertThat(httpClient.getLastRequest().encodedPath()).isEqualTo("/../key.txt");
46+
}
47+
48+
@Test
49+
public void clientPreservesEmbeddedDotSegmentInUriLabel() {
50+
httpClient.stubNextResponse200();
51+
client.getObjectAsBytes(r -> r.bucket("mybucket").key("foo/../key.txt"));
52+
assertThat(httpClient.getLastRequest().encodedPath()).isEqualTo("/foo/../key.txt");
53+
}
54+
}

0 commit comments

Comments
 (0)