From f4aba344bb95d3cc044ad0ea141a15de4e296737 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sun, 22 Nov 2026 17:56:35 +0100 Subject: [PATCH] mod_http3: Select an IP-based virtual host by the destination address. h3_synth_conn() set c->local_addr to 0.0.0.0, which no IP-based virtual host matches, so an HTTP/3 request to the address of one IP-based virtual host was served by another, and %A logged 0.0.0.0. Take the local address from the connection's network path, the address the client sent to. Keep the TCP port of the HTTP/3 host, so that an H3Port of its own still matches . An address that no IP-based virtual host names still reaches the first HTTP/3 virtual host; test_h3_018_02 records that as a known bug. * modules/http3/quic/h3q.c, modules/http3/include/quic/h3q.h (h3q_engine_local_addr): New. * modules/http3/h3_request.c (sockaddr_resolve, local_addr_resolve): New, from peer_addr_resolve. (h3_synth_conn): Set the local address from the connection's path. * test/modules/http3/test_018_ip_vhost.py: New. --- changes-entries/h3_ip_vhost.txt | 4 ++ modules/http3/h3_request.c | 46 +++++++++---- modules/http3/include/quic/h3q.h | 11 +++ modules/http3/quic/h3q.c | 12 ++++ test/modules/http3/test_018_ip_vhost.py | 92 +++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 changes-entries/h3_ip_vhost.txt create mode 100644 test/modules/http3/test_018_ip_vhost.py diff --git a/changes-entries/h3_ip_vhost.txt b/changes-entries/h3_ip_vhost.txt new file mode 100644 index 00000000000..f9933e2ffa4 --- /dev/null +++ b/changes-entries/h3_ip_vhost.txt @@ -0,0 +1,4 @@ + *) mod_http3: Select an IP-based virtual host by the address the client + sent to, as for TCP, and log that address as the local address. An + HTTP/3 request to the address of one IP-based virtual host could be + served by another. [Arturo Bernal] diff --git a/modules/http3/h3_request.c b/modules/http3/h3_request.c index babb3b51c75..77b0dd3736a 100644 --- a/modules/http3/h3_request.c +++ b/modules/http3/h3_request.c @@ -64,25 +64,19 @@ typedef struct apr_off_t bytes_last_request; } h3_logio_config_t; -/* The engine reports a raw sockaddr, so the conversion to APR belongs here. */ -static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* pool, apr_sockaddr_t** addr, char** client_ip) +/* The engine reports raw sockaddrs, so the conversion to APR belongs here. + * A nonzero @p port replaces the port of the sockaddr. */ +static int sockaddr_resolve(const struct sockaddr_storage* sa, socklen_t sa_len, apr_port_t port, apr_pool_t* pool, apr_sockaddr_t** addr, char** ip_str) { - struct sockaddr_storage peer = {0}; - socklen_t peer_len = 0; - if (!h3q_engine_peer_addr(engine, qconn, &peer, &peer_len)) - { - return 0; - } - char host[NI_MAXHOST] = {0}; char serv[NI_MAXSERV] = {0}; - if (getnameinfo((const struct sockaddr*)&peer, peer_len, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV) != 0) + if (getnameinfo((const struct sockaddr*)sa, sa_len, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { return 0; } char* end = NULL; - unsigned long port = strtoul(serv, &end, 10); - if (serv[0] == '\0' || !end || end[0] != '\0' || port > 65535) + unsigned long sa_port = strtoul(serv, &end, 10); + if (serv[0] == '\0' || !end || end[0] != '\0' || sa_port > 65535) { return 0; } @@ -90,7 +84,7 @@ static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* po /* Fill locals first: apr_sockaddr_info_get NULLs its out-param on failure. */ apr_sockaddr_t* resolved = NULL; char* ip = NULL; - if (apr_sockaddr_info_get(&resolved, host, APR_UNSPEC, (apr_port_t)port, 0, pool) != APR_SUCCESS) + if (apr_sockaddr_info_get(&resolved, host, APR_UNSPEC, port ? port : (apr_port_t)sa_port, 0, pool) != APR_SUCCESS) { return 0; } @@ -99,10 +93,27 @@ static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* po return 0; } *addr = resolved; - *client_ip = ip; + *ip_str = ip; return 1; } +static int peer_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_pool_t* pool, apr_sockaddr_t** addr, char** client_ip) +{ + struct sockaddr_storage peer = {0}; + socklen_t peer_len = 0; + return h3q_engine_peer_addr(engine, qconn, &peer, &peer_len) && sockaddr_resolve(&peer, peer_len, 0, pool, addr, client_ip); +} + +/* The address the client sent to, which selects an IP-based virtual host. + * It keeps the port of the host's TCP listener: an H3Port of its own does + * not make the request belong to another virtual host. */ +static int local_addr_resolve(h3q_engine* engine, h3q_conn* qconn, apr_port_t port, apr_pool_t* pool, apr_sockaddr_t** addr, char** local_ip) +{ + struct sockaddr_storage local = {0}; + socklen_t local_len = 0; + return h3q_engine_local_addr(engine, qconn, &local, &local_len) && sockaddr_resolve(&local, local_len, port, pool, addr, local_ip); +} + conn_rec* h3_synth_conn(h3_session* session) { CHECK(session); @@ -132,6 +143,13 @@ conn_rec* h3_synth_conn(h3_session* session) { c->remote_host = NULL; } + if (child_h3_io) + { + /* Else 0.0.0.0, which no IP-based virtual host matches. Like the + * client address, it is the path the connection was accepted on and + * is not updated if the connection migrates. */ + local_addr_resolve(child_h3_io->qengine, session->qconn, vhost_port, cpool, &c->local_addr, &c->local_ip); + } c->bucket_alloc = apr_bucket_alloc_create(cpool); c->log = &s->log; #if H3_DEVEL diff --git a/modules/http3/include/quic/h3q.h b/modules/http3/include/quic/h3q.h index e6597a46639..f519fad0162 100644 --- a/modules/http3/include/quic/h3q.h +++ b/modules/http3/include/quic/h3q.h @@ -119,4 +119,15 @@ h3q_conn* h3q_engine_accept_conn(h3q_engine* engine); */ int h3q_engine_peer_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_storage* addr, socklen_t* addr_len); +/** + * Copy the local address from the connection's network path: the address + * the peer sent to where the socket reports it, else the bound address. + * @param engine Engine owning @p conn; NULL reports failure. + * @param conn Connection to inspect. + * @param addr Out: local socket address. + * @param addr_len Out: bytes of @p addr that are meaningful. + * @return 1 if the address was recovered, 0 otherwise. + */ +int h3q_engine_local_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_storage* addr, socklen_t* addr_len); + #endif /* H3Q_H */ diff --git a/modules/http3/quic/h3q.c b/modules/http3/quic/h3q.c index 5fdcdc9519b..6ddafd3a1b5 100644 --- a/modules/http3/quic/h3q.c +++ b/modules/http3/quic/h3q.c @@ -626,3 +626,15 @@ int h3q_engine_peer_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_sto *addr_len = (socklen_t)conn->path.path.remote.addrlen; return 1; } + +int h3q_engine_local_addr(h3q_engine* engine, h3q_conn* conn, struct sockaddr_storage* addr, socklen_t* addr_len) +{ + (void)engine; + if (!conn || !addr || !addr_len || conn->path.path.local.addrlen == 0 || conn->path.path.local.addrlen > sizeof(*addr)) + { + return 0; + } + memcpy(addr, conn->path.path.local.addr, conn->path.path.local.addrlen); + *addr_len = (socklen_t)conn->path.path.local.addrlen; + return 1; +} diff --git a/test/modules/http3/test_018_ip_vhost.py b/test/modules/http3/test_018_ip_vhost.py new file mode 100644 index 00000000000..4ea3ef249fb --- /dev/null +++ b/test/modules/http3/test_018_ip_vhost.py @@ -0,0 +1,92 @@ +import socket +import sys + +import pytest + +from .env import H3Conf, known_bug + +# Two IP-based virtual hosts on one port, told apart only by the address the +# client connects to. All of 127.0.0.0/8 is local on Linux. +HOSTS = [("127.0.0.1", "test1"), ("127.0.0.2", "test2")] + + +def free_port(env) -> int: + """A port free for UDP and for TCP on 127.0.0.1.""" + for _ in range(20): + port = env.free_udp_port() + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.bind(("127.0.0.1", port)) + return port + except OSError: + continue + finally: + s.close() + pytest.fail("no free port for TCP and UDP") + + +def get(env, port, addr, h3): + """GET /alive.json on port, connecting to addr.""" + url = f"https://{env.h3_domain}:{port}/alive.json" + options = ["--connect-to", f"{env.h3_domain}:{port}:{addr}:{port}"] + if h3: + return env.h3_get(url, options=options) + return env.curl_raw([url], options=["--http1.1"] + options) + + +@pytest.mark.skipif(sys.platform != "linux", + reason="needs a second loopback address") +class TestIpVhost: + + @pytest.fixture(autouse=True, scope='class') + @classmethod + def _class_scope(cls, env): + env.require_curl_http3() + + @staticmethod + def install_ip_vhosts(env, port): + conf = H3Conf(env) + conf.add(f"Listen {port}") + for addr, docs in HOSTS: + conf.add([ + f"", + f"ServerName {env.h3_domain}", + f"DocumentRoot htdocs/{docs}", + "SSLEngine on", + ]) + for cred in env.get_credentials_for_name(env.h3_domain): + conf.add_certificate(cred.cert_file, cred.pkey_file) + conf.add_h3_protocol() + conf.add("") + conf.install() + assert env.apache_restart() == 0 + assert env.wait_udp_port(port) + + # HTTP/3 selects the IP-based virtual host by the address the client + # sent to, as TCP does + def test_h3_018_01_by_address(self, env): + port = free_port(env) + self.install_ip_vhosts(env, port) + for addr, docs in HOSTS: + for h3 in [False, True]: + r = get(env, port, addr, h3) + proto = "HTTP/3" if h3 else "HTTP/1.1" + assert r.exit_code == 0, f"{addr} {proto}: {r}" + assert r.response["status"] == 200 + assert r.json["host"] == docs, \ + f"{addr} over {proto} served by {r.json['host']}" + + # an address no IP-based virtual host names: TCP falls back to the main + # server, which has no TLS here, so the HTTP/3 virtual host must not + # serve it either (421, as test_h3_103_04 asks for a vhost whose TLS + # setup differs from the HTTP/3 one) + def test_h3_018_02_unmatched_address(self, env): + port = free_port(env) + self.install_ip_vhosts(env, port) + r = get(env, port, "127.0.0.3", h3=True) + known_bug(r.exit_code == 0 and r.response["status"] == 200 + and r.json["host"] == "test1", + "an address no IP-based vhost names is served by the first " + "HTTP/3 vhost") + # refused at the handshake, or 421 on the request + assert r.exit_code != 0 or r.response["status"] == 421, f"{r}"