infra: pdf-host fence closes the VPC and IPv6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+98
−26
infra/modules/pdf-host/main.tf+1 −0infra/modules/pdf-host/pdf-init.sh.tftpl+42 −17infra/modules/pdf-host/tests/pdf_host.tftest.hcl+46 −9infra/modules/pdf-host/variables.tf+9 −0
CI failed — run details
diff --git a/infra/modules/pdf-host/main.tf b/infra/modules/pdf-host/main.tf| index d11191bfb..74b181dd7 100644 |
| --- a/infra/modules/pdf-host/main.tf |
| +++ b/infra/modules/pdf-host/main.tf |
| @@ -11,6 +11,7 @@ locals { |
| region = var.region |
| gotenberg_image = var.gotenberg_image |
| api_timeout = var.api_timeout |
| + memory_max = var.memory_max |
| }) |
| } |
diff --git a/infra/modules/pdf-host/pdf-init.sh.tftpl b/infra/modules/pdf-host/pdf-init.sh.tftpl| index 080a72b18..a2b168397 100644 |
| --- a/infra/modules/pdf-host/pdf-init.sh.tftpl |
| +++ b/infra/modules/pdf-host/pdf-init.sh.tftpl |
| @@ -34,15 +34,23 @@ apt-get install -qy docker.io nftables python3 |
| rm -f /usr/sbin/policy-rc.d |
| say "3/4 metadata and container fence" |
| -tok=$(curl -fsS -X PUT http://169.254.169.254/latest/api/token \ |
| +mkdir -p /etc/pdf-host /etc/systemd/system/docker.service.d |
| +# Run inside the fence unit, not just cloud-init: refresh associated CIDRs |
| +# on every boot and Docker start. Failed metadata must stop Docker too. |
| +cat > /usr/local/sbin/pdf-fence <<'EOF_FENCE' |
| +#!/bin/bash |
| +set -euo pipefail |
| +umask 077 |
| +tok=$(curl -fsS --connect-timeout 3 --max-time 10 -X PUT http://169.254.169.254/latest/api/token \ |
| -H 'X-aws-ec2-metadata-token-ttl-seconds: 300') |
| imds() { |
| - curl -fsS -H "X-aws-ec2-metadata-token: $tok" \ |
| + curl -fsS --connect-timeout 3 --max-time 10 -H "X-aws-ec2-metadata-token: $tok" \ |
| "http://169.254.169.254/latest/meta-data/$1" |
| } |
| ip=$(imds local-ipv4) |
| mac=$(imds mac) |
| vpc_cidr=$(imds "network/interfaces/macs/$mac/vpc-ipv4-cidr-block") |
| +vpc_cidrs=$(imds "network/interfaces/macs/$mac/vpc-ipv4-cidr-blocks") |
| # Parse metadata before using it in nft syntax or Docker's published address. |
| # The Amazon resolver is the primary VPC CIDR's base+2, not the subnet's. |
| resolver=$(python3 - "$ip" "$vpc_cidr" <<'PY' |
| @@ -54,7 +62,18 @@ print(vpc.network_address + 2) |
| PY |
| ) |
| -mkdir -p /etc/pdf-host /etc/systemd/system/docker.service.d |
| +vpc_drops=$(python3 - "$vpc_cidr" "$vpc_cidrs" <<'PY' |
| +import ipaddress |
| +import sys |
| +primary = ipaddress.IPv4Network(sys.argv[1], strict=True) |
| +networks = [ipaddress.IPv4Network(cidr, strict=True) for cidr in sys.argv[2].split()] |
| +if not networks or primary not in networks: |
| + sys.exit("VPC IPv4 CIDRs are empty or omit the primary CIDR") |
| +for network in networks: |
| + print(f" ip daddr {network} drop") |
| +PY |
| +) |
| + |
| cat > /etc/pdf-host/fence.nft <<EOF_NFT |
| # A private table survives Docker's iptables/nft rule rebuilds. Re-loading |
| # only this table is atomic: no interval with the old fence removed. |
| @@ -62,12 +81,15 @@ add table inet pdf_fence |
| flush table inet pdf_fence |
| table inet pdf_fence { |
| chain container_egress { |
| + meta nfproto ipv6 drop |
| # Replies to console requests must return to the console's private IP. |
| # Never exempt connections initiated by the renderer on conntrack state. |
| ct state established ct direction reply accept |
| ip daddr $resolver udp dport 53 accept |
| ip daddr $resolver tcp dport 53 accept |
| - # Source: internal/caps/render.go, docEngineEgressDeny (all 17 entries). |
| +$vpc_drops |
| + # Source: internal/caps/render.go, docEngineEgressDeny (IPv4 entries). |
| + # Its IPv6 entries are covered by the blanket drop above. |
| ip daddr 0.0.0.0/8 drop |
| ip daddr 10.0.0.0/8 drop |
| ip daddr 100.64.0.0/10 drop |
| @@ -79,18 +101,10 @@ table inet pdf_fence { |
| ip daddr 198.18.0.0/15 drop |
| ip daddr 224.0.0.0/4 drop |
| ip daddr 240.0.0.0/4 drop |
| - ip6 daddr 64:ff9b::/96 drop |
| - ip6 daddr fc00::/7 drop |
| - ip6 daddr fd00:ec2::254/128 drop |
| - ip6 daddr fe80::/10 drop |
| - ip6 daddr fec0::/10 drop |
| - ip6 daddr ff00::/8 drop |
| } |
| chain container_host { |
| - # Allow replies to host health probes, never new container-to-host calls. |
| - ct state established ct direction reply accept |
| - ip daddr $resolver udp dport 53 accept |
| - ip daddr $resolver tcp dport 53 accept |
| + # Allow IPv4 health-probe replies only; all IPv6 reaches the final drop. |
| + meta nfproto ipv4 ct state established ct direction reply accept |
| drop |
| } |
| chain forward { |
| @@ -103,15 +117,22 @@ table inet pdf_fence { |
| } |
| } |
| EOF_NFT |
| +/usr/sbin/nft -f /etc/pdf-host/fence.nft |
| +# Both values were parsed as IPv4 addresses before reaching this file. |
| +printf 'ip=%s\nresolver=%s\n' "$ip" "$resolver" > /run/pdf-host.env |
| +EOF_FENCE |
| +chmod 0755 /usr/local/sbin/pdf-fence |
| cat > /etc/systemd/system/pdf-fence.service <<'EOF_UNIT' |
| [Unit] |
| Description=PDF container network fence |
| Before=docker.service |
| +Wants=network-online.target |
| +After=network-online.target |
| [Service] |
| Type=oneshot |
| -ExecStart=/usr/sbin/nft -f /etc/pdf-host/fence.nft |
| +ExecStart=/usr/local/sbin/pdf-fence |
| RemainAfterExit=yes |
| [Install] |
| @@ -125,7 +146,7 @@ After=pdf-fence.service |
| [Service] |
| # Revalidate/reload on every Docker start, even while the oneshot is active. |
| -ExecStartPre=/usr/sbin/nft -f /etc/pdf-host/fence.nft |
| +ExecStartPre=/usr/local/sbin/pdf-fence |
| EOF_UNIT |
| systemctl daemon-reload |
| systemctl unmask docker.service docker.socket |
| @@ -133,10 +154,14 @@ systemctl enable pdf-fence.service docker.service |
| systemctl start docker.service |
| say "4/4 Gotenberg" |
| +source /run/pdf-host.env |
| docker run -d --name gotenberg --restart=always --network bridge \ |
| + --memory='${memory_max}' --memory-swap='${memory_max}' \ |
| + --cap-drop=ALL --security-opt no-new-privileges \ |
| -p "$ip:3000:3000" --dns "$resolver" \ |
| '${gotenberg_image}' gotenberg --api-timeout='${api_timeout}' \ |
| - --chromium-deny-list='^file:(?!//\/tmp/).*' --webhook-deny-list='.*' |
| + --chromium-deny-list='^file:(?!//\/tmp/).*' \ |
| + --chromium-deny-private-ips --api-download-from-deny-private-ips --webhook-disable |
| # This probe uses the published private address, not the container loopback. |
| curl --fail --silent --show-error --retry 30 --retry-connrefused --retry-delay 2 \ |
| --max-time 5 "http://$ip:3000/health" |
diff --git a/infra/modules/pdf-host/tests/pdf_host.tftest.hcl b/infra/modules/pdf-host/tests/pdf_host.tftest.hcl| index f8ddf8c5b..109dd0625 100644 |
| --- a/infra/modules/pdf-host/tests/pdf_host.tftest.hcl |
| +++ b/infra/modules/pdf-host/tests/pdf_host.tftest.hcl |
| @@ -104,7 +104,12 @@ run "container_config" { |
| strcontains(local.init_script, "-p \"$ip:3000:3000\" --dns \"$resolver\""), |
| strcontains(local.init_script, "gotenberg --api-timeout='55s'"), |
| strcontains(local.init_script, "--chromium-deny-list='^file:(?!//\\/tmp/).*'"), |
| - strcontains(local.init_script, "--webhook-deny-list='.*'"), |
| + strcontains(local.init_script, "--chromium-deny-private-ips"), |
| + strcontains(local.init_script, "--api-download-from-deny-private-ips"), |
| + strcontains(local.init_script, "--webhook-disable"), |
| + !strcontains(local.init_script, "--webhook-deny-list"), |
| + strcontains(local.init_script, "--memory='3g' --memory-swap='3g'"), |
| + strcontains(local.init_script, "--cap-drop=ALL --security-opt no-new-privileges"), |
| can(regex("^gotenberg/gotenberg@sha256:[0-9a-f]{64}$", var.gotenberg_image)), |
| ]) |
| error_message = "private bind, explicit bridge/DNS, digest pin and checked engine flags" |
| @@ -114,19 +119,19 @@ run "container_config" { |
| for cidr in [ |
| "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "169.254.0.0/16", "169.254.169.254/32", |
| "172.16.0.0/12", "192.0.0.0/24", "192.168.0.0/16", "198.18.0.0/15", "224.0.0.0/4", "240.0.0.0/4", |
| - "64:ff9b::/96", "fc00::/7", "fd00:ec2::254/128", "fe80::/10", "fec0::/10", "ff00::/8", |
| ] : strcontains(local.init_script, "daddr ${cidr} drop") |
| ]) |
| - error_message = "all 17 docEngineEgressDeny destinations must be dropped" |
| + error_message = "all IPv4 docEngineEgressDeny destinations must be dropped" |
| } |
| assert { |
| condition = alltrue([ |
| + strcontains(local.init_script, "table inet pdf_fence {"), |
| strcontains(local.init_script, "type filter hook forward priority -10; policy accept;"), |
| strcontains(local.init_script, "type filter hook input priority -10; policy accept;"), |
| strcontains(local.init_script, "iifname \"docker0\" jump container_egress"), |
| strcontains(local.init_script, "iifname \"docker0\" jump container_host"), |
| - length(regexall("ip daddr \\$resolver udp dport 53 accept", local.init_script)) == 2, |
| - length(regexall("ip daddr \\$resolver tcp dport 53 accept", local.init_script)) == 2, |
| + length(regexall("ip daddr \\$resolver udp dport 53 accept", local.init_script)) == 1, |
| + length(regexall("ip daddr \\$resolver tcp dport 53 accept", local.init_script)) == 1, |
| !strcontains(local.init_script, "hook output"), |
| !strcontains(local.init_script, "flush ruleset"), |
| ]) |
| @@ -134,10 +139,13 @@ run "container_config" { |
| } |
| assert { |
| condition = alltrue([ |
| - can(regex("chain container_host \\{[^}]*ct state established ct direction reply accept[^}]*ip daddr \\$resolver udp dport 53 accept[^}]*ip daddr \\$resolver tcp dport 53 accept\\n drop\\n", local.init_script)), |
| + can(regex("chain container_host \\{[^}]*ct state established ct direction reply accept\\n drop\\n", local.init_script)), |
| can(regex("chain container_egress \\{[^}]*ip daddr \\$resolver tcp dport 53 accept[^}]*ip daddr 0\\.0\\.0\\.0/8 drop", local.init_script)), |
| !strcontains(local.init_script, "ct state established,related accept"), |
| - strcontains(local.init_script, "network/interfaces/macs/$mac/vpc-ipv4-cidr-block"), |
| + strcontains(local.init_script, "vpc_cidrs=$(imds \"network/interfaces/macs/$mac/vpc-ipv4-cidr-blocks\")"), |
| + can(regex("chain container_egress \\{[^}]*ip daddr \\$resolver tcp dport 53 accept\\n\\$vpc_drops", local.init_script)), |
| + can(regex("chain container_egress \\{\\n meta nfproto ipv6 drop", local.init_script)), |
| + strcontains(local.init_script, "meta nfproto ipv4 ct state established ct direction reply accept"), |
| strcontains(local.init_script, "print(vpc.network_address + 2)"), |
| ]) |
| error_message = "DNS precedes the deny list; host traffic ends in drop; only replies bypass the fence" |
| @@ -145,9 +153,11 @@ run "container_config" { |
| assert { |
| condition = alltrue([ |
| strcontains(local.init_script, "Before=docker.service"), |
| + strcontains(local.init_script, "ExecStart=/usr/local/sbin/pdf-fence"), |
| + strcontains(local.init_script, "After=network-online.target"), |
| strcontains(local.init_script, "Requires=pdf-fence.service"), |
| strcontains(local.init_script, "After=pdf-fence.service"), |
| - strcontains(local.init_script, "ExecStartPre=/usr/sbin/nft -f /etc/pdf-host/fence.nft"), |
| + strcontains(local.init_script, "ExecStartPre=/usr/local/sbin/pdf-fence"), |
| strcontains(local.init_script, "systemctl mask docker.service docker.socket nftables.service"), |
| ]) |
| error_message = "Docker must refuse startup if the fence fails, including restarts" |
| @@ -173,7 +183,7 @@ run "changed_init_does_not_update_box" { |
| ami_id = "ami-11111111111111111" |
| } |
| assert { |
| - condition = aws_instance.pdf.user_data_base64 == base64gzip(templatefile("${path.module}/pdf-init.sh.tftpl", { region = var.region, gotenberg_image = var.gotenberg_image, api_timeout = "55s" })) && aws_instance.pdf.ami == "ami-0123456789abcdef0" |
| + condition = aws_instance.pdf.user_data_base64 == base64gzip(templatefile("${path.module}/pdf-init.sh.tftpl", { region = var.region, gotenberg_image = var.gotenberg_image, api_timeout = "55s", memory_max = var.memory_max })) && aws_instance.pdf.ami == "ami-0123456789abcdef0" |
| error_message = "changing the init template or AMI must leave the existing instance alone" |
| } |
| } |
| @@ -186,3 +196,30 @@ run "another_vpc_needs_a_subnet" { |
| } |
| expect_failures = [aws_instance.pdf] |
| } |
| + |
| +run "custom_memory_limit" { |
| + command = plan |
| + variables { |
| + memory_max = "2g" |
| + } |
| + assert { |
| + condition = strcontains(local.init_script, "--memory='2g' --memory-swap='2g'") |
| + error_message = "memory_max must cap both RAM and combined RAM/swap" |
| + } |
| +} |
| + |
| +run "memory_limit_cannot_disable_cap" { |
| + command = plan |
| + variables { |
| + memory_max = "0" |
| + } |
| + expect_failures = [var.memory_max] |
| +} |
| + |
| +run "memory_limit_cannot_inject_shell" { |
| + command = plan |
| + variables { |
| + memory_max = "3g'; exit 0; #" |
| + } |
| + expect_failures = [var.memory_max] |
| +} |
diff --git a/infra/modules/pdf-host/variables.tf b/infra/modules/pdf-host/variables.tf| index 19f7ec23d..8426adfb8 100644 |
| --- a/infra/modules/pdf-host/variables.tf |
| +++ b/infra/modules/pdf-host/variables.tf |
| @@ -46,3 +46,12 @@ variable "api_timeout" { |
| error_message = "api_timeout must be a duration using ms, s, m or h." |
| } |
| } |
| + |
| +variable "memory_max" { |
| + type = string |
| + default = "3g" |
| + validation { |
| + condition = can(regex("^[1-9][0-9]*[mMgG]$", var.memory_max)) |
| + error_message = "memory_max must be a positive whole number of MiB or GiB using m or g." |
| + } |
| +} |