mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(core): serve HTTP/1.0 requests that omit the Host header (#4174)
A health probe written without a Host header answered 500 on a healthy
emulator:
GET /_floci/health HTTP/1.0 -> 500
GET /_floci/health HTTP/1.1 + Host: localhost -> 200
Host is mandatory only from HTTP/1.1 on, so Vert.x answers 400 to a 1.1
request without one and exempts 1.0 by design. A 1.0 request then reached
the application with no authority anywhere: HttpServerRequest.absoluteURI()
is null, and UriInfo.getRequestUri() fails inside new URI(null). Every
host-routing filter reads the request URI, through RequestHost or directly,
so LambdaUrlRoutingFilter threw a NullPointerException before routing ever
reached a resource. Nothing about it was health-specific: every endpoint
answered 500 the same way.
MissingHostHeaderFilter is a pre-matching filter that fills in the missing
authority. For an origin-form target, which names no host anywhere, that is
the local socket the request arrived on, the address the client actually
reached. An address literal names no S3 bucket, Lambda function URL, or
custom domain, so the request routes path-style, which is what a client
sending no Host asked for.
An absolute-form target states an authority of its own, which RFC 9112
section 3.2.2 has the server route by, and it answered 500 for the same
reason: the authority vertx-web builds its absolute URI from is the Host
header alone. So GET http://bucket.s3.localhost:4566/key HTTP/1.0 takes the
bucket from the target and keeps routing to it. Asterisk-form names no host
at all and is left alone, as it is with a Host header.
The repair is the request URI rather than the Host header. Vert.x resolves
the authority once, in RoutingContextImpl.route(), before any handler runs,
and caches the absence, so a header set afterwards is never looked at again.
Rewriting the JAX-RS request URI is the same mechanism the virtual-host
filters already use to reroute a request.
HTTP/1.1 without Host keeps answering Vert.x's 400 with the message naming
the missing header.
This commit is contained in:
1 parent
684239b0ef
commit
f084243fc0
3 files changed
+302
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.net.SocketAddress;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
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.when;
|
||||
|
||||
class MissingHostHeaderFilterTest {
|
||||
|
||||
@Test
|
||||
void originFormTakesTheAuthorityFromTheLocalSocket() {
|
||||
assertEquals("127.0.0.1:4566", MissingHostHeaderFilter.requestAuthority(request("/_floci/health")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void absoluteFormTakesTheAuthorityFromTheTarget() {
|
||||
assertEquals("bucket.s3.localhost:4566",
|
||||
MissingHostHeaderFilter.requestAuthority(request("http://bucket.s3.localhost:4566/key")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void absoluteFormWithoutAPathTakesTheAuthorityFromTheTarget() {
|
||||
assertEquals("bucket.s3.localhost:4566",
|
||||
MissingHostHeaderFilter.requestAuthority(request("http://bucket.s3.localhost:4566")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void asteriskFormHasNoAuthority() {
|
||||
assertNull(MissingHostHeaderFilter.requestAuthority(request("*")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedTargetHasNoAuthority() {
|
||||
assertNull(MissingHostHeaderFilter.requestAuthority(request("http://bucket.s3.localhost:45 66/key")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localAuthorityKeepsIpv4Literal() {
|
||||
assertEquals("127.0.0.1:4566",
|
||||
MissingHostHeaderFilter.localAuthority(SocketAddress.inetSocketAddress(4566, "127.0.0.1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localAuthorityBracketsIpv6Literal() {
|
||||
assertEquals("[::1]:4566",
|
||||
MissingHostHeaderFilter.localAuthority(SocketAddress.inetSocketAddress(4566, "::1")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localAuthorityLeavesBracketedIpv6Alone() {
|
||||
assertEquals("[::1]:4566",
|
||||
MissingHostHeaderFilter.localAuthority(SocketAddress.inetSocketAddress(4566, "[::1]")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localAuthorityWithoutAddressIsNull() {
|
||||
assertNull(MissingHostHeaderFilter.localAuthority(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void localAuthorityOfDomainSocketIsNull() {
|
||||
assertNull(MissingHostHeaderFilter.localAuthority(SocketAddress.domainSocketAddress("/tmp/floci.sock")));
|
||||
}
|
||||
|
||||
private static HttpServerRequest request(String target) {
|
||||
HttpServerRequest request = mock(HttpServerRequest.class);
|
||||
when(request.uri()).thenReturn(target);
|
||||
when(request.localAddress()).thenReturn(SocketAddress.inetSocketAddress(4566, "127.0.0.1"));
|
||||
return request;
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.RestAssured;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* A Host-less request cannot be expressed with RestAssured or any SDK client, so these go over a
|
||||
* raw socket. HTTP/1.0 omitting Host is legal and used to answer 500; HTTP/1.1 omitting it is a
|
||||
* protocol violation that Vert.x rejects before the router, and still should. An absolute-form
|
||||
* target states its own authority and must keep routing by it, Host header or not.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class MissingHostHeaderIntegrationTest {
|
||||
|
||||
@Test
|
||||
void http10WithoutHostHeaderServesHealth() throws Exception {
|
||||
String response = rawRequest("GET /_floci/health HTTP/1.0\r\n\r\n");
|
||||
|
||||
assertThat(response, startsWith("HTTP/1.0 200 OK"));
|
||||
assertThat(response, containsString("\"edition\":\"community\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void http10WithoutHostHeaderServesPathStyleS3() throws Exception {
|
||||
String response = rawRequest("GET / HTTP/1.0\r\n\r\n");
|
||||
|
||||
assertThat(response, startsWith("HTTP/1.0 200 OK"));
|
||||
assertThat(response, containsString("ListAllMyBucketsResult"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void http10AbsoluteFormWithoutHostHeaderServesHealth() throws Exception {
|
||||
String response = rawRequest(
|
||||
"GET http://127.0.0.1:" + RestAssured.port + "/_floci/health HTTP/1.0\r\n\r\n");
|
||||
|
||||
assertThat(response, startsWith("HTTP/1.0 200 OK"));
|
||||
assertThat(response, containsString("\"edition\":\"community\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void http10AbsoluteFormWithoutHostHeaderRoutesByTheTargetAuthority() throws Exception {
|
||||
String bucket = "absolute-form-bucket";
|
||||
given().put("/" + bucket).then().statusCode(200);
|
||||
try {
|
||||
String response = rawRequest("GET http://" + bucket + ".s3.localhost:"
|
||||
+ RestAssured.port + "/ HTTP/1.0\r\n\r\n");
|
||||
|
||||
assertThat(response, startsWith("HTTP/1.0 200 OK"));
|
||||
assertThat(response, containsString("<Name>" + bucket + "</Name>"));
|
||||
} finally {
|
||||
given().delete("/" + bucket);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void http11WithoutHostHeaderIsRejected() throws Exception {
|
||||
String response = rawRequest("GET /_floci/health HTTP/1.1\r\nConnection: close\r\n\r\n");
|
||||
|
||||
assertThat(response, startsWith("HTTP/1.1 400 Bad Request"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void http11WithHostHeaderServesHealth() throws Exception {
|
||||
String response = rawRequest(
|
||||
"GET /_floci/health HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
|
||||
|
||||
assertThat(response, startsWith("HTTP/1.1 200 OK"));
|
||||
assertThat(response, containsString("\"edition\":\"community\""));
|
||||
}
|
||||
|
||||
private String rawRequest(String request) throws Exception {
|
||||
try (Socket socket = new Socket("127.0.0.1", RestAssured.port)) {
|
||||
socket.setSoTimeout(15_000);
|
||||
OutputStream out = socket.getOutputStream();
|
||||
out.write(request.getBytes(StandardCharsets.US_ASCII));
|
||||
out.flush();
|
||||
|
||||
InputStream in = socket.getInputStream();
|
||||
ByteArrayOutputStream received = new ByteArrayOutputStream();
|
||||
byte[] chunk = new byte[8192];
|
||||
int read;
|
||||
while ((read = in.read(chunk)) != -1) {
|
||||
received.write(chunk, 0, read);
|
||||
}
|
||||
return received.toString(StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user