Keep other machines on the network from reaching Docker containers
shellcheck / shellcheck (push) Failing after 19s

60-security.sh now checks network exposure (step 4), from two other
machines made of network namespaces in the VM: one on private addresses,
like a neighbour on the same Wi-Fi, and one on public-looking ones. From
each, over IPv4 and IPv6: only SSH and LocalSend are open, a container's
published port can't be reached, containers still reach both machines,
and SSH takes keys only. It also checks ufw's rules and sshd -T.

It found that ufw-docker's rules let any private address (10/8,
172.16/12, 192.168/16) reach published ports, so anyone on a café's or
hotel's Wi-Fi could. system/docker-firewall.sh lets only Docker's own
bridges (docker0, br-+) past instead; firewall.sh runs it on a new
install and migration 1794878760 on existing machines. To let other
machines reach a container's port: sudo ufw-docker allow <container> <port>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 07:32:44 -07:00
co-authored by Claude Opus 5.5
parent 203754c632
commit ffbaf71ba7
6 changed files with 413 additions and 27 deletions
+110
View File
@@ -0,0 +1,110 @@
#!/usr/bin/env bats
load test_helper
# system/docker-firewall.sh, on a copy of ufw's after.rules with ufw-docker's
# block as `ufw-docker install` writes it. sudo runs the command as you, and
# install copies without changing the owner.
setup() {
common_setup
export UFW_AFTER_RULES="$BATS_TEST_TMPDIR/after.rules"
stub sudo 'exec "$@"'
stub install 'eval "src=\${$(($# - 1))}" "dst=\${$#}"; cp "$src" "$dst"'
stub systemctl '[ "$1" = is-active ] && exit 0; exit 0'
SCRIPT="$REPO/system/docker-firewall.sh"
}
# ufw's after.rules, with ufw-docker's block between its own lines
write_rules() {
cat > "$UFW_AFTER_RULES" << 'EOF'
# rules.input-after
*filter
:ufw-after-input - [0:0]
-A ufw-after-input -p udp --dport 137 -j ufw-skip-to-policy-input
COMMIT
# BEGIN UFW AND DOCKER
*filter
:ufw-user-forward - [0:0]
:ufw-docker-logging-deny - [0:0]
:DOCKER-USER - [0:0]
-A DOCKER-USER -j ufw-user-forward
-A DOCKER-USER -m conntrack --ctstate RELATED,ESTABLISHED -j RETURN
-A DOCKER-USER -m conntrack --ctstate INVALID -j DROP
-A DOCKER-USER -i docker0 -o docker0 -j ACCEPT
-A DOCKER-USER -j RETURN -s 10.0.0.0/8
-A DOCKER-USER -j RETURN -s 172.16.0.0/12
-A DOCKER-USER -j RETURN -s 192.168.0.0/16
-A DOCKER-USER -j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 10.0.0.0/8
-A DOCKER-USER -j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 172.16.0.0/12
-A DOCKER-USER -j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 192.168.0.0/16
-A DOCKER-USER -j RETURN
-A ufw-docker-logging-deny -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW DOCKER BLOCK] "
-A ufw-docker-logging-deny -j DROP
COMMIT
# END UFW AND DOCKER
EOF
chmod 640 "$UFW_AFTER_RULES"
}
@test "only Docker's bridges get past, not every private address, and ufw restarts" {
write_rules
original="$(cat "$UFW_AFTER_RULES")"
run bash "$SCRIPT"
[ "$status" -eq 0 ]
refute grep -q -- '-j RETURN -s ' "$UFW_AFTER_RULES"
# In the private lines' place, and nothing else changed
diff <(grep -v -- '-j RETURN -s ' <<< "$original") \
<(grep -vxF -e '-A DOCKER-USER -j RETURN -i docker0' -e '-A DOCKER-USER -j RETURN -i br-+' "$UFW_AFTER_RULES")
[ "$(grep -n -- '-j RETURN -i docker0$' "$UFW_AFTER_RULES" | cut -d: -f1)" -eq 17 ]
[ "$(grep -n -- '-j RETURN -i br-+$' "$UFW_AFTER_RULES" | cut -d: -f1)" -eq 18 ]
# The private ranges are still kept from reaching containers otherwise
grep -q -- '-j ufw-docker-logging-deny -m conntrack --ctstate NEW -d 192.168.0.0/16' "$UFW_AFTER_RULES"
[ "$(stat -c %a "$UFW_AFTER_RULES")" = 640 ]
called systemctl restart ufw.service
}
@test "running it again changes nothing" {
write_rules
bash "$SCRIPT"
once="$(cat "$UFW_AFTER_RULES")"
: > "$STUB_LOG"
run bash "$SCRIPT"
[ "$status" -eq 0 ]
[ "$(cat "$UFW_AFTER_RULES")" = "$once" ]
not_called systemctl
}
@test "it leaves after.rules alone without ufw-docker's rules" {
printf '*filter\n-A ufw-after-input -j RETURN -s 10.0.0.0/8\nCOMMIT\n' > "$UFW_AFTER_RULES"
before="$(cat "$UFW_AFTER_RULES")"
run bash "$SCRIPT"
[ "$status" -eq 0 ]
[ "$(cat "$UFW_AFTER_RULES")" = "$before" ]
}
@test "it takes out only the private lines that are left, keeping your own" {
write_rules
sed -i -e '/RETURN -s 192.168.0.0\/16/d' "$UFW_AFTER_RULES"
sed -i -e 's|^-A DOCKER-USER -j RETURN -s 10.0.0.0/8$|&\n-A DOCKER-USER -j RETURN -s 192.168.1.20/32|' "$UFW_AFTER_RULES"
run bash "$SCRIPT"
[ "$status" -eq 0 ]
grep -qx -- '-A DOCKER-USER -j RETURN -s 192.168.1.20/32' "$UFW_AFTER_RULES"
refute grep -qE -- '-s (10.0.0.0/8|172.16.0.0/12)$' "$UFW_AFTER_RULES"
[ "$(grep -c -- '-j RETURN -i docker0$' "$UFW_AFTER_RULES")" -eq 1 ]
}
@test "if it can't read the rules, it changes nothing" {
write_rules
before="$(cat "$UFW_AFTER_RULES")"
stub sudo '[ "$1" = cat ] && exit 1; exec "$@"'
run bash "$SCRIPT"
[ "$status" -ne 0 ]
[ "$(cat "$UFW_AFTER_RULES")" = "$before" ]
not_called systemctl
}