From 8fb07d26545131f784c38927e8c0dd3fb651908e Mon Sep 17 00:00:00 2001 From: opcode-sec <162790345+opcode-sec@users.noreply.github.com> Date: Sun, 24 May 2026 20:30:50 +0100 Subject: [PATCH] fix(s3): persist inline x-amz-tagging header on PutObject (#987) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(s3): persist inline x-amz-tagging header on PutObject PutObject silently dropped tags supplied via the x-amz-tagging request header — the controller did not read the header and tags never reached the object store. Adds a header parameter, parses the URL-encoded key=value tag set (with 10-tag and 8 KB limits), and persists via the existing putObjectTagging path so behavior matches real AWS S3. * test(s3): add negative-path tests for inline x-amz-tagging header Cover malformed pair, empty key, duplicate key, >10 tags, and oversized header. Note in S3Controller that the InvalidArgument and BadRequest codes match observed real-AWS behavior even though they are not in the S3 Smithy service model --------- Signed-off-by: Hector Ventura Co-authored-by: Hector Ventura --- .../src/test/java/com/floci/test/S3Test.java | 100 ++++++++++++++++++ .../floci/services/s3/S3Controller.java | 62 ++++++++++- .../floci/services/s3/S3Service.java | 3 + .../services/s3/model/PutObjectOptions.java | 5 + 4 files changed, 169 insertions(+), 1 deletion(-) diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java index f4cf335217..a977e667d7 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/S3Test.java @@ -427,4 +427,104 @@ class S3Test { } catch (Exception ignored) {} } } + + @Test + @Order(27) + void putObjectPersistsInlineTaggingHeader() { + String bucket = TestFixtures.uniqueName("inline-tag-bucket"); + String key = "inline-tagged.txt"; + s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build()); + try { + s3.putObject(PutObjectRequest.builder() + .bucket(bucket).key(key) + .tagging(Tagging.builder() + .tagSet( + software.amazon.awssdk.services.s3.model.Tag.builder().key("env").value("test").build(), + software.amazon.awssdk.services.s3.model.Tag.builder().key("project").value("floci").build() + ) + .build()) + .build(), + RequestBody.fromString("inline-tagged content")); + + GetObjectTaggingResponse response = s3.getObjectTagging( + GetObjectTaggingRequest.builder().bucket(bucket).key(key).build()); + + assertThat(response.tagSet()).hasSize(2); + assertThat(response.tagSet()) + .anyMatch(t -> "env".equals(t.key()) && "test".equals(t.value())) + .anyMatch(t -> "project".equals(t.key()) && "floci".equals(t.value())); + } finally { + try { + s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build()); + } catch (Exception ignored) {} + try { + s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build()); + } catch (Exception ignored) {} + } + } + + @Test + @Order(28) + void putObjectInlineTaggingRejectsMalformedPair() { + assertInlineTaggingHeaderRejected("nokeyhasvalue", 400, "InvalidArgument"); + } + + @Test + @Order(29) + void putObjectInlineTaggingRejectsEmptyKey() { + assertInlineTaggingHeaderRejected("=value", 400, "InvalidArgument"); + } + + @Test + @Order(30) + void putObjectInlineTaggingRejectsDuplicateKey() { + assertInlineTaggingHeaderRejected("env=a&env=b", 400, "InvalidArgument"); + } + + @Test + @Order(31) + void putObjectInlineTaggingRejectsTooManyTags() { + StringBuilder header = new StringBuilder(); + for (int i = 0; i < 11; i++) { + if (i > 0) header.append('&'); + header.append("k").append(i).append("=v").append(i); + } + assertInlineTaggingHeaderRejected(header.toString(), 400, "BadRequest"); + } + + @Test + @Order(32) + void putObjectInlineTaggingRejectsOversizedHeader() { + // The size check runs before pair parsing, so a single huge value trips it + // without first being rejected by the >10-tag rule. + String huge = "k=" + "v".repeat(8 * 1024 + 1); + assertInlineTaggingHeaderRejected(huge, 400, "InvalidArgument"); + } + + private void assertInlineTaggingHeaderRejected(String rawTaggingHeader, int expectedStatus, String expectedErrorCode) { + String bucket = TestFixtures.uniqueName("inline-tag-reject"); + String key = "rejected.txt"; + s3.createBucket(CreateBucketRequest.builder().bucket(bucket).build()); + try { + assertThatThrownBy(() -> s3.putObject( + PutObjectRequest.builder() + .bucket(bucket).key(key) + .overrideConfiguration(o -> o.putHeader("x-amz-tagging", rawTaggingHeader)) + .build(), + RequestBody.fromString("content"))) + .isInstanceOf(S3Exception.class) + .satisfies(e -> { + S3Exception s3e = (S3Exception) e; + assertThat(s3e.statusCode()).isEqualTo(expectedStatus); + assertThat(s3e.awsErrorDetails().errorCode()).isEqualTo(expectedErrorCode); + }); + } finally { + try { + s3.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(key).build()); + } catch (Exception ignored) {} + try { + s3.deleteBucket(DeleteBucketRequest.builder().bucket(bucket).build()); + } catch (Exception ignored) {} + } + } } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 0b6d6ccabf..0adec0de25 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -437,6 +437,7 @@ public class S3Controller { @HeaderParam("Content-Encoding") String contentEncoding, @HeaderParam("x-amz-content-sha256") String contentSha256, @HeaderParam("x-amz-copy-source") String copySource, + @HeaderParam("x-amz-tagging") String tagging, @HeaderParam("If-Match") String ifMatch, @HeaderParam("If-None-Match") String ifNoneMatch, @QueryParam("uploadId") String uploadId, @@ -483,6 +484,8 @@ public class S3Controller { return preconditionResponse; } + Map inlineTags = parseInlineTaggingHeader(tagging); + String lockMode = httpHeaders.getHeaderString("x-amz-object-lock-mode"); String retainUntilStr = httpHeaders.getHeaderString("x-amz-object-lock-retain-until-date"); String legalHold = httpHeaders.getHeaderString("x-amz-object-lock-legal-hold"); @@ -508,7 +511,8 @@ public class S3Controller { .withServerSideEncryption(serverSideEncryption) .withAcl(cannedAcl) .withChecksumAlgorithm(checksumAlgorithm) - .withClientChecksum(extractChecksumFromHeaders(httpHeaders))); + .withClientChecksum(extractChecksumFromHeaders(httpHeaders)) + .withTagging(inlineTags)); var resp = Response.ok().header("ETag", obj.getETag()); if (obj.getVersionId() != null) { resp.header("x-amz-version-id", obj.getVersionId()); @@ -2271,6 +2275,62 @@ public class S3Controller { } } + private boolean hasQueryParam(UriInfo uriInfo, String param) { + if (uriInfo.getQueryParameters().containsKey(param)) return true; + String query = uriInfo.getRequestUri().getQuery(); + if (query == null) return false; + return query.equals(param) || query.contains(param + "&") || query.contains("&" + param); + } + + private static final int MAX_INLINE_TAGS = 10; + private static final int MAX_INLINE_TAGGING_HEADER_BYTES = 8 * 1024; + + /** + * Parses an {@code x-amz-tagging} request-header value (URL-encoded + * {@code k=v&k=v}) into a tag map. Returns an empty map for null or blank input. + * + *

Note: the error codes thrown here ({@code InvalidArgument} for malformed input, + * {@code BadRequest} for exceeding the 10-tag limit) match real-AWS S3 behavior + * observed in practice but are not in the S3 Smithy service model. + */ + private static Map parseInlineTaggingHeader(String header) { + if (header == null || header.isEmpty()) { + return Map.of(); + } + if (header.getBytes(StandardCharsets.UTF_8).length > MAX_INLINE_TAGGING_HEADER_BYTES) { + throw new AwsException("InvalidArgument", + "The x-amz-tagging header exceeds the 8 KB limit.", 400); + } + Map tags = new LinkedHashMap<>(); + for (String pair : header.split("&")) { + if (pair.isEmpty()) { + continue; + } + int eq = pair.indexOf('='); + if (eq < 0) { + throw new AwsException("InvalidArgument", + "The x-amz-tagging header is malformed: missing '=' in pair.", 400); + } + String rawKey = pair.substring(0, eq); + String rawValue = pair.substring(eq + 1); + if (rawKey.isEmpty()) { + throw new AwsException("InvalidArgument", + "The x-amz-tagging header is malformed: empty tag key.", 400); + } + String tagKey = URLDecoder.decode(rawKey, StandardCharsets.UTF_8); + String tagValue = URLDecoder.decode(rawValue, StandardCharsets.UTF_8); + if (tags.put(tagKey, tagValue) != null) { + throw new AwsException("InvalidArgument", + "The x-amz-tagging header contains duplicate tag key: " + tagKey, 400); + } + } + if (tags.size() > MAX_INLINE_TAGS) { + throw new AwsException("BadRequest", + "Object tags cannot be greater than " + MAX_INLINE_TAGS, 400); + } + return tags; + } + /** * Extracts the object key from the raw Vert.x request URI, preserving leading slashes * that JAX-RS path normalization would otherwise strip. diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index 95ba239495..27d22a29fa 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -297,6 +297,9 @@ public class S3Service { object.setCacheControl(effectiveOptions.getCacheControl()); object.setServerSideEncryption(normalizedServerSideEncryption); object.setAcl(cannedObjectAclXml(effectiveOptions.getAcl())); + if (effectiveOptions.getTagging() != null && !effectiveOptions.getTagging().isEmpty()) { + object.setTags(new HashMap<>(effectiveOptions.getTagging())); + } if (bucket.isVersioningEnabled()) { String versionId = UUID.randomUUID().toString(); diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java b/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java index 41e1de1623..83d612ba4f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/model/PutObjectOptions.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.s3.model; import java.time.Instant; +import java.util.Map; public class PutObjectOptions { private String storageClass; @@ -13,6 +14,7 @@ public class PutObjectOptions { private String serverSideEncryption; private String acl; private String checksumAlgorithm; + private Map tagging; public String getStorageClass() { return storageClass; } public PutObjectOptions withStorageClass(String storageClass) { this.storageClass = storageClass; return this; } @@ -44,6 +46,9 @@ public class PutObjectOptions { public String getChecksumAlgorithm() { return checksumAlgorithm; } public PutObjectOptions withChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; } + public Map getTagging() { return tagging; } + public PutObjectOptions withTagging(Map tagging) { this.tagging = tagging; return this; } + private S3Checksum clientChecksum; public S3Checksum getClientChecksum() { return clientChecksum; } public PutObjectOptions withClientChecksum(S3Checksum clientChecksum) { this.clientChecksum = clientChecksum; return this; }