fix(s3): persist inline x-amz-tagging header on PutObject (#987)

* 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 <hectorvent@gmail.com>
Co-authored-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
opcode-secandHector Ventura authored and GitHub committed 2026-05-24 14:30:50 -05:00
1 parent 2da06840e9
commit 8fb07d2654
4 files changed
+169 -1

No files matched your search

@@ -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<String, String> 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.
*
* <p>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<String, String> 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<String, String> 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.
@@ -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();
@@ -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<String, String> 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<String, String> getTagging() { return tagging; }
public PutObjectOptions withTagging(Map<String, String> tagging) { this.tagging = tagging; return this; }
private S3Checksum clientChecksum;
public S3Checksum getClientChecksum() { return clientChecksum; }
public PutObjectOptions withClientChecksum(S3Checksum clientChecksum) { this.clientChecksum = clientChecksum; return this; }