diff --git a/src/main/java/io/floci/aws/core/common/ResteasyRequests.java b/src/main/java/io/floci/aws/core/common/ResteasyRequests.java index 304f47ff3d..7272832d75 100644 --- a/src/main/java/io/floci/aws/core/common/ResteasyRequests.java +++ b/src/main/java/io/floci/aws/core/common/ResteasyRequests.java @@ -45,6 +45,12 @@ public final class ResteasyRequests { return request != null ? queryParameters(request) : uriInfo.getQueryParameters(); } + /** The query string as the client sent it, read without building the absolute request URI. */ + public static String rawQuery(UriInfo uriInfo) { + ResteasyReactiveRequestContext request = CurrentRequestManager.get(); + return request != null ? request.serverRequest().query() : uriInfo.getRequestUri().getRawQuery(); + } + private static MultivaluedMap queryParameters(ResteasyReactiveRequestContext request) { if (request.serverRequest().query() == null) { return NO_QUERY; diff --git a/src/main/java/io/floci/aws/services/s3/S3QueryParams.java b/src/main/java/io/floci/aws/services/s3/S3QueryParams.java index 14f9327bf6..ec0f689877 100644 --- a/src/main/java/io/floci/aws/services/s3/S3QueryParams.java +++ b/src/main/java/io/floci/aws/services/s3/S3QueryParams.java @@ -8,13 +8,11 @@ import jakarta.ws.rs.core.UriInfo; final class S3QueryParams { private final MultivaluedMap decoded; - private final UriInfo uriInfo; - private String rawQuery; - private boolean rawQueryRead; + private final String rawQuery; S3QueryParams(UriInfo uriInfo) { - this.uriInfo = uriInfo; this.decoded = ResteasyRequests.queryParameters(uriInfo); + this.rawQuery = ResteasyRequests.rawQuery(uriInfo); } MultivaluedMap decoded() { @@ -22,18 +20,10 @@ final class S3QueryParams { } boolean has(String name) { - return decoded.containsKey(name) || S3RequestParser.hasQueryParamInString(rawQuery(), name); + return decoded.containsKey(name) || S3RequestParser.hasQueryParamInString(rawQuery, name); } String first(String name) { return decoded.getFirst(name); } - - private String rawQuery() { - if (!rawQueryRead) { - rawQuery = uriInfo.getRequestUri().getQuery(); - rawQueryRead = true; - } - return rawQuery; - } } diff --git a/src/main/java/io/floci/aws/services/s3/S3RequestParser.java b/src/main/java/io/floci/aws/services/s3/S3RequestParser.java index 22dcc88e80..13347cf8c1 100644 --- a/src/main/java/io/floci/aws/services/s3/S3RequestParser.java +++ b/src/main/java/io/floci/aws/services/s3/S3RequestParser.java @@ -3,6 +3,9 @@ package io.floci.aws.services.s3; import io.floci.aws.core.common.ResteasyRequests; import jakarta.ws.rs.core.UriInfo; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + /** * Package-private utility for parsing S3 request elements. * Methods are static and stateless for direct testability. @@ -15,25 +18,15 @@ final class S3RequestParser { private S3RequestParser() { } - /** - * Returns {@code true} if the request URI contains a query parameter with the given name. - * Checks {@link UriInfo#getQueryParameters()} first; if that does not contain the param, - * falls back to parsing the raw query string by splitting on {@code &} and extracting - * the parameter name (everything before the first {@code =}). - * - *

This avoids false positives from substring matches inside parameter values - * (e.g. {@code X-Amz-SignedHeaders=host%3Bx-amz-tagging} would falsely match - * {@code "tagging"} with naive {@code String.contains()}). - */ + /** Returns {@code true} if the decoded query parameters, then the raw query names, contain the given name. */ static boolean hasQueryParam(UriInfo uriInfo, String param) { - if (ResteasyRequests.queryParameters(uriInfo).containsKey(param)) return true; - String query = uriInfo.getRequestUri().getQuery(); - return hasQueryParamInString(query, param); + return ResteasyRequests.queryParameters(uriInfo).containsKey(param) + || hasQueryParamInString(ResteasyRequests.rawQuery(uriInfo), param); } /** - * Direct string-based variant for testing without UriInfo mocking. - * Package-private so unit tests in the same package can call it. + * Returns {@code true} if a name in the raw query string equals the given one after decoding. + * The query is split before decoding, so an encoded {@code &} inside a value never starts a name. */ static boolean hasQueryParamInString(String query, String param) { if (query == null) { @@ -48,11 +41,34 @@ final class S3RequestParser { } int eq = query.indexOf('=', start, end); int nameEnd = eq >= 0 ? eq : end; - if (nameEnd - start == param.length() && query.startsWith(param, start)) { + if (isEncoded(query, start, nameEnd)) { + if (decodedNameEquals(query.substring(start, nameEnd), param)) { + return true; + } + } else if (nameEnd - start == param.length() && query.startsWith(param, start)) { return true; } start = end + 1; } return false; } + + private static boolean isEncoded(String query, int start, int end) { + for (int i = start; i < end; i++) { + char c = query.charAt(i); + if (c == '%' || c == '+') { + return true; + } + } + return false; + } + + private static boolean decodedNameEquals(String name, String param) { + try { + return URLDecoder.decode(name, StandardCharsets.UTF_8).equals(param); + } catch (IllegalArgumentException ignored) { + // A name with a malformed escape matches no parameter. + return false; + } + } } diff --git a/src/test/java/io/floci/aws/core/common/ResteasyRequestsTest.java b/src/test/java/io/floci/aws/core/common/ResteasyRequestsTest.java index 299083a85f..17bea31998 100644 --- a/src/test/java/io/floci/aws/core/common/ResteasyRequestsTest.java +++ b/src/test/java/io/floci/aws/core/common/ResteasyRequestsTest.java @@ -10,9 +10,11 @@ import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestCo import org.jboss.resteasy.reactive.server.spi.ServerHttpRequest; import org.junit.jupiter.api.Test; +import java.net.URI; import java.util.HashMap; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -129,4 +131,24 @@ class ResteasyRequestsTest { CurrentRequestManager.set(null); } } + + @Test + void rawQueryInsideARequestReadsTheServerRequestWithoutTheRequestUri() { + UriInfo uriInfo = mock(UriInfo.class); + CurrentRequestManager.set(requestContext("a=b%26c", uriInfo)); + try { + assertEquals("a=b%26c", ResteasyRequests.rawQuery(uriInfo)); + verify(uriInfo, never()).getRequestUri(); + } finally { + CurrentRequestManager.set(null); + } + } + + @Test + void rawQueryOutsideARequestReadsTheRequestUri() { + UriInfo uriInfo = mock(UriInfo.class); + when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost/b?a=b%26c")); + + assertEquals("a=b%26c", ResteasyRequests.rawQuery(uriInfo)); + } } diff --git a/src/test/java/io/floci/aws/services/s3/S3PresignedPutTaggingIntegrationTest.java b/src/test/java/io/floci/aws/services/s3/S3PresignedPutTaggingIntegrationTest.java index d1c383617a..0151923143 100644 --- a/src/test/java/io/floci/aws/services/s3/S3PresignedPutTaggingIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3PresignedPutTaggingIntegrationTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import java.net.URI; import java.util.Arrays; import java.util.stream.Collectors; +import java.util.stream.IntStream; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; @@ -31,11 +32,9 @@ class S3PresignedPutTaggingIntegrationTest { * includes {@code x-amz-tagging} must NOT be misrouted to the * {@code ?tagging} sub-resource handler (PutObjectTagging). * - *

The fix was in {@link S3RequestParser#hasQueryParamInString}: the old - * {@code String.contains()} implementation falsely matched "tagging" inside the + *

The old {@code String.contains()} check matched "tagging" inside the * value of {@code X-Amz-SignedHeaders} (e.g. - * {@code host%3Bx-amz-tagging}), causing a 404. The new implementation correctly - * splits on {@code &} and extracts parameter names before {@code =}. + * {@code host%3Bx-amz-tagging}), causing a 404. */ @Test void presignedPutWithTaggingSignedHeaderDoesNotMisrouteToPutObjectTagging() { @@ -202,6 +201,74 @@ class S3PresignedPutTaggingIntegrationTest { .body(containsString("d")); } + @Test + void listObjectsV2PrefixWithAnEncodedAmpersandListsObjects() { + String bucket = newBucket("encoded-amp-v2-"); + given().urlEncodingEnabled(false).body("x").when().put("/" + bucket + "/a&tagging/x").then().statusCode(200); + given().body("y").when().put("/" + bucket + "/b/y").then().statusCode(200); + + given() + .urlEncodingEnabled(false) + .when() + .get("/" + bucket + "?list-type=2&prefix=a%26tagging") + .then() + .statusCode(200) + .body(containsString("NoSuchTagSet")); + } + + @Test + void taggingAfter1024OtherParametersReadsBucketTagging() { + String bucket = newBucket("many-params-"); + // Two-character names keep the request line under the 4096 bytes other hosts are allowed. + String others = IntStream.range(0, 1025) + .mapToObj(i -> Integer.toString(i + 36, 36)) + .collect(Collectors.joining("&")); + + given() + .urlEncodingEnabled(false) + .when() + .get("/" + bucket + "?" + others + "&tagging") + .then() + .statusCode(404) + .body(containsString("NoSuchTagSet")); + } + + private static String newBucket(String prefix) { + String bucket = prefix + System.nanoTime(); + given().when().put("/" + bucket).then().statusCode(200); + return bucket; + } + private static void createBucket() { given() .when() diff --git a/src/test/java/io/floci/aws/services/s3/S3ServiceTaggingTest.java b/src/test/java/io/floci/aws/services/s3/S3ServiceTaggingTest.java index 2a8c7e7923..34e8e54a53 100644 --- a/src/test/java/io/floci/aws/services/s3/S3ServiceTaggingTest.java +++ b/src/test/java/io/floci/aws/services/s3/S3ServiceTaggingTest.java @@ -71,4 +71,26 @@ class S3ServiceTaggingTest { assertFalse(S3RequestParser.hasQueryParamInString(query, "tagging")); assertFalse(S3RequestParser.hasQueryParamInString(query, "acl")); } + + @Test + void hasQueryParamInString_encodedAmpersandInValue_isNoMatch() { + assertFalse(S3RequestParser.hasQueryParamInString("prefix=a%26tagging", "tagging")); + } + + @Test + void hasQueryParamInString_caseVariantBeforeName_matchesOnlyExactCase() { + assertTrue(S3RequestParser.hasQueryParamInString("Tagging&tagging", "tagging")); + assertFalse(S3RequestParser.hasQueryParamInString("Tagging", "tagging")); + } + + @Test + void hasQueryParamInString_encodedName_matches() { + assertTrue(S3RequestParser.hasQueryParamInString("%74agging", "tagging")); + } + + @Test + void hasQueryParamInString_malformedEscape_isNoMatchAndDoesNotThrow() { + assertTrue(S3RequestParser.hasQueryParamInString("%zz&tagging", "tagging")); + assertFalse(S3RequestParser.hasQueryParamInString("%zz", "tagging")); + } }