fix(s3): verify SigV4 signatures against the wire path for leading-slash keys (#4125)

A key that starts with a slash travels as a double slash on the wire
(PUT /bucket//email.txt) and the client signs that path verbatim, since
S3 never normalizes the canonical URI. Both signature filters rebuilt the
canonical request from the JAX-RS UriInfo path, which collapses the
consecutive slashes, so every such request was rejected with
SignatureDoesNotMatch under enforce-auth and, conversely, a signature
over the normalized path verified for the other object.

Both filters now take the path from the raw Vert.x request, the way
S3Controller already extracts the key. The virtual-hosted key extraction
also kept dropping the leading slash, because it compared the raw path
against the collapsed rewritten URI; it now accepts the collapsed form.

Closes #4123

Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-22 16:02:12 -04:00
1 parent 4753428567
commit 2bf92a9beb
5 files changed
+311 -13

No files matched your search

@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.iam.IamService;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import jakarta.inject.Inject;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
@@ -32,14 +33,17 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
private final PreSignedUrlGenerator presignGenerator;
private final S3Service s3Service;
private final IamService iamService;
private final CurrentVertxRequest currentVertxRequest;
@Inject
public PreSignedUrlFilter(PreSignedUrlGenerator presignGenerator,
S3Service s3Service,
IamService iamService) {
IamService iamService,
CurrentVertxRequest currentVertxRequest) {
this.presignGenerator = presignGenerator;
this.s3Service = s3Service;
this.iamService = iamService;
this.currentVertxRequest = currentVertxRequest;
}
@Override
@@ -196,8 +200,9 @@ public class PreSignedUrlFilter implements ContainerRequestFilter {
}
}
// Canonical request
String path = requestUri.getRawPath();
// Canonical request: the wire path, since a leading-slash key travels (and is
// signed) as a double slash that JAX-RS would otherwise collapse.
String path = S3HeaderSignatureFilter.signedPath(currentVertxRequest, requestUri);
String canonicalQueryString = buildCanonicalQueryString(queryParams);
String payloadHash = requestContext.getHeaderString("x-amz-content-sha256");
if (payloadHash == null) {
@@ -3760,16 +3760,21 @@ public class S3Controller {
/**
* True when the raw request path carries no bucket prefix because the bucket came from the
* Host header: the rewritten request URI is then exactly "/" + bucket + rawPath, which is
* what {@link S3VirtualHostFilter} produces and what a path-style request never is.
* what {@link S3VirtualHostFilter} produces and what a path-style request never is. JAX-RS
* collapses consecutive slashes before the filter sees the path, so a leading-slash key
* ({@code //file.txt} on the wire) is rewritten to {@code /<bucket>/file.txt}; the collapsed
* form is compared as well so that key keeps its slash.
*/
private static boolean isVirtualHostedRawPath(UriInfo uriInfo, String bucket, String rawPath) {
if (rawPath.isEmpty() || rawPath.charAt(0) != '/') {
return false;
}
String rewritten = uriInfo.getRequestUri().getRawPath();
return rewritten != null
&& !rewritten.equals(rawPath)
&& rewritten.equals("/" + bucket + rawPath);
if (rewritten == null || rewritten.equals(rawPath)) {
return false;
}
return rewritten.equals("/" + bucket + rawPath)
|| rewritten.equals("/" + bucket + rawPath.replaceAll("/{2,}", "/"));
}
private void validateKeyNoTraversal(String key) {
@@ -2,6 +2,8 @@ package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.iam.IamService;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
import io.vertx.ext.web.RoutingContext;
import jakarta.annotation.Priority;
import jakarta.inject.Inject;
import jakarta.ws.rs.Priorities;
@@ -31,7 +33,7 @@ import java.util.Optional;
* <p>The presigned placements are already verified elsewhere ({@link PreSignedUrlFilter} for the
* query string, {@link S3PostPolicySigner} for a browser POST). This filter closes the remaining
* placement, the one every AWS SDK uses for ordinary calls: the canonical request is rebuilt from
* the request as it arrived (raw path, canonical query string, the headers named in
* the request as it arrived (the wire path, canonical query string, the headers named in
* {@code SignedHeaders}, and the {@code x-amz-content-sha256} value the client signed), the
* signature is derived with the key's secret, and the two are compared in constant time. When the
* declared content hash is a real digest rather than an {@code UNSIGNED-PAYLOAD} or
@@ -75,14 +77,17 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
private final S3Service s3Service;
private final IamService iamService;
private final CurrentVertxRequest currentVertxRequest;
@Context
ResourceInfo resourceInfo;
@Inject
public S3HeaderSignatureFilter(S3Service s3Service, IamService iamService) {
public S3HeaderSignatureFilter(S3Service s3Service, IamService iamService,
CurrentVertxRequest currentVertxRequest) {
this.s3Service = s3Service;
this.iamService = iamService;
this.currentVertxRequest = currentVertxRequest;
}
@Override
@@ -196,7 +201,7 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
}
String canonicalRequest = ctx.getMethod() + "\n"
+ requestUri.getRawPath() + "\n"
+ signedPath(currentVertxRequest, requestUri) + "\n"
+ PreSignedUrlFilter.buildCanonicalQueryString(ctx.getUriInfo().getQueryParameters()) + "\n"
+ canonicalHeaders + "\n"
+ signedHeaders + "\n"
@@ -212,6 +217,25 @@ public class S3HeaderSignatureFilter implements ContainerRequestFilter {
signature.getBytes(StandardCharsets.UTF_8));
}
/**
* The path the client signed: the request line exactly as it arrived, before JAX-RS collapsed
* consecutive slashes in {@code UriInfo}. S3 never normalizes the canonical URI, so a key with
* a leading slash ({@code /email.txt}) is sent, and signed, as {@code /bucket//email.txt}
* (or {@code //email.txt} virtual-hosted). Verifying against the normalized path would reject
* every such request and, conversely, let a signature over {@code /bucket/email.txt} pass for
* the other object. Falls back to {@code requestUri} when no Vert.x request is current.
*/
static String signedPath(CurrentVertxRequest currentVertxRequest, URI requestUri) {
RoutingContext routingContext = currentVertxRequest != null ? currentVertxRequest.getCurrent() : null;
if (routingContext != null && routingContext.request() != null) {
String path = routingContext.request().path();
if (path != null && !path.isEmpty()) {
return path;
}
}
return requestUri.getRawPath();
}
/**
* Hashes the entity and hands it back to the request untouched. The S3 controllers already
* receive the body as a {@code byte[]}, so buffering it here costs nothing extra. The stream