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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user