Repository navigation
fix(start-wrt): answer plain HTTP at the public address with a redirect to HTTPS while 443 is published - #4000
Conversation
… 443 is published StartOS asks its IPv4 gateway for no port-80 mapping: it publishes only 443 and expects the gateway to answer plain HTTP at the public address with a redirect to HTTPS, as StartTunnel does. StartWRT never implemented its half, so a LAN client opening http://sub.example.com for a domain pointing at the WAN address hairpinned to the router's own port 80 and got the router UI. Port control now binds a listener to <WAN IPv4>:80 beside the daemon's wildcard :80 (both SO_REUSEPORT; the kernel delivers a connection to the most specific socket) and hands each connection to start-core's shared handle_http_on_https, a 307 to the same host and path over HTTPS. The socket is never wired into the axum app, so it can serve nothing but the redirect, and the LAN gateway addresses and router.lan still reach the UI. No firewall rule is touched: who may reach WAN:80 remains the Remote Access policy; this changes only what answers there. The listener is bound only while WAN 443 leaves the router (an enabled WAN DNAT covering tcp/443, or a live hostname route on 443) and yields to a DNAT on tcp/80, so a router publishing nothing on 443 behaves exactly as before. Reconciled at port control startup, on the sweep, on hostname route changes, and when a manual published port is saved.
… DNAT The redirect listener on WAN:80 answered only connections the wan zone already admitted, so under Remote Access "Never" (or "When behind NAT" with a public address) a client on the Internet opening http://sub.example.com was refused. StartOS publishes only 443 and leaves port 80 to its gateway, and that half of the contract was unmet from outside. An ACCEPT on wan tcp/80 would be fail-open: UCI rules persist across a daemon restart and fw4 loads them before the daemon binds, so the UI's wildcard :80 socket would answer in that window or after a failed bind. Instead, port control writes a wan-zone DNAT (`startwrt_http_redirect`, no dest_ip, which fw4 renders as an nft `redirect to :8880`) while 443 is published, and the redirect binds 0.0.0.0:8880 beside its WAN:80 socket. fw4 accepts DNAT'd connections at input, so no ACCEPT rule opens 80, and with the daemon down the DNAT lands on a closed port. The section carries its own marker option rather than `_apf_label`, which the sweep would lease and collect and the UI would list as an automatic forward. `wan_dnat_covers` ignores it so the redirect does not gate itself off, `apply_forward_uci` still counts it so PCP/UPnP grants for external 80 conflict while it is active, and `sni_port_conflicts` reserves 8880 so no hostname route can out-rank the wildcard socket. The section's presence no longer depends on knowing the WAN address, which avoids a remove-and-re-add firewall reload at every boot; only the WAN:80 hairpin socket needs the address. Reconciliation runs under `write_serial`. Callers that already hold it use the serialized variant, and `published_ports::set` skips the redirect's own firewall reload since it reloads anyway. A settled section is never rewritten, so the sweep does not reload fw4 every minute. Under "Never", port 80 now answers a 307 instead of rejecting while 443 is published. It echoes the requested Host and can serve nothing else.
|
@Dominion5254 — dr-bonez raised a design question on this in Matrix: why bind separate listeners (the The proposalOne outermost
Why the plumbing already exists
One snag
On the fail-open argumentThe body rejects an ACCEPT rule because "fw4 loads UCI before the daemon binds, so the UI would answer in that window". Traced: the UI wildcard binds at What it costs — stated plainly
What goes awayPort 8880 and its reservation (and A defect in the current design, independent of the decision
Also noting for accuracy: the body's "as StartTunnel does" is correct — Your call. If you'd rather I prototype the layer on a branch so the two can be compared on the K1, say so and I'll do that rather than leave it as prose. |
…path The redirect bound two sockets of its own — <WAN>:80 for LAN hairpins and 0.0.0.0:8880 behind a wan-zone tcp/80 DNAT for the Internet side — because a socket bound after the UI's wildcard :80 cannot be admitted by an ACCEPT rule without the UI answering in the gap between the two binds. Deciding per request removes the gap and with it the reason for both sockets: an outermost layer on the router answers a request whose destination is the WAN address with a 307 and passes everything else through, live from the first accepted connection. StartOS makes the same trade in net/domain_redirect.rs. Gone with them: port 8880 and its reservation, the hairpin rebind on every WAN renumber, the startwrt_http_redirect section and its marker, the family field on FirewallRedirect, the impostor-replacement logic, and wan_dnat_covers' exclusion of its own section. The Internet side is now a WAN ACCEPT rule on tcp/80, carried in the SNI admission set rather than reconciled separately, so sync_sni_rules is the one place the redirect is decided. It keeps the SNI label deliberately: a label older builds do not know is a rule older builds never purge, and such a rule outliving a downgrade would admit plain HTTP to a daemon with no layer behind it. Sharing the label means a rollback purges it on its first run, as it already does for a stale apf_sni_443. The gate the layer reads is that rule's presence, not "443 is published". The two disagree exactly at startup — a hostname route does not survive a restart but the rule it earned does — so the gate is seeded from UCI before the listener binds and tracks what the firewall actually admits. Until port control resolves the WAN address the layer redirects rather than falling through, since a double-NAT WAN is not separable from the LAN by address class. TcpMetadata.local_addr now comes from the accepted stream rather than the listener, which is the connection's destination on a wildcard socket and identical to the old value on a specifically bound one. served_name and arrival are its only readers and both want the destination. Also reserves port 80 against hostname routes while the redirect is up, which the DNAT did not: a UPnP grant could take external 80 and be silently dead WAN-side.
…ination alone The port-80 admission rule matches the wan zone, not a destination, and the layer redirected only a request whose destination was the WAN address. Four consequences, found auditing b700399 against the PR discussion: A WAN-side host that routes the LAN subnet through the router's WAN address arrives at the LAN address and was served the plaintext UI under "Never". The DNAT this replaced caught all wan-zone tcp/80, so that was a regression. The layer now serves the UI only to a client on a subnet the router is connected to off the WAN, at an address that is not the WAN's; every other IPv4 request gets the 307. The gate carries every WAN address rather than the first. After a restart the gate was seeded admitted with no WAN address, which redirected every plain-HTTP request, LAN included, until the first 60 s sweep and for as long as the WAN had no address. Addresses are now read before the listener binds, when port control starts, on every sweep and WAN hotplug, and once more (at most every 5 s) before a redirect is answered, so a bridge that comes up after the daemon is trusted on its first request. With the WAN down there is no WAN subnet to exclude and the LAN keeps the UI. The gate was set to the wanted state before the rule was reconciled, so unpublishing 443 shut it while fw4 still admitted WAN:80 — the UI answered there until the reload finished, or indefinitely if it failed. It now opens before the rule is written and shuts only after the reload that removes it. A hostname route on 80 writes the same section under the same label, and the redirect's rule was identified by port alone: the route's own rule read as the redirect's, so its renewal was refused as taken and a manual port-80 forward was refused rather than displacing it. The rule's name now tells the two apart, the redirect yields to a live route on 80, and the rule is rewritten when the port changes hands. The seed still opens the gate on any SNI rule on 80, since a lapsed route's rule admits the same traffic. The label stays shared, so a build without the redirect still purges the rule. Not migrated: the startwrt_http_redirect section written by 7207775. It reads as a user's port-80 forward and holds the redirect off, but no build on master ever wrote it; a bench router that ran that commit needs `uci delete firewall.startwrt_http_redirect`.
…he facts Review on #3999 asked for the mannered prose to go throughout, and this branch carried the same habits: a 23-line module doc, doc comments that argued their rationale and named their callers, tests narrating what their names state, and user docs saying the same thing three times. Each comment is now one fact. The changelog entry, release note, `published-ports.md` and `settings.md` state the behavior once. `ARCHITECTURE.md` also described the layer as keyed on the connection's destination, stale since 3f1fa0d, and omitted the per-request reread of stale addresses. Comment-only in every `.rs` file.
|
@helix-nine — took the proposal. The branch now decides port 80 per request on the existing listener; the head is Adopted as proposed ( The defect you flagged is fixed: Where it departs from the proposal (
Anyone who ran the earlier head ( |
|
Read Verified: On the departures:
Nothing else from me. Reads ready to merge from my side; approval is dr-bonez's call. |
…the UI `redirect()` returned `None` when the request carried no usable `Host`, and `respond()` read that as "not the redirect's request" and fell through to the UI. A WAN-side client could reach the plaintext router UI while 443 is published with `printf 'GET / HTTP/1.0\r\n\r\n' | nc <WAN> 80`, under every Remote Access setting. Once the layer has decided a request redirects, it now always answers: a 307 with an authority, a `400 Host header required` without one, which is what the standalone redirect socket this layer replaced answered.
…alone The redirect's admission rule matched the wan zone with no destination, so the firewall also admitted a WAN-side packet for the router's LAN address to the daemon, and only the layer's client-subnet check kept it from the UI. The rule now carries `dest_ip` set to the WAN address, so such a packet is rejected at the firewall as it was before the redirect existed; the layer's check stays as the second line. Port control rewrites the rule when the WAN address changes (the sweep and the WAN hotplug already invalidate the cached address) and writes none while the address is unknown. The gate still opens whenever the redirect is wanted, so a LAN hairpin to the WAN address redirects with or without the rule.
|
Two more commits since the reply above, from a bench walk-through of the layer:
Benched on the K1 (double NAT, Remote Access "Never", Linux probe on the outer LAN): Host-less and malformed-Host probes get 400; a routed packet for the LAN address is refused; the LAN still gets the UI with or without a |
|
Read Verified: 5 and 6 both hold. Two things the walk-through turned up. Neither is introduced by these two commits — both have been in the branch since 1. Boot race:
|
`port_control::run` was spawned before `http_redirect::seed` ran. `run` raises the gate before writing the admission rule; `seed` sets the gate to whether an SNI rule on 80 is already on disk. On the first boot of a router that already publishes 443, no such rule exists yet, so `seed`'s parse could read the old firewall and its `set_admitted(false)` land after `run`'s `set_admitted(true)`: rule live, gate shut, listener bound, and WAN:80 served the plaintext UI until the first sweep at 60 s. Seeding before the spawn leaves "rule absent, gate shut" as the worst boot state, which the firewall refuses. Found by helix-nine in review of #4000.
`wan_ipv4_addrs` returns an empty list on a ubus failure, which the gate could not tell from a WAN with no address. With no WAN address to subtract, the WAN interface's own subnet stayed in the trusted set, so a peer on that subnet dialling `<WAN>:80` got the UI instead of a 307 while the admission rule was still live from a cached lookup, until the next successful read. `read_wan_ipv4_addrs` returns `None` on a ubus failure and the gate then trusts no subnet; an empty list still means the WAN is down and keeps serving the LAN at its own address. Found by helix-nine in review of #4000.
|
Both confirmed and fixed. Head is now e1eb9e9.
Minor items left as they are. 603 + 18 tests pass. |
|
Read
Verified on the head: Nothing further from me — ready to merge from my side; approval is dr-bonez's call. |
# Conflicts: # projects/start-wrt/CHANGELOG.md
…ts release note into highlights
Why
StartOS publishes only port 443 through its gateway and expects the gateway to answer plain HTTP at the public address with a redirect to HTTPS. StartWRT never did. From inside the network,
http://sub.example.comhairpinned to the router's own port 80 and got the router UI; from the Internet it was refused unless Remote Access admitted the source.What
[::]:80listener (http_redirect.rs), outermost on the router. It serves the UI to a client on a connected subnet off the WAN, at an address that is not the WAN's. Every other IPv4 request gets a 307 to the same authority and path over HTTPS, with no body. No new sockets.SNIlabel, so a build without the redirect purges it on start rather than leaving port 80 open to a daemon with no layer. Itsnametells it from a hostname route's rule on 80.SNIrule on 80. Addresses are read at seeding, on every sweep and WAN hotplug, and again (throttled to 5 s) before a redirect is answered. Unreadable addresses or an unreadable firewall redirect.TcpMetadata.local_addron a plainTcpListeneris now the connection's destination (stream.local_addr()), asWildcardListeneralready reports it. Nothing else reads that field off a wildcardTcpListener.Remote Access is unchanged: who may reach the router UI or SSH from the WAN is decided by the existing rules. The one surface change is that, while 443 is published, port 80 at the WAN address answers a 307 under every mode, "Never" included.
Testing
Unit tests cover
desired(DNAT, protocol, range, disabled, hostname routes, port-80 precedence), the per-request decision (shut gate, WAN address, connected clients, WAN-side clients at a LAN address, unread addresses, WAN down, v4-mapped, IPv6), the layer's response, the rule reconcile (settle, withdraw, change of holder, purge by a build without the redirect), and the port-80 reservation.Benched on a K1 behind another NAT (Remote Access "Never") at
3f1fa0d38, all pass;181bbcdf3after it is comment- and docs-only:http://<WAN>/→ 307 with the client's own Host;router.lanand the LAN address serve the UIKnown gaps
Start here
http_redirect.rsredirectsanddesired;port_control.rssync_sni_rules_innerandreconcile_sni_rules_uci.