mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(s3): recognize virtual-hosted-style requests over HTTP/2 (#1954)
* fix(s3): recognize virtual-hosted-style requests over HTTP/2
Browsers negotiate HTTP/2 via ALPN over HTTPS, and HTTP/2 (RFC 9113) has no
Host header — the authority is carried in the :authority pseudo-header.
S3VirtualHostFilter read only getHeaderString("Host"), which is null over
HTTP/2, so virtual-hosted-style requests fell through to path-style handling:
the bucket label in the authority was ignored and the first path segment was
treated as the bucket instead.
Resolve the effective authority from the request URI (populated from
:authority) when the Host header is absent, so bucket routing works on both
HTTP/1.1 and HTTP/2.
Closes #1866
* fix(s3): detect website requests over HTTP/2 via request-URI authority
Over HTTP/2 there is no Host header, so S3Controller.isWebsiteRequest saw a null host and served API XML instead of website HTML for virtual-hosted website buckets. Resolve the effective authority from the request URI (reusing S3VirtualHostFilter.resolveHost) so website index/error-document handling works on HTTP/2 too; the filter's path rewrite preserves the s3-website authority for this.
Tests: isWebsiteRequest detection over HTTP/1.1 and HTTP/2 (S3ControllerWebsiteRequestTest), and that the filter rewrite keeps the s3-website authority downstream (S3VirtualHostFilterTest).
This commit is contained in:
1 parent
79031a240a
commit
5f7d4d2dc8
4 files changed
+157
-6
No files matched your search
@@ -498,7 +498,7 @@ public class S3Controller {
|
||||
}
|
||||
|
||||
// --- Website Hosting Redirection Logic ---
|
||||
if (isWebsiteRequest(httpHeaders) && (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type")))) {
|
||||
if (isWebsiteRequest(httpHeaders, uriInfo) && (uriInfo.getQueryParameters().isEmpty() || (uriInfo.getQueryParameters().size() == 1 && hasQueryParam(uriInfo, "list-type")))) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
if (webConfig.getIndexDocument() != null) {
|
||||
@@ -815,7 +815,7 @@ public class S3Controller {
|
||||
return fullObjectResponse(bucket, key, versionId, obj, overrides, includeChecksum);
|
||||
} catch (AwsException e) {
|
||||
emitCloudTrailEvent("GetObject", bucket, key, 0L, 0L, e.getErrorCode(), e.getMessage());
|
||||
if (isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders)) {
|
||||
if (isWebsiteErrorDocumentTrigger(e) && isWebsiteRequest(httpHeaders, uriInfo)) {
|
||||
try {
|
||||
WebsiteConfiguration webConfig = s3Service.getBucketWebsite(bucket);
|
||||
Response r = serveErrorDocument(bucket, webConfig, authorization, e.getHttpStatus());
|
||||
@@ -2181,8 +2181,11 @@ public class S3Controller {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isWebsiteRequest(HttpHeaders httpHeaders) {
|
||||
String host = httpHeaders.getHeaderString("Host");
|
||||
static boolean isWebsiteRequest(HttpHeaders httpHeaders, UriInfo uriInfo) {
|
||||
// HTTP/2 (RFC 9113) carries no Host header, so fall back to the request URI authority —
|
||||
// the same resolution S3VirtualHostFilter applies. Without this, a website bucket reached
|
||||
// over HTTP/2 would be served as an API (XML) response instead of website HTML.
|
||||
String host = S3VirtualHostFilter.resolveHost(httpHeaders.getHeaderString("Host"), uriInfo.getRequestUri());
|
||||
return host != null && host.contains("s3-website");
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,13 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext) {
|
||||
String host = requestContext.getHeaderString("Host");
|
||||
URI uri = requestContext.getUriInfo().getRequestUri();
|
||||
|
||||
// HTTP/2 (RFC 9113) has no "Host" header — the authority travels in the
|
||||
// ":authority" pseudo-header, surfaced here as the request URI authority.
|
||||
// Falling back to it keeps virtual-hosted-style routing working when a
|
||||
// browser negotiates HTTP/2 over HTTPS (where the Host header is absent).
|
||||
String host = resolveHost(requestContext.getHeaderString("Host"), uri);
|
||||
if (host == null) return;
|
||||
|
||||
// Do not hijack requests meant for other AWS services
|
||||
@@ -55,7 +61,6 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
|
||||
String bucket = extractBucket(host, baseHostname);
|
||||
if (bucket == null) return;
|
||||
|
||||
URI uri = requestContext.getUriInfo().getRequestUri();
|
||||
String path = uri.getRawPath();
|
||||
|
||||
// Do not rewrite S3 Control API paths — the account ID appears as a host label
|
||||
@@ -74,6 +79,26 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
|
||||
requestContext.setRequestUri(newUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the effective request authority used for virtual-host detection.
|
||||
*
|
||||
* <p>HTTP/1.1 carries it in the {@code Host} header. HTTP/2 (RFC 9113) has no
|
||||
* {@code Host} header — the authority is in the {@code :authority} pseudo-header,
|
||||
* which the container exposes as the request URI authority. When the {@code Host}
|
||||
* header is absent we fall back to the URI authority so virtual-hosted-style
|
||||
* requests are recognized on both protocol versions.
|
||||
*
|
||||
* @param hostHeader the value of the {@code Host} header, or {@code null}
|
||||
* @param requestUri the request URI, or {@code null}
|
||||
* @return the effective authority ({@code host[:port]}), or {@code null} if neither is available
|
||||
*/
|
||||
static String resolveHost(String hostHeader, URI requestUri) {
|
||||
if (hostHeader != null) {
|
||||
return hostHeader;
|
||||
}
|
||||
return requestUri != null ? requestUri.getAuthority() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts a bucket name from a virtual-hosted-style Host header.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user