mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+56
@@ -0,0 +1,56 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class S3ControllerWebsiteRequestTest {
|
||||
|
||||
private static HttpHeaders headers(String hostHeader) {
|
||||
HttpHeaders h = mock(HttpHeaders.class);
|
||||
when(h.getHeaderString("Host")).thenReturn(hostHeader);
|
||||
return h;
|
||||
}
|
||||
|
||||
private static UriInfo uri(String requestUri) {
|
||||
UriInfo u = mock(UriInfo.class);
|
||||
when(u.getRequestUri()).thenReturn(URI.create(requestUri));
|
||||
return u;
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectsWebsiteFromHostHeaderOverHttp1() {
|
||||
assertTrue(S3Controller.isWebsiteRequest(
|
||||
headers("my-bucket.s3-website-us-east-1.amazonaws.com"),
|
||||
uri("http://my-bucket.s3-website-us-east-1.amazonaws.com/index.html")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detectsWebsiteFromUriAuthorityWhenHostHeaderAbsentOverHttp2() {
|
||||
// Regression for the HTTP/2 review finding: no Host header, authority carried by the URI.
|
||||
assertTrue(S3Controller.isWebsiteRequest(
|
||||
headers(null),
|
||||
uri("https://my-bucket.s3-website-us-east-1.localhost:4566/index.html")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonWebsiteHttp2RequestIsNotWebsite() {
|
||||
assertFalse(S3Controller.isWebsiteRequest(
|
||||
headers(null),
|
||||
uri("https://my-bucket.s3.us-east-1.localhost:4566/key.txt")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonWebsiteHostHeaderIsNotWebsite() {
|
||||
assertFalse(S3Controller.isWebsiteRequest(
|
||||
headers("my-bucket.s3.amazonaws.com"),
|
||||
uri("http://my-bucket.s3.amazonaws.com/key.txt")));
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.NullSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class S3VirtualHostFilterTest {
|
||||
|
||||
@@ -142,4 +150,63 @@ class S3VirtualHostFilterTest {
|
||||
void extractHostnameFromUrlReturnsNullForNull() {
|
||||
assertNull(S3VirtualHostFilter.extractHostnameFromUrl(null));
|
||||
}
|
||||
|
||||
// --- Host resolution: HTTP/1.1 Host header vs HTTP/2 :authority fallback ---
|
||||
|
||||
@Test
|
||||
void resolveHostPrefersHostHeaderOverUriAuthority() {
|
||||
URI uri = URI.create("https://my-bucket.s3.us-east-1.localhost:4566/key.txt");
|
||||
assertEquals("my-bucket.localhost:4566", S3VirtualHostFilter.resolveHost("my-bucket.localhost:4566", uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveHostFallsBackToUriAuthorityWhenHostHeaderAbsent() {
|
||||
// HTTP/2 request: no Host header, authority carried by the URI (:authority).
|
||||
URI uri = URI.create("https://my-bucket.s3.us-east-1.localhost:4566/key.txt");
|
||||
assertEquals("my-bucket.s3.us-east-1.localhost:4566", S3VirtualHostFilter.resolveHost(null, uri));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveHostReturnsNullWhenNeitherAvailable() {
|
||||
assertNull(S3VirtualHostFilter.resolveHost(null, null));
|
||||
assertNull(S3VirtualHostFilter.resolveHost(null, URI.create("/relative/path")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void http2VirtualHostedRequestResolvesBucketWithoutHostHeader() {
|
||||
// Regression for #1866: over HTTP/2 the Host header is null, so the bucket must
|
||||
// be recovered from the URI authority instead of falling through to path-style.
|
||||
URI uri = URI.create("https://my-bucket.s3.us-east-1.localhost:4566/key.txt");
|
||||
String host = S3VirtualHostFilter.resolveHost(null, uri);
|
||||
assertEquals("my-bucket", S3VirtualHostFilter.extractBucket(host, "localhost"));
|
||||
}
|
||||
|
||||
// --- HTTP/2 website request: the path rewrite must preserve the s3-website authority (#1954) ---
|
||||
|
||||
@Test
|
||||
void http2WebsiteRequestRewritePreservesAuthorityForDownstreamDetection() {
|
||||
// Over HTTP/2 a website request has no Host header. The filter rewrites the path to
|
||||
// /bucket/key for the path-style S3 controller, but must keep the s3-website authority
|
||||
// on the request URI. Otherwise S3Controller.isWebsiteRequest — which, with no Host
|
||||
// header, reads the URI authority (S3VirtualHostFilter.resolveHost) — can no longer
|
||||
// recognize the request and serves API XML instead of the index/error document.
|
||||
URI requestUri = URI.create("https://my-bucket.s3-website-us-east-1.localhost:4566/index.html");
|
||||
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getRequestUri()).thenReturn(requestUri);
|
||||
ContainerRequestContext ctx = mock(ContainerRequestContext.class);
|
||||
when(ctx.getUriInfo()).thenReturn(uriInfo);
|
||||
when(ctx.getHeaderString("Host")).thenReturn(null); // HTTP/2: no Host header
|
||||
|
||||
new S3VirtualHostFilter().filter(ctx);
|
||||
|
||||
ArgumentCaptor<URI> rewritten = ArgumentCaptor.forClass(URI.class);
|
||||
verify(ctx).setRequestUri(rewritten.capture());
|
||||
URI newUri = rewritten.getValue();
|
||||
|
||||
// Path rewritten to path-style for the S3 controller...
|
||||
assertEquals("/my-bucket/index.html", newUri.getRawPath());
|
||||
// ...but the s3-website authority survives, so downstream website detection still fires.
|
||||
assertEquals("my-bucket.s3-website-us-east-1.localhost:4566", newUri.getAuthority());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user