mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(s3): stop splitting query values on an encoded & (#8706)
* fix(s3): stop treating an encoded & in a query value as a subresource * fix(s3): split the raw query to find a name the decoded map lost
This commit is contained in:
1 parent
4c0181cd20
commit
93fd5690e8
6 files changed
+156
-33
No files matched your search
@@ -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<String, String> queryParameters(ResteasyReactiveRequestContext request) {
|
||||
if (request.serverRequest().query() == null) {
|
||||
return NO_QUERY;
|
||||
|
||||
@@ -8,13 +8,11 @@ import jakarta.ws.rs.core.UriInfo;
|
||||
final class S3QueryParams {
|
||||
|
||||
private final MultivaluedMap<String, String> 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<String, String> 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;
|
||||
}
|
||||
}
|
||||
@@ -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 =}).
|
||||
*
|
||||
* <p>This avoids false positives from substring matches inside parameter <em>values</em>
|
||||
* (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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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).
|
||||
*
|
||||
* <p>The fix was in {@link S3RequestParser#hasQueryParamInString}: the old
|
||||
* {@code String.contains()} implementation falsely matched "tagging" inside the
|
||||
* <p>The old {@code String.contains()} check matched "tagging" inside the
|
||||
* <em>value</em> 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("<Value>d</Value>"));
|
||||
}
|
||||
|
||||
@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("<ListBucketResult"))
|
||||
.body("ListBucketResult.Prefix", equalTo("a&tagging"))
|
||||
.body("ListBucketResult.Contents.size()", equalTo(1))
|
||||
.body("ListBucketResult.Contents.Key", equalTo("a&tagging/x"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listObjectsPrefixWithAnEncodedAmpersandListsObjects() {
|
||||
String bucket = newBucket("encoded-amp-v1-");
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get("/" + bucket + "?prefix=a%26uploads")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<ListBucketResult"))
|
||||
.body("ListBucketResult.Prefix", equalTo("a&uploads"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void caseVariantBeforeTaggingReadsBucketTagging() {
|
||||
String bucket = newBucket("case-variant-");
|
||||
|
||||
given()
|
||||
.urlEncodingEnabled(false)
|
||||
.when()
|
||||
.get("/" + bucket + "?Tagging&tagging")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body(containsString("<Code>NoSuchTagSet</Code>"));
|
||||
}
|
||||
|
||||
@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("<Code>NoSuchTagSet</Code>"));
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user