Skip to content

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

Closed

Description

@dan-petty

Problem Description

Part 4 of #1381. The owner approved the split of #1381 into five parts on 2026-10-28.

The sandbox package resolves some host names twice, and it resolves others outside the one resolve-and-check function of devops_cli.http.egress (vet_addresses).

  • The metrics scrape already dials through the factory at the loopback level. Before that, _validate_scrape_url calls validate_url_egress, which resolves the name a second time. That pre-flight check goes.
  • The address-range builders of SandboxNetworkConfig resolve whitelist names to the CIDRs of a Kubernetes NetworkPolicy. They build firewall rules, not connections, so fix(security): every HTTP client dials only addresses its egress level allows, checked at the connect and on every redirect #898 keeps their lookup. They call socket.getaddrinfo directly and classify the answers themselves. They change to vet_addresses at their level.
  • The public whitelist validator resolves each name through is_loopback_or_private_host(host, resolve_dns=True), which calls _resolve_host_ips. The policy builder then resolves the same name again. Part 5 deletes _resolve_host_ips, so the validator stops its lookup, and the builder makes the one lookup.

Scope, with lines on origin/main d739a47 and the level from the #898 level table:

Caller Check today After Level
Metrics scrape, scrape_prometheus_metrics pre-flight validate_url_egress in _validate_scrape_url (sandbox/metrics.py:351); the client at :374 already uses the factory the pre-flight check goes; the scheme and host check at :341-346 stays loopback (same)
Public whitelist builder, _resolve_public_dns socket.getaddrinfo at sandbox/models.py:237, classified at :244-254 vet_addresses, with a time-of-check docstring public (same)
Local whitelist builder, _resolve_local_dns socket.getaddrinfo at sandbox/models.py:274, classified at :281-291 vet_addresses, with a time-of-check docstring; the builder keeps its rule that each address is private or loopback private, then private or loopback only (same)
Public whitelist validator, _validate_public_whitelist_item is_loopback_or_private_host(host, resolve_dns=True) at sandbox/models.py:159 no lookup (resolve_dns=False); the builder refuses a non-public answer no lookup

"The flag" (ai.allow_private_network) changes none of these levels.

Notes:

  • vet_addresses takes a port. _build_whitelist_egress (sandbox/models.py:326) has the port of each entry from _parse_whitelist_entry.
  • vet_addresses refuses a cloud metadata name before the lookup, and it refuses a scoped IPv6 answer. The builders get both refusals.
  • The 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 then blocks that address, so the gap fails closed. Each docstring says this.

Out of scope:

  • sandbox/probe.py, which already uses the factory at the private level.
  • The tests that patch _resolve_host_ips to check the container engine endpoint (tests/test_sandbox_lifecycle.py:537, :593). Part 5 changes that check.

Reproduction Steps

  1. On origin/main (d739a47), run rg -n 'validate_url_egress|resolve_dns=True|socket\.getaddrinfo' src/devops_cli/sandbox.
  2. In a test, install resolver = scripted_resolver(monkeypatch, {"example.com": [[PUBLIC_ADDRESS]]}) and record_connects(monkeypatch, []) from tests/web_fakes.py.
  3. Call scrape_prometheus_metrics("http://example.com:9100/metrics").
  4. Read resolver.lookups["example.com"].
  5. Reset the resolver. Build SandboxNetworkConfig(mode="public_whitelist", public_whitelist=["example.com"]).to_k8s_network_policy().
  6. Read resolver.lookups["example.com"] again.

Expected vs Actual Behavior

Expected: the metrics scrape resolves its host once, at the connect. The NetworkPolicy builder resolves each whitelist name once, through vet_addresses.

Actual: step 1 lists the checks in the table. Step 4 gives 2: one lookup for the pre-flight check and one at the connect. Step 6 gives 2 for the public whitelist: one lookup in the validator and one in the builder.

Environment Diagnostics

  • DevOps CLI Version: 0.8.3 (origin/main d739a47)
  • Python Version: 3.14.10
  • OS & Architecture: Linux x86_64
  • Execution Environment: DevContainer

Logs, Traces, or Terminal Output

src/devops_cli/sandbox/metrics.py:16:from devops_cli.core.validation import is_loopback_host, validate_url_egress
src/devops_cli/sandbox/metrics.py:351:    return validate_url_egress(
src/devops_cli/sandbox/models.py:159:    if is_loopback_or_private_host(host, resolve_dns=True):
src/devops_cli/sandbox/models.py:237:        addr_info = socket.getaddrinfo(host, None)
src/devops_cli/sandbox/models.py:274:        addr_info = socket.getaddrinfo(host, None)

Acceptance criteria

These are the criteria of #1381 that apply to this part.

  • 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 address-range builders in sandbox/models.py call vet_addresses at the level of the table. Each one names its time-of-check gap in a docstring.
  • A test shows that the metrics scrape looks up its host once for each scrape, and that the NetworkPolicy builder looks up each whitelist name once.
  • A test shows that each builder refuses a cloud metadata name, a cloud metadata answer and an answer outside its level. The local builder also refuses a public answer.
  • test_each_caller_dials_at_its_level in tests/test_http_egress.py has a row for the metrics scrape (loopback), with and without ai.allow_private_network.
  • Tests stay offline (ScriptedResolver and ConnectRecorder in tests/web_fakes.py), and each call phase stays under 1 s.
  • If the change makes a document wrong, the same pull request fixes it. The general statement that every HTTP client is checked at the connect waits for Part 5.
  • The required CI checks pass.

Related

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Source: #1381

Activity

  1. added
    source/agent
    type/refactor
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/refactor. The change reorganises existing resolution calls to use a shared vet_addresses function and drops a redundant pre-flight lookup without altering the security outcome.
    • Priority: P2-Medium. It is one of five approved sub-tasks of #1381 and consolidates code paths without fixing a live vulnerability.
    • Value: Medium. It removes a double-lookup and a direct socket.getaddrinfo call, reducing TOCTOU surface and keeping one validation path.
    • Effort: Medium. It touches three call sites in two files with specific line numbers and a clear target API, but requires updating tests that patch _resolve_host_ips.
    • Related: refactor(security): the egress checks read legacy numeric hosts with getaddrinfo and AI_NUMERICHOST, and the hand-written ipv4 parser goes #2152, which asks for a different change. Both touch getaddrinfo in the egress-validation path, but #2152 removes a hand-written IPv4 parser in core/validation.py while this candidate re-routes sandbox/models.py and sandbox/metrics.py through vet_addresses.
  3. added
    priority/p2-medium
    status/new
    value/medium
    effort/medium
    on Oct 28, 2026
  4. removed
    status/new
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumsource/agenttype/refactorvalue/medium

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions