Skip to content

fix(docker): bind published ports to loopback by default instead of 0.0.0.0 - #10

Open
nusendra wants to merge 1 commit into
mainfrom
fix/bind-loopback-by-default
Open

nusendra wants to merge 1 commit into
mainfrom
fix/bind-loopback-by-default

Conversation

@nusendra

Copy link
Copy Markdown
Owner

What

Ticking "Expose to host" published the port on 0.0.0.0 — every interface — with no way to ask for anything else. The host IP was a literal in ployer-docker:

Some(vec![PortBinding {
    host_ip: Some("0.0.0.0".to_string()),

On the reporting host that meant PostgreSQL, MariaDB (3306), MongoDB (27017) and Jellyfin (8096) were all reachable from the LAN and the tailnet, with no host firewall. pg_stat_activity showed live sessions from another machine on the LAN. Docker publishes through its own DOCKER iptables chain, so a ufw rule added afterwards would not have closed it.

Published ports now carry the interface they are bound to, and that interface defaults to loopback.

How

ployer-docker

  • BindAddress — Loopback (127.0.0.1, the Default) | AllInterfaces (0.0.0.0) | Address(String) for a specific interface such as a tailnet IP.
  • ContainerConfig.ports is now HashMap<String, PublishedPort> (bind + host port) instead of HashMap<String, String>. Per-port rather than per-container, because that is how Docker models it and because it round-trips a compose file losslessly.
  • build_port_bindings is split out of create_container and pure, so the interface a port lands on is assertable without a Docker daemon.

ployer-templates

  • parse_port_spec replaces splitting on the first colon, which read 127.0.0.1:15432:5432 as host port 127.0.0.1 and container port 15432:5432. It handles container, host:container, ip:host:container and bracketed IPv6. split_port stays as a thin wrapper for callers that do not care about the interface.
  • add_ports_to_all_services takes the interface and always writes it out, so the stored compose records the choice instead of leaving it to whatever the deployer's default happens to be.

API — POST /templates/:slug/install, PUT /applications/:id/expose and POST /containers take an optional bind. Omitted means loopback. The ports wire shape on POST /containers is unchanged.

Deploy path — the compose path reads the interface back out of each port spec and announces every published port in the deploy log, with a warning when it is not loopback-only. Applications deployed from git are loopback-bound on their ephemeral port; Caddy runs on the host and proxies to localhost, so the reverse proxy path is unaffected.

UI — "Expose to host" gains a "Reachable from" choice (this host only / all interfaces), on both the install form and the application page. Choosing all interfaces shows a warning, with sharper wording for category: database. The application page reads the current interface out of the stored compose, so the form shows what is actually running.

Breaking change

A redeploy of an already-exposed service moves it from 0.0.0.0 to 127.0.0.1. Compose files written before this name no interface, and no interface means the default.

This is deliberate — a security fix that only applies to new installs would leave every service already on the host exposed, which is the actual reported problem. Nothing happens until the service is redeployed; running containers are untouched. To keep LAN access, pick "All interfaces" on the application's page and save; the redeploy writes the interface into the compose explicitly, so it sticks.

To reach a loopback-bound service from another machine without republishing it:

ssh -L 15432:127.0.0.1:15432 user@ployer-host

Also a public API change inside the workspace: ContainerConfig.ports changes type and add_ports_to_all_services takes an extra argument. Both are internal crates with no external consumers; all call sites are updated here.

Related: restart: was parsed and discarded

The audit recorded the template's restart: as "parsed then discarded", which was half the story. deployment.rs passed restart: None and ployer-docker mapped None to "unless-stopped", so the live container genuinely was unless-stopped — but a template asking for restart: "no" would have silently got unless-stopped too.

The compose path now passes the declared value through. on-failure:5 keeps its retry cap. An unrecognised policy is an error instead of falling through to unless-stopped — a typo in a template should fail the install loudly. No declaration still means unless-stopped, so git-deployed apps and every existing template are unchanged.

Verification

cargo test --workspace     176 passed, 0 failed  (exit 0)   # 143 before
cargo clippy --workspace --all-targets -- -D warnings       # exit 0
npm run build (frontend)                                    # ok
npm run check (frontend)   2 errors, 52 warnings            # identical to main's baseline

SQLX_OFFLINE=true for every cargo command, per DOCUMENTATION.md.

ployer-docker had 6 tests and none covered the bind address; it has 20. ployer-templates goes 6 -> 25. Four mutations were introduced and reverted to check the tests actually bite:

Mutation Tests that failed
Loopback resolves to 0.0.0.0 4
An unspecified host IP resolves to AllInterfaces 1
Unknown restart policy falls back to unless-stopped 1
parse_port_spec drops the interface 2
add_ports_to_all_services drops the interface 6

Not covered

The one-line wiring in deployment.rs that passes service.restart to ContainerConfig is not pinned by a test. ployer-api is a binary crate with no lib target, so there is nowhere to put an integration test for the compose deploy path without restructuring the crate — out of scope here. Both ends are covered (the compose parser keeps restart, and ployer-docker honours it); only the wire between them is not.

Nothing was deployed and no running container was touched.

…restart

"Expose to host" published every port on 0.0.0.0. The host IP was a literal
in ployer-docker's create_container, so there was no way to ask for anything
else — a database installed from the dashboard was reachable from the LAN,
from any VPN the host had joined, and from the internet if the router
forwarded the port. Docker publishes through its own DOCKER iptables chain,
which ufw does not filter, so adding a firewall rule afterwards would not have
closed it either.

Published ports now carry the interface they are bound to:

- `BindAddress` (Loopback | AllInterfaces | Address) defaults to Loopback.
- `ContainerConfig.ports` maps a container port to a `PublishedPort`
  (bind + host port) instead of a bare host-port string.
- The install form, the application's expose toggle and POST /containers all
  take an optional `bind`; omitted means loopback.
- The chosen interface is written into the stored compose as a three-part
  spec (`127.0.0.1:15432:5432`), so it survives a redeploy and can be read
  back. `parse_port_spec` replaces the old split-on-first-colon, which read
  "127.0.0.1:15432:5432" as host port "127.0.0.1".
- Every published port is announced in the deploy log, with a warning when it
  is not loopback-only.

Applications deployed from git are loopback-bound too. They publish on an
ephemeral port that only Caddy uses, and Caddy runs on the host and proxies to
localhost, so nothing about the reverse proxy path changes.

Behaviour change: a compose written before this names no interface, so the
next redeploy of an already-installed service moves it from 0.0.0.0 to
127.0.0.1. Re-pick "All interfaces" on the application page to keep LAN
access; the redeploy then writes the interface out explicitly. Running
containers are untouched until they are redeployed.

Separately, the template's declared `restart:` was parsed and then discarded —
deployment.rs passed None and ployer-docker mapped None to "unless-stopped",
so a template asking for `restart: "no"` silently got "unless-stopped". The
compose path now passes the declared value through, "on-failure:5" keeps its
retry cap, and an unrecognised policy is an error instead of a silent fallback
to unless-stopped. No declaration still means "unless-stopped".

Tests: ployer-docker 6 -> 20, ployer-templates 6 -> 25, workspace 143 -> 176.
The bind address had no coverage at all before. Four mutations were checked:
Loopback->0.0.0.0 (4 tests fail), unspecified host IP->AllInterfaces (1),
unknown restart policy falling back silently (1), and dropping the interface
in parse_port_spec / add_ports_to_all_services (2 and 6).
@nusendra

Copy link
Copy Markdown
Owner Author

Heads-up on a cross-PR break, the kind DOCUMENTATION.md warns about: each PR is green on its own head, and git merge-tree reports no conflict, but cargo test on main fails after both land.

#10 (bind published ports to loopback by default) adds a bind: BindAddress field to InstallRequest in routes/templates.rs, and makes the install endpoint write the chosen interface into the stored compose.

Two things in templates_tests.rs break, whichever of us merges second:

  1. request() at line 318 builds InstallRequest as a struct literal, so it needs bind: BindAddress::default(), — otherwise E0063, missing field.

  2. Three assertions expect the two-part port spec. The compose now records the interface explicitly:

    • expose_publishes_each_template_port_on_the_matching_host_port: vec!["8080:8080"] -> vec!["127.0.0.1:8080:8080"]
    • expose_honours_an_explicit_host_port_override: vec!["18080:8080"] -> vec!["127.0.0.1:18080:8080"]
    • expose_publishes_the_ports_on_every_service: both vec!["8080:8080", "9090:9090"] -> vec!["127.0.0.1:8080:8080", "127.0.0.1:9090:9090"]

expose_on_a_template_without_ports_leaves_the_compose_unchanged, without_expose_the_compose_publishes_no_ports and host_ports_are_ignored_when_expose_is_false are unaffected.

Worth adding while you are in there: req.bind = BindAddress::AllInterfaces should produce 0.0.0.0:8080:8080. That is the one install-endpoint behaviour #10 cannot pin from ployer-templates or ployer-docker — it only covers the two ends, not the handler that wires them.

No action needed yet; just so neither of us merges into a red main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant