fix(s3): avoid misrouting presigned PUTs with tagging (#932) (#995)

This commit is contained in:
nblomquist authored and GitHub committed 2026-05-23 22:33:30 -05:00
1 parent f5dba627be
commit 894b39f446
5 files changed
+337 -7

No files matched your search

@@ -0,0 +1,82 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.util.Arrays;
import java.util.stream.Collectors;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
class S3PresignedPutTaggingIntegrationTest {
private static final String BUCKET = "presigned-tagging-bucket";
@Inject
PreSignedUrlGenerator presignGenerator;
/**
* Regression test for #932: a presigned PUT URL whose {@code X-Amz-SignedHeaders}
* 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
* <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 =}.
*/
@Test
void presignedPutWithTaggingSignedHeaderDoesNotMisrouteToPutObjectTagging() {
given()
.when()
.put("/" + BUCKET)
.then()
.statusCode(200);
int port = io.restassured.RestAssured.port;
String fullBaseUrl = "http://localhost:" + port;
String presignedUrl = presignGenerator.generatePresignedUrl(
fullBaseUrl, BUCKET, "tagged-via-presign.txt", "PUT", 3600);
URI uri = URI.create(presignedUrl);
String rawQuery = uri.getRawQuery();
assertTrue(rawQuery.contains("X-Amz-SignedHeaders=host"),
"expected generated presigned URL to include X-Amz-SignedHeaders=host");
// Add x-amz-tagging to X-Amz-SignedHeaders by properly parsing and reconstructing
// the query string — avoids the fragility of raw String.replace().
String taggingSignedHeadersQuery = Arrays.stream(rawQuery.split("&"))
.map(pair -> {
if (pair.startsWith("X-Amz-SignedHeaders=")) {
return pair + "%3Bx-amz-tagging";
}
return pair;
})
.collect(Collectors.joining("&"));
given()
.urlEncodingEnabled(false)
.contentType("text/plain")
.header("x-amz-tagging", "tag=test")
.body("uploaded via presigned PUT with tagging")
.when()
.put(uri.getRawPath() + "?" + taggingSignedHeadersQuery)
.then()
.statusCode(200)
.header("ETag", notNullValue());
given()
.when()
.get("/" + BUCKET + "/tagged-via-presign.txt")
.then()
.statusCode(200)
.body(equalTo("uploaded via presigned PUT with tagging"));
}
}
@@ -0,0 +1,74 @@
package io.github.hectorvent.floci.services.s3;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
/**
* Tests for S3 tagging: service-layer behavior and {@link S3RequestParser} utility methods.
*
* <p>Phase 1 (#932): tests 10+ cover {@code hasQueryParam} false-positive fix.
* Phase 2 (#986): tests 1-9 cover inline tagging header parsing and persistence.
*/
class S3ServiceTaggingTest {
// ========== Phase 2: Inline tagging header parsing ==========
// (Tests 1-9 will be added in Phase 2 — fix/s3-putobject-inline-tagging)
// ========== Phase 1: hasQueryParam false-positive fix ==========
@Test
void hasQueryParamReturnsTrueForExactParamName() {
assertTrue(S3RequestParser.hasQueryParamInString("tagging", "tagging"));
}
@Test
void hasQueryParamReturnsTrueForParamWithValue() {
assertTrue(S3RequestParser.hasQueryParamInString("tagging=", "tagging"));
}
@Test
void hasQueryParamReturnsTrueForParamAmongOthers() {
assertTrue(S3RequestParser.hasQueryParamInString(
"versionId=abc&tagging&acl", "tagging"));
}
@Test
void hasQueryParamReturnsFalseForNullQuery() {
assertFalse(S3RequestParser.hasQueryParamInString(null, "tagging"));
}
@Test
void hasQueryParamReturnsFalseForEmptyQuery() {
assertFalse(S3RequestParser.hasQueryParamInString("", "tagging"));
}
@Test
void hasQueryParamNoFalsePositiveOnValueContainingParamName() {
// This is the actual #932 bug: X-Amz-SignedHeaders value contains "x-amz-tagging"
// but there is no "tagging" query parameter. The old code would falsely match.
String query = "X-Amz-SignedHeaders=host%3Bx-amz-tagging&X-Amz-Algorithm=AWS4-HMAC-SHA256";
assertFalse(S3RequestParser.hasQueryParamInString(query, "tagging"));
}
@Test
void hasQueryParamNoFalsePositiveOnPartialPrefix() {
// "tagging" should not match "tag"
assertFalse(S3RequestParser.hasQueryParamInString("tagging", "tag"));
}
@Test
void hasQueryParamDetectsParamValueContainsEquals() {
// Param with = in value — only the first = is the delimiter
String query = "X-Amz-Credential=test/2024/us-east-1/s3/aws4_request";
assertTrue(S3RequestParser.hasQueryParamInString(query, "X-Amz-Credential"));
}
@Test
void hasQueryParamNoFalsePositiveOnUnrelatedQuery() {
String query = "versionId=abc&uploadId=xyz";
assertFalse(S3RequestParser.hasQueryParamInString(query, "tagging"));
assertFalse(S3RequestParser.hasQueryParamInString(query, "acl"));
}
}