Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(egress): exempt MITM redirect target from port-scoped egress rules

高然e01aa67a6297
diff --git a/.github/workflows/real-e2e.yml b/.github/workflows/real-e2e.yml
index 7e841f393..78063ad6e 100644
--- a/.github/workflows/real-e2e.yml
+++ b/.github/workflows/real-e2e.yml
@@ -54,6 +54,7 @@ jobs:
docker ps -aq --filter "label=opensandbox" | xargs -r docker rm -f || true
docker rm -f opensandbox-e2e-credential-vault-target || true
docker ps -aq --filter "label=opensandbox.e2e=credential-vault" | xargs -r docker rm -f || true
+ docker rm -f opensandbox-e2e-egress-mitm-target || true
docker rm -f opensandbox-e2e-redis || true
# Remove root-owned files from previous sandbox runs by mounting parent dir
docker run --rm -v /tmp:/host_tmp alpine rm -rf /host_tmp/opensandbox-e2e || true
@@ -124,6 +125,7 @@ jobs:
docker ps -aq --filter "label=opensandbox" | xargs -r docker rm -f || true
docker rm -f opensandbox-e2e-credential-vault-target || true
docker ps -aq --filter "label=opensandbox.e2e=credential-vault" | xargs -r docker rm -f || true
+ docker rm -f opensandbox-e2e-egress-mitm-target || true
docker rm -f opensandbox-e2e-redis || true
docker run --rm -v /tmp:/host_tmp alpine rm -rf /host_tmp/opensandbox-e2e || true
diff --git a/components/egress/docs/mitmproxy-transparent.md b/components/egress/docs/mitmproxy-transparent.md
index caac8da6d..06bd5c48b 100644
--- a/components/egress/docs/mitmproxy-transparent.md
+++ b/components/egress/docs/mitmproxy-transparent.md
@@ -214,6 +214,8 @@ Semantics and limits:
Transparent mitmproxy does not automatically consume egress `NetworkPolicy`. Domain allow/deny behavior is still determined by DNS + (optional) nft rules. If L7 policy enforcement is needed, implement it in mitm scripts.
+Under the `dns+nft` sidecar profile, the REDIRECT rewrites an intercepted flow's destination to the mitmproxy listener port (default `18081`) before the nft egress chain evaluates it, so the chain accepts that rewritten destination (`127.0.0.1:<port>`) ahead of every port-scoped rule — the `reachable.ports.cap` floor, user `ports` rules, and the default-deny tail never blackhole interception itself, even when the original ports (80/443) are exactly what the policy allows. Enforcement is preserved end to end: mitmproxy's own re-originated connections are not exempt, so a port outside the cap or a denied destination still fails, surfaced as a proxy error rather than a silent timeout.
+
## Implementation Notes and Limits
Startup flow (high level):
diff --git a/components/egress/nft.go b/components/egress/nft.go
index 21509302c..3f29b138f 100644
--- a/components/egress/nft.go
+++ b/components/egress/nft.go
@@ -114,6 +114,12 @@ func parseNftOptions(upstream *mitmproxy.UpstreamProxySpec) (nftables.Options, e
BlockDoT: true,
QuiesceOnApplyFailure: constants.IsTruthy(os.Getenv(constants.EnvExperimentalRevisionRuntime)),
}
+ // Transparent MITM rewrites intercepted dports to its listener port before
+ // the egress chain runs; the chain must exempt that rewritten tuple ahead
+ // of port-scoped rules. Same derivation as startMitmproxyTransparentIfEnabled.
+ if constants.IsTruthy(os.Getenv(constants.EnvMitmproxyTransparent)) {
+ opts.MitmRedirectPort = constants.EnvIntOrDefault(constants.EnvMitmproxyPort, constants.DefaultMitmproxyPort)
+ }
if constants.IsTruthy(os.Getenv(constants.EnvBlockDoH443)) {
opts.BlockDoH443 = true
}
diff --git a/components/egress/pkg/nftables/manager.go b/components/egress/pkg/nftables/manager.go
index 52c47920a..e1e7fcaf3 100644
--- a/components/egress/pkg/nftables/manager.go
+++ b/components/egress/pkg/nftables/manager.go
@@ -64,6 +64,15 @@ type Options struct {
// IP-only and unscoped by UID, so putting the proxy there would let
// sandbox workloads CONNECT it directly and bounce to denied destinations.
UpstreamProxy *UpstreamProxyEndpoint
+ // MitmRedirectPort, when > 0, is the transparent-MITM listener port the
+ // nat OUTPUT REDIRECT rewrites intercepted dports (80/443 + extras) to.
+ // The redirect runs at a lower priority than this filter chain and leaves
+ // oif on the original egress interface, so the rewritten dport would
+ // otherwise be judged as if the workload had dialed the listener itself:
+ // the port floor complement, user port-scoped rules, and the final
+ // default-deny drop would blackhole every intercepted flow. 0 (transparent
+ // MITM disabled) emits nothing.
+ MitmRedirectPort int
}
type Manager struct {
@@ -294,6 +303,16 @@ func buildRuleset(p *policy.NetworkPolicy, opts Options) (string, error) {
// The ip6 OUTPUT REDIRECTs (DNS → :15353, MITM → :18081) land on ::1 with the original egress
// interface still selected, so oifname "lo" does not match them: ::1 is the v6 loopback rule.
fmt.Fprintf(&b, "add rule inet %s %s ip6 daddr ::1 accept\n", tableName, chainName)
+ // The v4 MITM redirect lands on 127.0.0.1 with the original egress interface still
+ // selected, so oifname "lo" does not match either; exempt the redirect target ahead
+ // of every port-scoped rule (mirrors the dnsproxy rules above). Redirected flows
+ // are still policed through mitmproxy's own re-originated connections, which are
+ // not exempt. Direct workload dials of the listener already pass via oifname "lo"
+ // (the route to 127.0.0.1 is lo), so this adds no new reachability; v6 is covered
+ // by the ::1 rule above.
+ if opts.MitmRedirectPort > 0 {
+ fmt.Fprintf(&b, "add rule inet %s %s ip daddr 127.0.0.1 tcp dport %d accept\n", tableName, chainName, opts.MitmRedirectPort)
+ }
if ep := opts.UpstreamProxy; ep != nil {
b.WriteString(buildUpstreamProxyStatic(tableName, ep))
}
diff --git a/components/egress/pkg/nftables/manager_test.go b/components/egress/pkg/nftables/manager_test.go
index 45ae797d3..611f10209 100644
--- a/components/egress/pkg/nftables/manager_test.go
+++ b/components/egress/pkg/nftables/manager_test.go
@@ -822,3 +822,49 @@ func TestApplyStatic_PortFloorRangesDenyBeforeAllows(t *testing.T) {
require.Less(t, strings.Index(rendered, floorRule), strings.Index(rendered, userAllow),
"the platform floor must be evaluated before any user port allow")
}
+
+// Issue #2423: the nat OUTPUT REDIRECT (transparent MITM) rewrites intercepted
+// dports to the listener port BEFORE this chain runs (oif still on the egress
+// interface), so the rewritten dport must be accepted ahead of the port floor
+// complement, user port-scoped rules, and the final default-deny drop — or
+// every intercepted flow is silently blackholed even when the original port
+// (e.g. 443) is allowed by both the cap and the user policy.
+func TestApplyStatic_MitmRedirectExemptedBeforePortRules(t *testing.T) {
+ var rendered string
+ m := NewManagerWithRunnerAndOptions(func(_ context.Context, script string) ([]byte, error) {
+ rendered = script
+ return nil, nil
+ }, Options{MitmRedirectPort: 18081})
+ // Default-deny user policy that only opens 443, with the platform port
+ // floor (whitelisting 80/443) as an always-deny overlay: neither covers
+ // the rewritten dport 18081.
+ p, err := policy.ParsePolicy(`{"defaultAction":"deny","egress":[{"action":"allow","ports":[443]}]}`)
+ require.NoError(t, err)
+ p = policy.MergeAlwaysOverlay(p, []policy.EgressRule{
+ policy.NewPortFloorDenyRule([]policy.PortRange{{Lo: 1, Hi: 79}, {Lo: 81, Hi: 442}, {Lo: 444, Hi: 65535}}),
+ }, nil)
+ require.NoError(t, m.ApplyStatic(context.Background(), p))
+
+ exempt := "add rule inet opensandbox egress ip daddr 127.0.0.1 tcp dport 18081 accept"
+ expectContains(t, rendered, exempt)
+ for _, later := range []string{
+ "add rule inet opensandbox egress tcp dport { 1-79, 81-442, 444-65535 } drop",
+ "add rule inet opensandbox egress tcp dport { 443 } accept",
+ "add rule inet opensandbox egress drop",
+ } {
+ idx := strings.Index(rendered, later)
+ require.NotEqual(t, -1, idx, "expected rendered ruleset to contain %q", later)
+ require.Less(t, strings.Index(rendered, exempt), idx,
+ "the mitm redirect exemption must precede %q", later)
+ }
+}
+
+func TestApplyStatic_NoMitmRedirectExemptionWhenDisabled(t *testing.T) {
+ var rendered string
+ m := NewManagerWithRunner(func(_ context.Context, script string) ([]byte, error) {
+ rendered = script
+ return nil, nil
+ })
+ require.NoError(t, m.ApplyStatic(context.Background(), policy.DefaultDenyPolicy()))
+ require.NotContains(t, rendered, "dport 18081", "no exemption without transparent MITM")
+}
diff --git a/components/egress/revision_nft_options_test.go b/components/egress/revision_nft_options_test.go
index bcb011e61..0d3cf2037 100644
--- a/components/egress/revision_nft_options_test.go
+++ b/components/egress/revision_nft_options_test.go
@@ -56,3 +56,22 @@ func TestNftOptionsRevisionQuiescence(t *testing.T) {
})
}
}
+
+// The egress chain exempts the MITM redirect target ahead of port-scoped
+// rules (issue #2423); the port must be derived from the same env the
+// transparent launcher uses, and only when transparent MITM is enabled.
+func TestNftOptionsMitmRedirectPortFollowsTransparentMode(t *testing.T) {
+ opts, err := parseNftOptions(nil)
+ require.NoError(t, err)
+ require.Zero(t, opts.MitmRedirectPort, "no exemption when transparent MITM is disabled")
+
+ t.Setenv(constants.EnvMitmproxyTransparent, "true")
+ opts, err = parseNftOptions(nil)
+ require.NoError(t, err)
+ require.Equal(t, constants.DefaultMitmproxyPort, opts.MitmRedirectPort)
+
+ t.Setenv(constants.EnvMitmproxyPort, "18443")
+ opts, err = parseNftOptions(nil)
+ require.NoError(t, err)
+ require.Equal(t, 18443, opts.MitmRedirectPort)
+}
diff --git a/docs/architecture/network/egress.md b/docs/architecture/network/egress.md
index 89dee2791..217c46c3d 100644
--- a/docs/architecture/network/egress.md
+++ b/docs/architecture/network/egress.md
@@ -28,7 +28,7 @@ Egress is how a sandbox gets a network policy instead of open internet access. I
- <span id="precedence"></span>**Precedence.** First matching rule wins: platform `deny.always`, then platform `allow.always`, then your policy — the platform's deny always beats your allow. Overlays live inside the sidecar image and hot-reload every minute; a reload publishes only after nftables accepts the corresponding static policy. Parse or apply failures keep the last accepted in-memory rules and stay eligible for retry.
- **Runtime mutation.** Your policy is set per sandbox and can be mutated at runtime (add, replace, remove by target) through the API the SDKs expose.
-**Port floor.** Operators can also ship an `reachable.ports.cap` file next to the overlays: a whitelist of TCP ports (one port or `lo-hi` range per line) that are reachable **at all**. The sidecar derives the complement and enforces it as an always-deny ahead of every user rule — so regardless of what a sandbox's `defaultAction` or allow rules say, only the whitelisted ports pass (TCP only; the same fail-closed ordering that makes a deny rule win over any allow). A missing or empty file means no floor. Under transparent MITM, intercepted ports are governed by the interception config, so keep intercepted ports within the whitelist. Note established connections persist until their lease expires, as with any policy change.
+**Port floor.** Operators can also ship an `reachable.ports.cap` file next to the overlays: a whitelist of TCP ports (one port or `lo-hi` range per line) that are reachable **at all**. The sidecar derives the complement and enforces it as an always-deny ahead of every user rule — so regardless of what a sandbox's `defaultAction` or allow rules say, only the whitelisted ports pass (TCP only; the same fail-closed ordering that makes a deny rule win over any allow). A missing or empty file means no floor. Under transparent MITM, the floor still applies to mitmproxy's own upstream connections, so keep intercepted ports within the whitelist — a non-whitelisted intercepted port fails at the proxy instead of passing silently. The floor never blackholes interception itself: flows redirected into the mitmproxy listener are accepted ahead of the floor because the redirect rewrites their destination port before the policy chain runs (see [Port-scoped rules](#port-scoped-rules)). Note established connections persist until their lease expires, as with any policy change.
### Port-scoped rules
@@ -36,7 +36,7 @@ A rule's `target` may be omitted when `ports` is set, so a rule can scope by TCP
**DNS limitation for targetless allows.** The DNS proxy only resolves hostnames matched by a domain rule; a targetless `allow` with `ports` is invisible to it. Under `defaultAction: deny`, hostname lookups therefore still fail (`NXDOMAIN`) and such a rule serves clients that dial IPs directly. Hostname access needs explicit domain allow rules — which are not port-scoped — or destinations addressed by stable IP.
-Current limits: TCP only (no UDP); at most 256 ports per rule; domain targets cannot combine with `ports` (domain rules are evaluated at the DNS layer, which has no port dimension) and are rejected at validation; `DELETE /policy` removes rules by `target` — port-only rules have none, so `DELETE` with an explicit empty string (`""`) in the target list removes them all, or a full `POST`/`PUT` replaces the policy. The Fast Sandbox profile rejects `ports` with a 400 until its enforcement path supports them. Independently of user rules, operators can cap reachable ports platform-wide with the `reachable.ports.cap` floor (see [Precedence](#precedence)). One boundary to keep in mind: under transparent MITM, intercepted ports are redirected into the sidecar before the nft policy chain sees them, so port-scoped rules and the cap govern only non-intercepted traffic — keep the interception port list within the cap.
+Current limits: TCP only (no UDP); at most 256 ports per rule; domain targets cannot combine with `ports` (domain rules are evaluated at the DNS layer, which has no port dimension) and are rejected at validation; `DELETE /policy` removes rules by `target` — port-only rules have none, so `DELETE` with an explicit empty string (`""`) in the target list removes them all, or a full `POST`/`PUT` replaces the policy. The Fast Sandbox profile rejects `ports` with a 400 until its enforcement path supports them. Independently of user rules, operators can cap reachable ports platform-wide with the `reachable.ports.cap` floor (see [Precedence](#precedence)). One boundary to keep in mind: under transparent MITM, intercepted ports are redirected into the sidecar (destination rewritten to the mitmproxy listener port) before the nft policy chain sees them, so the chain accepts that rewritten destination ahead of port-scoped rules and interception works under default-deny policies and port floors — port-scoped rules and the cap then govern mitmproxy's own re-originated connections, keeping the interception port list within the cap.
## Design decisions
diff --git a/scripts/egress-mitm-e2e-target.sh b/scripts/egress-mitm-e2e-target.sh
new file mode 100644
index 000000000..bd1bd2f10
--- /dev/null
+++ b/scripts/egress-mitm-e2e-target.sh
@@ -0,0 +1,75 @@
+#!/bin/bash
+#
+# Copyright 2026 The OpenSandbox Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Plain-HTTP listener on a non-80/443 port used by the egress-MITM E2E tests
+# (tests/python/tests/test_egress_mitm_e2e.py) as a connectivity probe target:
+# it must LISTEN on its port so "blocked by policy/floor" (connect times out)
+# stays distinguishable from "nothing there" (instant refusal).
+
+: "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER:=opensandbox-e2e-egress-mitm-target}"
+: "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IMAGE:=python:3.11-alpine}"
+: "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_PORT:=8080}"
+: "${OPENSANDBOX_EGRESS_MITM_E2E_LABEL_KEY:=opensandbox.e2e}"
+: "${OPENSANDBOX_EGRESS_MITM_E2E_LABEL_VALUE:=egress-mitm}"
+
+setup_egress_mitm_e2e_target() {
+ local label="${OPENSANDBOX_EGRESS_MITM_E2E_LABEL_KEY}=${OPENSANDBOX_EGRESS_MITM_E2E_LABEL_VALUE}"
+
+ docker pull "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IMAGE}"
+ docker rm -f "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}" >/dev/null 2>&1 || true
+
+ docker run -d \
+ --name "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}" \
+ --label "${label}" \
+ "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IMAGE}" \
+ python -m http.server "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_PORT}" --bind 0.0.0.0 >/dev/null
+
+ local target_ready="false"
+ for _ in $(seq 1 30); do
+ if docker exec "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}" python - <<PY >/dev/null 2>&1
+import urllib.request
+urllib.request.urlopen("http://127.0.0.1:${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_PORT}/", timeout=1).read()
+PY
+ then
+ target_ready="true"
+ break
+ fi
+ sleep 1
+ done
+
+ if [ "${target_ready}" != "true" ]; then
+ docker logs "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}" || true
+ echo "Egress MITM E2E target container did not become ready" >&2
+ return 1
+ fi
+
+ local target_ip
+ target_ip="$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
+ "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}")"
+ if [ -z "${target_ip}" ]; then
+ docker logs "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}" || true
+ echo "Failed to determine Egress MITM E2E target container IP" >&2
+ return 1
+ fi
+
+ export OPENSANDBOX_EGRESS_MITM_E2E_LABEL_KEY
+ export OPENSANDBOX_EGRESS_MITM_E2E_LABEL_VALUE
+ export OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IP="${target_ip}"
+}
+
+cleanup_egress_mitm_e2e_target() {
+ docker rm -f "${OPENSANDBOX_EGRESS_MITM_E2E_TARGET_CONTAINER}" >/dev/null 2>&1 || true
+}
diff --git a/scripts/python-e2e.sh b/scripts/python-e2e.sh
index b13d1b9d6..f9bf02d02 100755
--- a/scripts/python-e2e.sh
+++ b/scripts/python-e2e.sh
@@ -26,6 +26,7 @@ REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SERVER_PID=""
source "${REPO_ROOT}/scripts/credential-vault-e2e-target.sh"
+source "${REPO_ROOT}/scripts/egress-mitm-e2e-target.sh"
cleanup_server() {
if [ -n "${SERVER_PID}" ]; then
@@ -37,6 +38,7 @@ cleanup_server() {
cleanup() {
cleanup_server
cleanup_credential_vault_e2e_target
+ cleanup_egress_mitm_e2e_target
}
trap cleanup EXIT
@@ -66,6 +68,7 @@ echo "-------- PYTHON E2E test logs for execd --------" > /tmp/opensandbox-e2e/l
export OPENSANDBOX_CREDENTIAL_VAULT_E2E_SANDBOX_IMAGE="${OPENSANDBOX_CREDENTIAL_VAULT_E2E_SANDBOX_IMAGE:-opensandbox/code-interpreter:${TAG}}"
setup_credential_vault_e2e_target
+setup_egress_mitm_e2e_target
# setup server
cd server
diff --git a/tests/python/tests/test_egress_mitm_e2e.py b/tests/python/tests/test_egress_mitm_e2e.py
new file mode 100644
index 000000000..8b41e4026
--- /dev/null
+++ b/tests/python/tests/test_egress_mitm_e2e.py
@@ -0,0 +1,275 @@
+#
+# Copyright 2026 The OpenSandbox Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+"""Egress x transparent-MITM interaction E2E (docker runtime, dns+nft).
+
+Regressions for the class of bugs where the transparent MITM REDIRECT (the
+destination is rewritten to the mitmproxy listener port before the nft egress
+chain runs) is judged as if the workload had dialed the listener itself, so
+every intercepted HTTPS flow is silently blackholed even when the policy
+allows the original destination port:
+
+- Scenario A: operator port floor (``reachable.ports.cap`` with only 80/443)
+ plus Credential Vault (transparent MITM) with a default-allow policy —
+ HTTPS must still work, and non-whitelisted ports must stay blocked
+ (issue #2423).
+- Scenario B: no floor, plus an allow-443/deny-all user policy with
+ Credential Vault enabled — HTTPS must still work through the default-deny
+ tail instead of dying on the rewritten port.
+
+The floor file is injected into the sandbox's egress sidecar via
+``docker exec`` (docker runtime only) and takes effect through the sidecar's
+periodic always-rules reload (~1 minute), which the test polls for. The tests
+are env-gated on ``OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IP``, set by
+``scripts/egress-mitm-e2e-target.sh`` (wired into ``scripts/python-e2e.sh``);
+without it they skip.
+"""
+
+from __future__ import annotations
+
+import os
+import subprocess
+import time
+from datetime import timedelta
+
+import pytest
+from opensandbox import SandboxSync
+from opensandbox.models.sandboxes import (
+ CredentialProxyConfig,
+ NetworkPolicy,
+ NetworkRule,
+ SandboxImageSpec,
+)
+
+from tests.base_e2e_test import (
+ create_connection_config_sync,
+ get_e2e_sandbox_resource,
+ get_sandbox_image,
+ is_kubernetes_runtime,
+)
+
+TARGET_IP = os.getenv("OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IP", "")
+E2E_LABEL_KEY = os.getenv(
+ "OPENSANDBOX_EGRESS_MITM_E2E_LABEL_KEY", "opensandbox.e2e"
+)
+E2E_LABEL_VALUE = os.getenv(
+ "OPENSANDBOX_EGRESS_MITM_E2E_LABEL_VALUE", "egress-mitm"
+)
+# The probe target listens here; the port is deliberately NOT 80/443 so it is
+# neither MITM-intercepted nor whitelisted by the port floor.
+PROBE_PORT = 8080
+# The sidecar reloads operator rule files at most once per minute; poll a bit
+# beyond that before concluding the floor never took effect.
+FLOOR_RELOAD_TIMEOUT_S = 150
+
+
[email protected](scope="module")
+def mitm_target_ip() -> str:
+ if not TARGET_IP:
+ pytest.skip("Set OPENSANDBOX_EGRESS_MITM_E2E_TARGET_IP to run this E2E")
+ if is_kubernetes_runtime():
+ pytest.skip("floor injection uses docker exec into the egress sidecar")
+ return TARGET_IP
+
+
+def test_port_floor_with_vault_keeps_https_working(
+ mitm_target_ip: str,
+) -> None:
+ """Issue #2423: reachable.ports.cap (80/443) + vault must not blackhole HTTPS.
+
+ The floor complement is derived from whitelisted ports, but the MITM
+ REDIRECT rewrites an intercepted SYN's destination port to the mitmproxy
+ listener (e.g. 18081) before the nft chain evaluates it — which lands in
+ the complement. Pre-fix, every intercepted HTTPS flow timed out even
+ though 443 is whitelisted and the policy is default-allow.
+ """
+ cfg, sandbox = _create_sandbox(
+ network_policy=NetworkPolicy(defaultAction="allow"),
+ )
+ try:
+ _write_reachable_ports_cap(sandbox.id, ["80", "443"])
+
+ # Phase 1 — precondition: the probe target is reachable before the
+ # floor lands (it listens, the policy is default-allow).
+ assert _wait_until_tcp(
+ sandbox, mitm_target_ip, PROBE_PORT, expect_connected=True, timeout_s=60
+ ), f"probe target {mitm_target_ip}:{PROBE_PORT} unreachable even before the floor"
+
+ # Phase 2 — the injected cap file takes effect via the sidecar's
+ # periodic always-rules reload (~1 minute): the non-whitelisted port
+ # must flip to blocked.
+ assert _wait_until_tcp(
+ sandbox, mitm_target_ip, PROBE_PORT, expect_connected=False
+ ), (
+ "reachable.ports.cap floor did not take effect: a non-whitelisted "
+ f"port ({PROBE_PORT}) stayed reachable under a default-allow policy; "
+ "the sidecar either never reloaded the injected cap file or the floor is not enforced"
+ )
+
+ _assert_https_via_mitm(sandbox, "https://example.com", "Example Domain")
+ finally:
+ _close_sandbox(cfg, sandbox)
+
+
+def test_allow_443_deny_all_with_vault_keeps_https_working(
+ mitm_target_ip: str,
+) -> None:
+ """allow-443/deny-all + transparent MITM must let HTTPS out.
+
+ Pre-fix, the MITM REDIRECT rewrote the SYN's dport 443 to the listener
+ port, which matches no allow rule, so the final default-deny drop
+ blackholed every intercepted flow even though the user explicitly allowed
+ 443. Enforcement must also stay intact: a port the policy does not allow
+ stays blocked.
+ """
+ cfg, sandbox = _create_sandbox(
+ network_policy=NetworkPolicy(
+ defaultAction="deny",
+ egress=[NetworkRule(action="allow", ports=[443])],
+ ),
+ )
+ try:
+ # Gate: the MITM path is live at all. Pre-fix this connect timed out
+ # because the rewritten dport hit the default-deny drop before
+ # mitmproxy ever saw the flow.
+ assert _wait_until_tcp(
+ sandbox, "1.1.1.1", 443, expect_connected=True, timeout_s=60
+ ), "TCP/443 was blackholed by the MITM redirect under allow-443/deny-all"
+
+ # The probe target listens on PROBE_PORT, so the block below proves
+ # the deny-all default really bites (not just "nothing there").
+ assert _wait_until_tcp(
+ sandbox, mitm_target_ip, PROBE_PORT, expect_connected=False, timeout_s=45
+ ), "deny-all policy did not block a non-allowed port"
+
+ _assert_https_via_mitm(
+ sandbox,
+ (
+ "https://cloudflare-dns.com/dns-query?name=example.com&type=A"
+ " --resolve cloudflare-dns.com:443:1.1.1.1"
+ " --header 'accept: application/dns-json'"
+ ),
+ '"Status":0',
+ )
+ finally:
+ _close_sandbox(cfg, sandbox)
+
+
+def _create_sandbox(network_policy: NetworkPolicy) -> tuple[object, SandboxSync]:
+ cfg = create_connection_config_sync()
+ sandbox = SandboxSync.create(
+ image=SandboxImageSpec(get_sandbox_image()),
+ resource=get_e2e_sandbox_resource(),
+ connection_config=cfg,
+ timeout=timedelta(minutes=5),
+ ready_timeout=timedelta(seconds=60),
+ network_policy=network_policy,
+ credential_proxy=CredentialProxyConfig(enabled=True),
+ metadata={E2E_LABEL_KEY: E2E_LABEL_VALUE},
+ )
+ return cfg, sandbox
+
+
+def _close_sandbox(cfg: object, sandbox: SandboxSync) -> None:
+ try:
+ sandbox.kill()
+ finally:
+ sandbox.close()
+ try:
+ cfg.transport.close()
+ except Exception:
+ # Best-effort teardown: do not fail the test if transport is already closed
+ # or cannot be closed during cleanup.
+ pass
+
+
+def _write_reachable_ports_cap(sandbox_id: str, ports: list[str]) -> None:
+ """Inject the operator port floor into this sandbox's egress sidecar.
+
+ The sidecar re-reads /var/egress rules periodically (~1 minute) and
+ re-applies the nft static policy, so the floor takes effect without a
+ restart.
+ """
+ body = "\\n".join(ports) + "\\n"
+ _sidecar_exec(
+ sandbox_id,
+ "mkdir -p /var/egress/rules && "
+ f"printf '{body}' > /var/egress/rules/reachable.ports.cap && "
+ "cat /var/egress/rules/reachable.ports.cap",
+ )
+
+
+def _sidecar_exec(sandbox_id: str, script: str) -> str:
+ container = f"sandbox-egress-{sandbox_id}"
+ result = subprocess.run(
+ ["docker", "exec", container, "sh", "-c", script],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+ assert result.returncode == 0, (
+ f"docker exec {container} failed: "
+ f"{result.stderr.strip() or result.stdout.strip()}"
+ )
+ return result.stdout
+
+
+def _tcp_connects(sandbox: SandboxSync, host: str, port: int) -> bool:
+ probe = sandbox.commands.run(
+ f"timeout 6 bash -c 'echo > /dev/tcp/{host}/{port}' 2>/dev/null"
+ )
+ return probe.error is None and probe.exit_code == 0
+
+
+def _wait_until_tcp(
+ sandbox: SandboxSync,
+ host: str,
+ port: int,
+ *,
+ expect_connected: bool,
+ timeout_s: int = FLOOR_RELOAD_TIMEOUT_S,
+) -> bool:
+ """Poll the TCP probe until it matches the expectation, or give up.
+
+ Covers sidecar startup ordering (nft apply, MITM readiness) and the
+ always-rules reload latency without fixed sleeps.
+ """
+ deadline = time.monotonic() + timeout_s
+ while time.monotonic() < deadline:
+ if _tcp_connects(sandbox, host, port) == expect_connected:
+ return True
+ time.sleep(5)
+ return False
+
+
+def _assert_https_via_mitm(sandbox: SandboxSync, url_and_args: str, expect: str) -> None:
+ """Assert a real HTTPS request succeeds through the MITM path, with retries.
+
+ TLS verification stays ON: the sandbox trusts the mitm CA installed by the
+ execd bootstrap, and mitmproxy verifies the upstream against public roots.
+ """
+ last = ""
+ for _ in range(3):
+ result = sandbox.commands.run(
+ f"curl --silent --show-error --connect-timeout 8 --max-time 25 {url_and_args}"
+ )
+ stdout = "".join(part.text for part in result.logs.stdout)
+ if result.error is None and result.exit_code == 0 and expect in stdout:
+ return
+ last = str(result.error) if result.error else f"exit={result.exit_code} stdout={stdout!r}"
+ time.sleep(5)
+ pytest.fail(f"HTTPS through the MITM did not succeed: {last}")