Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): escape a carriage return in XML responses so a key keeps it (#6598)
Niels Kootstra2fd74eaf0852
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/XmlBuilder.java b/src/main/java/io/github/hectorvent/floci/core/common/XmlBuilder.javaindex c42937a853..a2c3c88119 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/XmlBuilder.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/XmlBuilder.java@@ -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) {diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ListBucketsRegionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ListBucketsRegionIntegrationTest.javaindex 2fa1e280b8..16d91949e1 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ListBucketsRegionIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ListBucketsRegionIntegrationTest.java@@ -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();- }}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3XmlKeyEscapingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3XmlKeyEscapingIntegrationTest.javanew file mode 100644index 0000000000..4c4eeec2fb--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3XmlKeyEscapingIntegrationTest.java@@ -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));+ }+ }+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/testing/S3TestClients.java b/src/test/java/io/github/hectorvent/floci/testing/S3TestClients.javanew file mode 100644index 0000000000..28b5e23612--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/testing/S3TestClients.java@@ -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();+ }+}