mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(s3): escape a carriage return in XML responses so a key keeps it (#6598)
* fix(s3): escape a carriage return in XML responses so a key keeps it * test(s3): share one path-style S3 client between the S3 tests
This commit is contained in:
1 parent
b280a7764a
commit
2fd74eaf08
4 files changed
+72
-18
No files matched your search
@@ -83,7 +83,8 @@ public final class XmlBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes the five XML special characters: {@code & < > " '}.
|
||||
* Escapes the five XML special characters: {@code & < > " '}, and a carriage return as
|
||||
* {@code }, which an XML parser would otherwise read back as a line feed.
|
||||
* Returns an empty string for null or empty input.
|
||||
*/
|
||||
public static String escape(String s) {
|
||||
@@ -100,6 +101,7 @@ public final class XmlBuilder {
|
||||
case '>' -> ">";
|
||||
case '"' -> """;
|
||||
case '\'' -> "'";
|
||||
case '\r' -> " ";
|
||||
default -> null;
|
||||
};
|
||||
if (replacement != null) {
|
||||
|
||||
+2
-17
@@ -1,21 +1,16 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.S3TestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.Bucket;
|
||||
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@@ -53,7 +48,7 @@ class S3ListBucketsRegionIntegrationTest {
|
||||
String prefix = "s3-region-6241-" + UUID.randomUUID();
|
||||
String defaultBucket = prefix + "-us";
|
||||
String explicitBucket = prefix + "-eu";
|
||||
try (S3Client client = client()) {
|
||||
try (S3Client client = S3TestClients.pathStyle()) {
|
||||
client.createBucket(request -> request.bucket(defaultBucket));
|
||||
try {
|
||||
client.createBucket(request -> request.bucket(explicitBucket)
|
||||
@@ -102,14 +97,4 @@ class S3ListBucketsRegionIntegrationTest {
|
||||
given().delete("/" + bucket).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
private static S3Client client() {
|
||||
return S3Client.builder()
|
||||
.endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder())
|
||||
.forcePathStyle(true)
|
||||
.region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("741624100001", "test")))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.testing.S3TestClients;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
|
||||
import software.amazon.awssdk.services.s3.model.S3Object;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
class S3XmlKeyEscapingIntegrationTest {
|
||||
|
||||
@Test
|
||||
void aKeyWithACarriageReturnIsListedUnchanged() {
|
||||
String bucket = "s3-xml-key-cr-" + UUID.randomUUID();
|
||||
String key = "line\rbreak.txt";
|
||||
try (S3Client client = S3TestClients.pathStyle()) {
|
||||
client.createBucket(request -> request.bucket(bucket));
|
||||
try {
|
||||
client.putObject(request -> request.bucket(bucket).key(key), RequestBody.fromString("x"));
|
||||
try {
|
||||
ListObjectsV2Response response = client.listObjectsV2(request -> request.bucket(bucket));
|
||||
List<String> keys = response.contents().stream().map(S3Object::key).toList();
|
||||
assertEquals(List.of(key), keys);
|
||||
} finally {
|
||||
client.deleteObject(request -> request.bucket(bucket).key(key));
|
||||
}
|
||||
} finally {
|
||||
client.deleteBucket(request -> request.bucket(bucket));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.github.hectorvent.floci.testing;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/** AWS SDK S3 clients pointed at the Floci instance a {@code @QuarkusTest} runs. */
|
||||
public final class S3TestClients {
|
||||
|
||||
private S3TestClients() {
|
||||
}
|
||||
|
||||
/** A path-style client in us-east-1 for account {@code 741624100001}. */
|
||||
public static S3Client pathStyle() {
|
||||
return S3Client.builder()
|
||||
.endpointOverride(URI.create("http://localhost:" + RestAssured.port))
|
||||
.httpClientBuilder(UrlConnectionHttpClient.builder())
|
||||
.forcePathStyle(true)
|
||||
.region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("741624100001", "test")))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user