ListenerHTTP1/ListenerHTTP: allow choosing the bind address (always 0.0.0.0 today) #11

Open
opened 2026-11-26 08:34:25 +00:00 by jorijnvdgraaf · 0 comments

Problem

ListenerHTTP1 and ListenerAsyncHTTP1 take a port and nothing else. Every constructor ends in the port-only ListenerTCP overload:

impl->listener = std::make_unique<ListenerTCP>(port, [state](ClientTCP* client) {
    state->Adopt(client);
});

and that overload binds every IPv4 interface (0.0.0.0). A consumer has no way to ask for 127.0.0.1.

For an HTTP/1.1 listener that is the wrong address more often than not. The usual deployment is a backend behind a reverse proxy that terminates TLS, and such a backend is only correct while nothing but the proxy can reach it:

  • it speaks plaintext;
  • it believes what the proxy says about the client (the last X-Forwarded-For entry), which anyone who connects directly can write themselves;
  • access rules that live in the proxy (per-path allowlists, body limits, logging) do not exist for a direct connection.

catcrafts.net is this case: Caddy in front, ListenerHTTP1 behind. The server was written to listen on loopback, says so in its own startup line, and cannot make it true. ss -ltn shows 0.0.0.0:<port>, and the only thing that keeps the port private is a host firewall rule, which is one layer where there should be two.

The lower layers already take an address. Only the HTTP listeners cannot pass one down:

ListenerTCP(std::string_view bindAddress, std::uint16_t port, connectCallback, ...);
ListenerQUIC(std::string_view bindAddress, std::uint16_t port, alpn, creds, connectCallback, ...);

Proposal

Overloads with the address first, as ListenerTCP and ListenerQUIC have them, forwarding to those:

ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes);
ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes, fallback);
ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes, TLSServerCredentials);
ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes, fallback, TLSServerCredentials);

the same four on ListenerAsyncHTTP1, and for symmetry on ListenerHTTP / ListenerAsyncHTTP, which sit on ListenerQUIC and have the same gap.

Same rules as the layers below, so there is one behaviour to learn: a numeric IPv4 or IPv6 address, a name such as localhost refused, and port 0 letting the kernel pick. That last one needs a Port() on the HTTP listeners to be useful (ListenerTCP and ListenerQUIC have it), which would also let tests stop choosing ports by hand.

Fully backwards compatible: the port-only constructors keep binding what they bind today.

Smaller things in the same place

  • The port-only constructors of ListenerHTTP1 do not say what they bind. ListenerTCP's comment does ("Every IPv4 interface (0.0.0.0) at port"), and so does AGENTS.consumer.md for ListenerTCP; the HTTP listener's header comment and its consumer section are where someone deploying behind a proxy would look, and neither mentions it. One sentence there would have caught this.
  • Not asking for the default to change. Loopback-by-default would be the safer choice for a plaintext listener, but it breaks every consumer that serves directly, and an explicit address is enough.

What catcrafts.net does with it

ListenerHTTP1 listener("127.0.0.1", port, std::move(routes), std::move(fallback));

plus a suite that asserts the socket does not answer on a non-loopback address, so the startup line cannot drift from the truth again.

## Problem `ListenerHTTP1` and `ListenerAsyncHTTP1` take a port and nothing else. Every constructor ends in the port-only `ListenerTCP` overload: ```cpp impl->listener = std::make_unique<ListenerTCP>(port, [state](ClientTCP* client) { state->Adopt(client); }); ``` and that overload binds every IPv4 interface (`0.0.0.0`). A consumer has no way to ask for `127.0.0.1`. For an HTTP/1.1 listener that is the wrong address more often than not. The usual deployment is a backend behind a reverse proxy that terminates TLS, and such a backend is only correct while nothing but the proxy can reach it: - it speaks plaintext; - it believes what the proxy says about the client (the last `X-Forwarded-For` entry), which anyone who connects directly can write themselves; - access rules that live in the proxy (per-path allowlists, body limits, logging) do not exist for a direct connection. `catcrafts.net` is this case: Caddy in front, `ListenerHTTP1` behind. The server was written to listen on loopback, says so in its own startup line, and cannot make it true. `ss -ltn` shows `0.0.0.0:<port>`, and the only thing that keeps the port private is a host firewall rule, which is one layer where there should be two. The lower layers already take an address. Only the HTTP listeners cannot pass one down: ```cpp ListenerTCP(std::string_view bindAddress, std::uint16_t port, connectCallback, ...); ListenerQUIC(std::string_view bindAddress, std::uint16_t port, alpn, creds, connectCallback, ...); ``` ## Proposal Overloads with the address first, as `ListenerTCP` and `ListenerQUIC` have them, forwarding to those: ```cpp ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes); ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes, fallback); ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes, TLSServerCredentials); ListenerHTTP1(std::string_view bindAddress, std::uint16_t port, routes, fallback, TLSServerCredentials); ``` the same four on `ListenerAsyncHTTP1`, and for symmetry on `ListenerHTTP` / `ListenerAsyncHTTP`, which sit on `ListenerQUIC` and have the same gap. Same rules as the layers below, so there is one behaviour to learn: a numeric IPv4 or IPv6 address, a name such as `localhost` refused, and port `0` letting the kernel pick. That last one needs a `Port()` on the HTTP listeners to be useful (`ListenerTCP` and `ListenerQUIC` have it), which would also let tests stop choosing ports by hand. Fully backwards compatible: the port-only constructors keep binding what they bind today. ## Smaller things in the same place - The port-only constructors of `ListenerHTTP1` do not say what they bind. `ListenerTCP`'s comment does ("Every IPv4 interface (0.0.0.0) at `port`"), and so does `AGENTS.consumer.md` for `ListenerTCP`; the HTTP listener's header comment and its consumer section are where someone deploying behind a proxy would look, and neither mentions it. One sentence there would have caught this. - Not asking for the default to change. Loopback-by-default would be the safer choice for a plaintext listener, but it breaks every consumer that serves directly, and an explicit address is enough. ## What catcrafts.net does with it ```cpp ListenerHTTP1 listener("127.0.0.1", port, std::move(routes), std::move(fallback)); ``` plus a suite that asserts the socket does not answer on a non-loopback address, so the startup line cannot drift from the truth again.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Network#11
No description provided.