Skip to content

fix(sandbox): the metrics scrape and the whitelist NetworkPolicy resolve each host one time, through the egress check - #2521

Merged
dan-petty merged 10 commits into
mainfrom
fix/2431-sandbox-vet-addresses
Oct 28, 2026
Merged

dan-petty merged 10 commits into
mainfrom
fix/2431-sandbox-vet-addresses

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

The Prometheus scrape of the workload sandbox and the builders of the whitelist NetworkPolicy resolve each host name one time, through the resolve-and-check function of the egress policy (vet_addresses in devops_cli.http.egress). Before, the scrape checked its URL with a pre-flight validate_url_egress call, which resolved the name. Then the factory client resolved the name again at the connect. A rebinding DNS server could give a different address at each lookup. The public whitelist validator also resolved each name, and then the policy builder resolved the name again with a direct socket.getaddrinfo call and classified the answers itself. This change is part 4 of the split of #1381.

The callers and their levels:

  • Metrics scrape (scrape_prometheus_metrics): loopback level. The pre-flight address check and its loopback shortcut go. _validate_scrape_url keeps only the checks that read the URL: the scheme is http or https, and the URL names a host. An address refusal comes from the connect, and the scrape reports it as a failed scrape request. The error of the scrape does not start with "SSRF validation blocked scrape URL" any more.
  • Public whitelist builder (_resolve_public_dns): public level.
  • Local whitelist builder (_resolve_local_dns): private level. Then each address must be private or loopback, and not link-local, as before. Thus the local whitelist continues to refuse a link-local answer such as 169.254.1.2, which the private level admits.
  • Public whitelist validator (_validate_public_whitelist_item): no lookup (is_loopback_or_private_host(host, resolve_dns=False)). The policy builder makes the one lookup, and refuses a name that answers an address that is not public.

ai.allow_private_network changes none of these levels.

Each builder gives vet_addresses the port of its whitelist entry. Thus the builders also refuse a cloud metadata name before the lookup, and a scoped IPv6 answer such as 2606:4700::1%2. The public builder admitted that answer before. The builders continue to sort the address blocks, so the same answers give the same manifest.

The docstring of each builder names its time-of-check gap. The NetworkPolicy holds the addresses of one lookup. A later lookup in the pod can give a different address. The policy does not allow that address, so the pod cannot connect to it.

A refusal of a whitelist builder is a SandboxValidationError. It names the host, the refused answer when there is one, and the reason, and the refusal of vet_addresses is its cause. The message of SSRFBlockedError masks the host, so it does not tell the person which entry the builder refused. A failed lookup (httpcore2.ConnectError) changes into a SandboxValidationError in the same way. The refusals of an IP address or CIDR in the four builder functions also raise SandboxValidationError, not a bare ValueError. SandboxValidationError is a ValueError, so devops sandbox network-policy exits 1 and prints no policy, as before.

test_each_caller_dials_at_its_level has a row for the metrics scrape, with and without ai.allow_private_network. test_a_scrape_looks_up_its_host_once and test_the_whitelist_policy_looks_up_each_name_once count the lookups. test_the_whitelist_policy_refuses_an_answer_outside_its_level gives each builder a cloud metadata name, a cloud metadata answer, a scoped IPv6 answer and an answer outside its level, and the local builder also a link-local answer. test_a_whitelist_name_that_does_not_resolve_is_refused and test_network_policy_exits_1_naming_a_whitelist_name_whose_answer_it_refuses check the error and the exit status. The swallowed-failures ceiling of sandbox/metrics.py goes down to 1. The new capability sandbox-egress holds the requirements. README.md names the sandbox metrics scrape among the clients of the egress-checked factory.

Part 5 of the split (#2437) deletes _resolve_host_ips and _enforce_non_private_ssrf, changes the check of the container engine endpoint, and writes the general statement that every HTTP client is checked at the connect.

Closes #2431

Change: openspec/changes/archive/2026-10-28-2431-sandbox-vet-addresses/

…st builders

The change folder 2431-sandbox-vet-addresses plans part 4 of #1381.
…onnect

The scrape lost its pre-flight validate_url_egress check, which resolved the host a second time before the factory client resolved it at the connect. The client checks the address at the loopback level. The scrape still refuses a scheme that is not http or https, and a URL with no host.
…, with vet_addresses

The public and local whitelist builders resolved a name with socket.getaddrinfo and classified the answers themselves, and the public whitelist validator resolved each name again. Now the validator makes no lookup, and each builder resolves a name one time with vet_addresses at the port of its entry: the public level, or the private level and then the private-or-loopback rule of the local whitelist. A builder refuses a cloud metadata name before the lookup, and a scoped IPv6 answer. A refusal is a SandboxValidationError that names the host, with the refusal of vet_addresses as its cause.
…trics module to 1

The scrape catches only the ValueError of its URL check now, so the module has one swallowed failure. The ratchet refuses a count below its ceiling.
…nd the exit code

The command test names the refused answer, which only the lookup of the policy builder gives. Thus the test fails when a whitelist validator makes the lookup again. The test of a failed lookup checks the exit code, as docs/TESTING.md tells an error-path test to do.
…es, and name the refused answer

The purpose and the requirement of the one whitelist lookup had more than 25 words. The scenarios use the active voice. A refusal of an answer names that answer.
@dan-petty dan-petty added scope/k8s Kubernetes commands, Helm charts, pods, services, and policies scope/security Security linters (trivy, bandit, gitleaks, kubeconform) and Vault type/security Type security: a vulnerability or a leak labels Oct 28, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Test evidence for the branch at 46db21f, rebased on origin/main e6230dc.

Targeted tests

  • uv run devops ci test tests/test_http_egress.py tests/test_sandbox_metrics.py tests/test_sandbox_networking.py tests/test_sandbox_policy_properties.py tests/test_validation.py: 451 passed.
  • uv run devops ci test tests/test_swallowed_failures_ratchet.py tests/test_sandbox_lifecycle.py tests/test_workload_sandbox.py tests/test_security_sandbox_and_protection.py tests/commands/test_command_error_exit.py tests/test_sandbox_probe.py: 245 passed.
  • rg -n 'validate_url_egress|validate_service_url|validate_configured_service_url|validate_url\(|_resolve_host_ips|resolve_dns=True|socket\.getaddrinfo' src/devops_cli/sandbox prints nothing.
  • The pre-push hook (fast static checks) passed.

Fail-to-pass

uv run devops ci fail-to-pass --base origin/main exited 0. The failures that show the defect:

  • test_a_scrape_looks_up_its_host_once: 2 lookups of the scrape host on the base, not 1.
  • test_the_whitelist_policy_looks_up_each_name_once[public]: 2 lookups of each name on the base.
  • test_each_caller_dials_at_its_level[metrics scrape-*]: the public level on the base, not the loopback level.
  • The 10 rows of test_the_whitelist_policy_refuses_an_answer_outside_its_level, the 2 rows of test_a_whitelist_name_that_does_not_resolve_is_refused and test_network_policy_exits_1_naming_a_whitelist_name_whose_answer_it_refuses: a bare ValueError that does not name the refused answer, or no refusal of a scoped IPv6 answer.

The other failures come from the builder signature, which takes a port on the branch. test_the_properties_run_under_the_ci_profile_so_a_loaded_host_cannot_fail_them fails on the base because of the hypothesis profile of the replay environment, not because of this change. It passes on the branch.

The failed lines of the replay:

failed on base: tests.test_http_egress::test_each_caller_dials_at_its_level[metrics scrape-with-flag] (AssertionError: assert (<EgressLevel...'public'>, []) == (<EgressLevel...oopback'>, []))
failed on base: tests.test_http_egress::test_each_caller_dials_at_its_level[metrics scrape-without-flag] (AssertionError: assert (<EgressLevel...'public'>, []) == (<EgressLevel...oopback'>, []))
failed on base: tests.test_sandbox_metrics::test_a_scrape_looks_up_its_host_once (AssertionError: assert (None, ['up']...5.14', 9100)]) == (None, ['up']...5.14', 9100)]))
failed on base: tests.test_sandbox_networking::test_a_loopback_host_or_network_resolves_to_no_address (TypeError: _resolve_local_host() takes 1 positional argument but 2 were given)
failed on base: tests.test_sandbox_networking::test_a_whitelist_name_that_does_not_resolve_is_refused[local] (ValueError: Local whitelist hostname 'node1.example.internal' DNS resolution failed: [Errno -2] lookups fail in this test: node1.example.internal)
failed on base: tests.test_sandbox_networking::test_a_whitelist_name_that_does_not_resolve_is_refused[public] (ValueError: Public whitelist domain 'example.com' DNS resolution failed: [Errno -2] lookups fail in this test: example.com)
failed on base: tests.test_sandbox_networking::test_an_entry_that_resolves_to_no_address_adds_no_rule (TypeError: test_an_entry_that_resolves_to_no_address_adds_no_rule.<locals>.<lambda>() missing 1 required positional argument: '_port')
failed on base: tests.test_sandbox_networking::test_network_policy_exits_1_naming_a_whitelist_name_whose_answer_it_refuses (AssertionError: assert (1, True, Fal...mple.com': 1}) == (1, True, Tru...mple.com': 1}))
failed on base: tests.test_sandbox_networking::test_resolve_local_host_ipv6_emits_full_prefix (TypeError: _resolve_local_host() takes 1 positional argument but 2 were given)
failed on base: tests.test_sandbox_networking::test_resolve_public_host_ipv6_emits_full_prefix (TypeError: _resolve_public_host() takes 1 positional argument but 2 were given)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_looks_up_each_name_once[public] (AssertionError: assert {'example.com...ample.org': 2} == {'example.com...ample.org': 1})
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[local, a cloud metadata answer] (ValueError: Local whitelist hostname 'node1.example.internal' resolves to forbidden link-local '169.254.169.254'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[local, a cloud metadata name] (ValueError: Link-local cloud metadata 'metadata.google.internal' is forbidden in local whitelist.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[local, a link-local answer] (ValueError: Local whitelist hostname 'node1.example.internal' resolves to forbidden link-local '169.254.1.2'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[local, a public answer] (ValueError: Local whitelist hostname 'node1.example.internal' resolves to non-private address '93.184.215.14'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[local, a scoped ipv6 answer] (ValueError: Local whitelist hostname 'node1.example.internal' resolves to forbidden link-local 'fe80::1%2'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[public, a cloud metadata answer] (ValueError: Public whitelist domain 'example.com' resolves to cloud metadata address '169.254.169.254'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[public, a cloud metadata name] (ValueError: Public whitelist domain 'metadata.google.internal' resolves to cloud metadata address '169.254.169.254'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[public, a loopback answer] (ValueError: Public whitelist domain 'example.com' resolves to non-public address '127.0.0.1'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[public, a private answer] (ValueError: Public whitelist domain 'example.com' resolves to non-public address '192.0.2.10'.)
failed on base: tests.test_sandbox_networking::test_the_whitelist_policy_refuses_an_answer_outside_its_level[public, a scoped ipv6 answer] (Failed: DID NOT RAISE SandboxValidationError)
failed on base: tests.test_sandbox_policy_properties::test_property_cloud_metadata_superset_containment_at_all_six_sites (TypeError: _resolve_local_host() takes 1 positional argument but 2 were given)
failed on base: tests.test_sandbox_policy_properties::test_property_resolve_hosts_emit_correct_prefix_lengths (TypeError: _resolve_local_host() takes 1 positional argument but 2 were given)
failed on base: tests.test_sandbox_policy_properties::test_the_properties_run_under_the_ci_profile_so_a_loaded_host_cannot_fail_them (AssertionError: assert ('default', t...s=200), False) == ('ci', None, True))
failed on base: tests.test_validation::test_widened_metadata_denials_models_and_the_connect (TypeError: _resolve_local_host() got an unexpected keyword argument 'port')
25 failed on base (25 failed and 426 passed in tests/test_http_egress.py, tests/test_sandbox_metrics.py, tests/test_sandbox_networking.py, tests/test_sandbox_policy_properties.py, tests/test_validation.py; 0 errored).

@dan-petty
dan-petty marked this pull request as ready for review October 28, 2026 19:51
@dan-petty
dan-petty merged commit 7c01426 into main Oct 28, 2026
17 of 19 checks passed
@dan-petty
dan-petty deleted the fix/2431-sandbox-vet-addresses branch October 28, 2026 19:53
dan-petty pushed a commit that referenced this pull request Oct 28, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.11.0](v0.10.1...v0.11.0)
(2026-10-28)


### Features

* **monitoring:** the roadmap Service's identity refusals, failing and
stalled jobs, restarts, and GitHub and model request volume raise alerts
([#2496](#2496))
([0137f23](0137f23)),
closes [#2472](#2472)
* **service:** roadmap-service runs at one replica, and each model call
in the cluster is sent at most two times
([#2500](#2500))
([2d32dae](2d32dae)),
closes [#2494](#2494)


### Bug Fixes

* **cli:** every command reports a DevOpsCLIError through one exit seam
([#2502](#2502))
([eadd55f](eadd55f)),
closes [#2487](#2487)
* **github:** pr monitor waits until the checks settle and names every
blocker, and pr ready reads the merge-readiness verdict
([#2498](#2498))
([43326ea](43326ea))
* **k8s:** a pod that does not call the Kubernetes API mounts no service
account token
([#2522](#2522))
([305d3ba](305d3ba))
* **k8s:** the cloudflared tunnel replicas prefer different nodes and
keep one up through a drain
([#2499](#2499))
([d4edaf6](d4edaf6)),
closes [#1023](#1023)
* **k8s:** the grafana, prometheus, argo cd and loki clients dial
through the egress-checked factory, and their pre-flight address checks
go ([#2497](#2497))
([e7ca1d5](e7ca1d5))
* **monitoring:** the roadmap Service alerts count a series that starts
in the hour, and a lane of a new pod can stall
([#2514](#2514))
([c7692b2](c7692b2)),
closes [#2505](#2505)
* **review:** a review's scanners leave out the rules that never gave a
valid finding here
([#2508](#2508))
([fe953ac](fe953ac))
* **roadmap:** a refine round leaves an outside account issue to a
person, and refine neutralizes comment openers, www links and
cross-repository references
([#2507](#2507))
([e6230dc](e6230dc)),
closes [#2478](#2478)
* **sandbox:** the metrics scrape and the whitelist NetworkPolicy
resolve each host one time, through the egress check
([#2521](#2521))
([7c01426](7c01426))
* **security:** the subprocess environment guard refuses the zsh and
interpreter keys that make a child run code
([#2510](#2510))
([459621a](459621a))
* **service:** a restart keeps the halt, the budget window and the
failure ledger, and a rate limit or a suspended installation stops every
worker ([#2515](#2515))
([3630de1](3630de1)),
closes [#2474](#2474)


### Code Refactoring

* **config:** settings resolve in one declared order through
pydantic-settings sources
([#2518](#2518))
([6a5447a](6a5447a)),
closes [#2445](#2445)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/k8s Kubernetes commands, Helm charts, pods, services, and policies scope/security Security linters (trivy, bandit, gitleaks, kubeconform) and Vault type/security Type security: a vulnerability or a leak

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(sandbox): the metrics scrape loses its pre-flight address check, and the sandbox address-range builders resolve with vet_addresses

1 participant