Skip to content

fix(ai): the ai service clients dial through the egress-checked factory, and their pre-flight address checks go #2422

Closed

Description

@dan-petty

Problem Description

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

The AI service clients below build a raw httpx2.Client or call httpx2.get. Most of them check the host name before the dial with a pre-flight check (validate_url_egress, validate_service_url, validate_configured_service_url or validate_url). Some check nothing. httpx2 then resolves the name again at the socket. A rebinding DNS server can give a different address the second time. #898 closed this window for the callers that it moved to the egress-checked factory (devops_cli.http.client). This part moves the AI service callers.

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

Caller Raw client Pre-flight check that goes Level
Gateway model info ai/gateway.py:289 ai/gateway.py:286 configured
Gateway health probe ai/gateway.py:482 ai/gateway.py:473 configured (wider: loopback without the flag; metadata refused with it)
Backend probe ai/gateway.py:668 ai/gateway.py:607 configured (wider, as above)
devops ai gateway connect probe commands/ai_gateway.py:167 commands/ai_gateway.py:220 private (same as today: the address is a node address from the Kubernetes API, and the command sets ai.allow_private_network itself)
devops ai gateway load Prometheus query ai/pool_load.py:47 ai/pool_load.py:43, commands/ai_gateway.py:610 configured (see the note below)
Embeddings ai/rag/embeddings.py:439, :681, :775, :916 ai/rag/embeddings.py:678, :760, :902, :1138 configured
devops ai cost --source URL ai/spend/pricing.py:672 none today configured (narrower: a private source needs the flag)
Default pricing registry ai/spend/pricing.py:684 none today public (narrower)
Documentation crawler ai/library/docs_ingester.py:257 ai/library/docs_ingester.py:242, :266 public; the flag does not widen it (narrower)
pydantic-ai Ollama model no client: create_ollama_model (called from ai/pydantic_ai_bridge.py:128) gives the provider no http_client, so pydantic-ai builds its own ai/models/ollama.py:34 (resolve_dns=True), :73 configured (narrower: a private Ollama needs the flag on this path too)
Benchmark endpoints the AI client and the embeddings engine commands/benchmark.py:75, ai/benchmark/runner.py:225, ai/benchmark/embedding_runner.py:303 configured, through the AI client (wider: loopback without the flag)
pydantic-ai provider base URL none: ai/providers/__init__.py:117-118 already uses the factory at configured ai/providers/__init__.py:93 configured (same)

"Configured" is loopback, or private when ai.allow_private_network is set (configured_level).

Note on the pool-load query: the #898 table gives the pool-load query the private level and the Prometheus commands, commands/ai_gateway.py among them, the configured level. devops ai gateway load is now the only caller of prometheus_query. Thus the query dials at the level that its caller gives, and the command gives configured.

Out of scope:

Reproduction Steps

  1. On origin/main (86d5bce), run rg -n -g '*.py' 'httpx2\.((Async)?Client|get|post)\(' src/devops_cli/ai/gateway.py src/devops_cli/commands/ai_gateway.py src/devops_cli/ai/pool_load.py src/devops_cli/ai/rag/embeddings.py src/devops_cli/ai/spend/pricing.py src/devops_cli/ai/library/docs_ingester.py.
  2. In a test, install scripted_resolver(monkeypatch, {"example.com": rebinding(1, "10.0.0.1")}) and record_connects from tests/web_fakes.py.
  3. Without ai.allow_private_network, probe the gateway at https://example.com/v1 with GatewayRouter.probe_gateway.
  4. Read recorder.dialled.

Expected vs Actual Behavior

Expected: each caller resolves its host once, at the connect. It dials only the addresses that its level admits. The factory checks each redirect hop the same way.

Actual: step 1 lists the raw clients in the table. In step 4, the pre-flight check passes on the first (public) answer, and httpx2 resolves the name again and dials 10.0.0.1.

Environment Diagnostics

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

Logs, Traces, or Terminal Output

src/devops_cli/ai/gateway.py:289:        with httpx2.Client(timeout=2.0) as ephemeral_client:
src/devops_cli/ai/gateway.py:482:            with httpx2.Client(timeout=timeout) as client:
src/devops_cli/ai/gateway.py:668:        with httpx2.Client(timeout=timeout) as client:
src/devops_cli/ai/library/docs_ingester.py:257:        with httpx2.Client(timeout=30.0) as client:
src/devops_cli/ai/pool_load.py:47:            with httpx2.Client(timeout=timeout) as client:
src/devops_cli/ai/rag/embeddings.py:439:                self._http_client = httpx2.Client(timeout=client_timeout)
src/devops_cli/ai/rag/embeddings.py:681:            with httpx2.Client(timeout=timeout) as client:
src/devops_cli/ai/rag/embeddings.py:775:            with httpx2.Client(timeout=client_timeout) as client:
src/devops_cli/ai/rag/embeddings.py:916:            with httpx2.Client(timeout=client_timeout) as client:
src/devops_cli/ai/spend/pricing.py:672:            r = httpx2.get(source_url, timeout=timeout)
src/devops_cli/ai/spend/pricing.py:684:            r = httpx2.get(url, timeout=timeout)
src/devops_cli/commands/ai_gateway.py:167:        with httpx2.Client(timeout=timeout) as client:

Acceptance criteria

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

  • rg -n -g '*.py' 'httpx2\.((Async)?Client|get|post|put|patch|delete|head|request|stream)\(' src/devops_cli/ai/gateway.py src/devops_cli/commands/ai_gateway.py src/devops_cli/ai/pool_load.py src/devops_cli/ai/rag/embeddings.py src/devops_cli/ai/spend/pricing.py src/devops_cli/ai/library/docs_ingester.py prints nothing.
  • The pydantic-ai Ollama model gets an http_client from new_async_http_client at the configured level.
  • No pre-flight address check remains in the files of the table: rg -n 'validate_url_egress|validate_service_url|validate_configured_service_url|validate_url\(|resolve_dns=True' <those files> prints nothing. A scheme and syntax check without a lookup can stay where a command needs an early error.
  • test_each_caller_dials_at_its_level in tests/test_http_egress.py has a row for each caller in the table, with and without ai.allow_private_network. Each narrower or wider row has a case that fails on the code before the move.
  • Tests stay offline (ScriptedResolver and ConnectRecorder in tests/web_fakes.py), and each call phase stays under 1 s.
  • If the move 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

Proposed design

Problem statement

The AI service clients in six files build a raw httpx2.Client or call httpx2.get, then check the host name before the dial. A rebinding DNS server can give a different address on the second resolution at the socket. This part moves those callers to the egress-checked factory so the address is vetted at the connect, and removes the now-redundant pre-flight checks.

Acceptance criteria

  • Criterion 1: No raw httpx2 client or request call remains in the six in-scope files.
    Verification: Run rg -n -g '*.py' 'httpx2\.((Async)?Client|get|post|put|patch|delete|head|request|stream)\(' src/devops_cli/ai/gateway.py src/devops_cli/commands/ai_gateway.py src/devops_cli/ai/pool_load.py src/devops_cli/ai/rag/embeddings.py src/devops_cli/ai/spend/pricing.py src/devops_cli/ai/library/docs_ingester.py and confirm it prints nothing.
  • Criterion 2: The pydantic-ai Ollama model receives an http_client from new_async_http_client at the configured level.
    Verification: Inspect ai/models/ollama.py and ai/pydantic_ai_bridge.py:128 to confirm create_ollama_model passes an http_client built by new_async_http_client(level=configured_level(...)).
  • Criterion 3: No pre-flight address check remains in the in-scope files.
    Verification: Run rg -n 'validate_url_egress|validate_service_url|validate_configured_service_url|validate_url\(|resolve_dns=True' src/devops_cli/ai/gateway.py src/devops_cli/commands/ai_gateway.py src/devops_cli/ai/pool_load.py src/devops_cli/ai/rag/embeddings.py src/devops_cli/ai/spend/pricing.py src/devops_cli/ai/library/docs_ingester.py and confirm it prints nothing. A scheme and syntax check without a DNS lookup may remain where a command needs an early error.
  • Criterion 4: test_each_caller_dials_at_its_level has a row for each caller in the table, with and without ai.allow_private_network.
    Verification: Run pytest tests/test_http_egress.py::test_each_caller_dials_at_its_level -v and confirm a row exists for each of the twelve callers, each tested with the flag set and unset. Each narrower or wider row has a case that fails on the code before the move.
  • Criterion 5: Tests stay offline and each call phase stays under 1 s.
    Verification: Run pytest tests/test_http_egress.py -v --timeout=5 and confirm all tests pass using ScriptedResolver and ConnectRecorder from tests/web_fakes.py, with no network access and no test exceeding 1 s.
  • Criterion 6: If the move makes a document wrong, the same pull request fixes it.
    Verification: Search the repository for statements that claim every HTTP client is checked at the connect. Confirm the general statement waits for Part 5 and is not added here. Fix any document that is now inaccurate.
  • Criterion 7: The required CI checks pass.
    Verification: Confirm Static Analysis, Tests & Coverage, and Semantic PR title checks all report a passing status on the pull request.

Key questions

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/security
    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/security. The candidate closes a TOCTOU DNS rebinding vulnerability in AI service callers by routing them through the egress-checked factory.
    • Priority: P1-High. A security vulnerability with a concrete reproduction path affects multiple AI service callers.
    • Value: High. It removes a DNS rebinding attack window for gateway probes, embeddings, pricing, and other AI service calls.
    • Effort: Medium. The pattern is set by #898, but the candidate lists over ten call sites across six files to migrate.
    • Related: refactor(cli): Unify HTTP and DNS seams with shared fakes #1314, which asks for a different change. Both items work on the HTTP client layer, but #1314 unifies the three client factories into one seam while the candidate moves specific AI callers to the existing egress-checked factory.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at 73d1807 (see proposed design in issue body).

  4. added
    priority/p1-high
    status/ready
    effort/medium
    value/high
    on Oct 28, 2026
  5. removed
    status/ready
    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/p1-highsource/agenttype/securityvalue/high

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions