Skip to content

fix(ai): replies carry their server as an httpx2 origin, so a password in an AI URL stays out of output, the spend ledger, metrics, spans and cache #1110

Description

@dan-petty

Problem

Lines cited at dcafadf.

  • A display string is the data model. Each provider turns its base URL into "<type> (<host>)" text with urlparse(...).netloc, and the client later parses that text back to find the server.
    • Built at ai/client/unified.py:276-277 (one base URL) and :285 (the Ollama URL list), joined at :288-291; ai/client/ollama.py:354-356; ai/client/openai.py:138; ai/client/claude.py:103.
    • Read back by _extract_server_from_backend_info (unified.py:61-67, split("(", 1)[1].split(")", 1)[0]) at :137, :451 and :878, then split a second time by _server_span_attributes (:70-90, urlsplit(f"//{netloc}")) at :138 and :821.
  • Credentials go along. urlparse(...).netloc keeps userinfo. An offline probe at dcafadf with ai.ollama_urls = ["<masked-url> found user:pw` in:
    • the reply's backend_info, ollama (user:pw@example.com:11434). devops ai (commands/ai.py:678, :684), devops analyze (commands/analyze.py:313, :402, :497) and review metadata and progress lines (ai/review/runner.py:1191-1194, :1299-1300; ai/review/pipeline.py:1871-1878) all print it;
    • the spend ledger's server and backend_info columns (unified.py:451, :468 into ai/spend/ledger.py:659-674). devops ai cost report --by server (s.server, commands/ai_cost.py:127) and devops ai cost prometheus (the server label, ai/spend/prometheus.py:79) show the server column. No command reads the stored backend_info back (the ledger only names it in its INSERT, ledger.py:70-76), but the database file holds it;
    • the server label of the request, token and spend counters (ai/spend/ledger.py:692-702);
    • the backend field of the llm_response_received span event (unified.py:117-128, :152);
    • the on-disk response-cache entry (unified.py:533-549).
  • Other providers leak the same way. With gateway_url = "http://<masked-user>:<masked-password>@example.com:4000/v1", the display reads gateway (user:pw@example.com:4000) (unified.py:276-277). Span server.address is the only value that comes out clean, because urlsplit().hostname drops userinfo.
  • Userinfo is valid configuration, so refusing it is not the fix. get_ollama_urls (config/settings.py:502-514) accepts it. An offline probe showed that httpx2 sends it as Authorization: Basic …, which is how a client reaches an Ollama behind an authenticating reverse proxy.
  • Two served_by displays read the stored text. served_by is the gateway's x-litellm-model-api-base reply header, stored as received.
  • The port is guessed away. The text drops the scheme, so _server_span_attributes writes no server.port for a URL without an explicit port (docstring at unified.py:71-77). For example, claude (api.anthropic.com) gets server.address only.
  • Replacement: httpx2.URL(base).origin. httpx2 is a direct, exact-pinned dependency (httpx2==2.13.0, pyproject.toml:19). httpx2.Origin is public (listed in httpx2.__all__), hashable and comparable. An offline probe gave:
    • http://<masked-user>:<masked-password>@example.com:11434 → Origin(scheme='http', host='example.com', port=11434);
    • https://api.anthropic.com/v1 → port 443;
    • http://[2001:db8::1]:11434 → host 2001:db8::1;
    • http://<masked-user>:<masked-password>@example.com:11434 → host example.com, because httpx2 splits at the last '@', as it does when it sends.
    • httpx2.URL(u).origin raises ValueError for a URL with no scheme and httpx2.InvalidURL (not a ValueError subclass) for a malformed one such as http://[::1.
  • Display text uses httpx2's own authority serialiser: httpx2.URL(scheme=scheme, host=host, port=port).netloc.decode("ascii").
    • It gives example.com:11434, [2001:db8::1]:11434 and api.anthropic.com, and brackets IPv6 itself.
    • httpx2.Origin takes a URL, not a host and port, so a bare served_by host:port has no Origin. The serialiser takes the parts instead. With a scheme it drops that scheme's default port; without one it keeps any port (httpx2.URL(host="example.com", port=80).netloc is example.com:80).
    • For every URL without userinfo this matches today's urlparse().netloc text, with three exceptions, all measured with httpx2 2.13.0:
      • the host is lowercased: http://Example.COM:11434 gives example.com:11434, where today shows Example.COM:11434;
      • an IDN host prints as its A-label: http://bücher.example:11434 gives xn--bcher-kva.example:11434;
      • an explicit default port is dropped: http://example.com:80 and https://example.com:443 give example.com. http://example.com:443 keeps its port.

Acceptance criteria

  • The field. LLMResponse (ai/client/models.py:88-138) gains origin: httpx2.Origin | None.
    • The Ollama, OpenAI-compatible and Claude paths set it with httpx2.URL(url).origin from the URL they called: base in ollama.py (:354), self._api_base() in openai.py (:169), base in claude.py (:33-36).
    • Replies with no endpoint leave it None: cache hits, pydantic-ai conversions (models.py:193) and test fakes.
  • One label helper, next to LLMResponse, takes a host, a port and an optional scheme and returns httpx2.URL(scheme=scheme, host=host, port=port).netloc.decode("ascii").
    • For an origin it is called with o.host, o.port and o.scheme.
    • For a bare served_by authority it is called with no scheme, so a bare :80 or :443 stays in the label.
    • Every "<type> (<host>)" string uses it: unified.py:291, ollama.py:356, openai.py:138, claude.py:103.
  • Display. LLMClient derives its configured origins once, with httpx2.URL(u).origin.
    • backend_host and backend_info (unified.py:260-291) are display text, formatted from those origins through the label helper.
    • A configured URL that has no origin, or that httpx2 cannot parse (ValueError or httpx2.InvalidURL), displays unknown (CONST_AI_BACKEND_HOST_UNKNOWN, config/constants.py:534-536), never the raw value.
  • No parse-back. Nothing reads host data back out of display text.
    • These values come from an origin: the ledger's server value (unified.py:451, :878), the counters' server label, and span server.address / server.port (unified.py:137-139, :821).
    • _extract_server_from_backend_info and _server_span_attributes are deleted.
  • A dispatched reply with no origin (a test fake, or a later adapter that sets none) falls back to the client's configured origins.
    • The ledger server and the counter label are the client's configured label, as self.backend_host is today (unified.py:451).
    • Span server.* comes from the configured origin when exactly one is configured, as the stream span already does (unified.py:821). With several or none configured, the span gets no server.*.
  • Port. server.port is set whenever server.address is: 443 for https://api.anthropic.com, 11434 for the Ollama default.
  • Table test. Offline and parametrised.
    • Replies come from route_client (tests/llm_stream_fakes.py:85-102) or an httpx2.MockTransport.
    • The config sets allow_private_network=True, or the test sets DEVOPS_CLI_AI_ALLOW_PRIVATE_NETWORK=true as tests/test_ai_gateway_base_url.py:16-18 does. Without it, the provider paths' validate_base_url (ai/client/network.py:348-385) resolves every non-loopback host through _resolve_host_ips (core/validation.py:251), so example.com would hit DNS and 2001:db8::1 would be refused as non-public.
    • No DNS query is made.
Configured URL backend_info today backend_info after Ledger server and counter label after Span server.address, server.port after
ollama http://<masked-user>:<masked-password>@example.com:11434 ollama (user:pw@example.com:11434) ollama (example.com:11434) example.com:11434 example.com, 11434
gateway http://<masked-user>:<masked-password>@example.com:4000/v1 gateway (user:pw@example.com:4000) gateway (example.com:4000) example.com:4000 example.com, 4000
ollama http://[2001:db8::1]:11434 ollama ([2001:db8::1]:11434) unchanged [2001:db8::1]:11434 2001:db8::1, 11434
ollama http://Example.COM:11434 ollama (Example.COM:11434) ollama (example.com:11434) example.com:11434 example.com, 11434
ollama http://example.com:80 ollama (example.com:80) ollama (example.com) example.com example.com, 80
claude default https://api.anthropic.com claude (api.anthropic.com) unchanged api.anthropic.com api.anthropic.com, 443 (today: no port)
ollama http://example.com:11434 and http://example.com:11435, streamed ollama (example.com:11434, example.com:11435) unchanged unchanged neither, as today
  • Credentials. In the two userinfo rows:
    • the mocked server still receives Authorization: Basic dXNlcjpwdw==;
    • pw appears in none of these: the reply's backend_info, the ledger record, the counter labels, the span attributes and events, or the response-cache entry.
  • served_by displays read the value through one parse, next to the label helper.
  • No construct left.
    • rg -n 'urlparse|_extract_server_from_backend_info|_server_span_attributes' src/devops_cli/ai/client/unified.py src/devops_cli/ai/client/ollama.py src/devops_cli/ai/client/openai.py src/devops_cli/ai/client/claude.py src/devops_cli/commands/ai_cost.py src/devops_cli/commands/review.py prints nothing.
    • rg -n '\.netloc' src/devops_cli/ai/client src/devops_cli/commands/ai_cost.py src/devops_cli/commands/review.py prints only the label helper's call on an httpx2.URL.
  • Tests updated.
    • tests/test_ai_client_telemetry.py:204-225 (test_server_attributes_come_from_one_backend_netloc) is rewritten over origins, with the host-list case still yielding neither attribute.
    • tests/test_telemetry.py:270-310 and :588-608 call _set_response_span_attributes without a client, so their replies carry an origin. In :588-608 the port-less https://example.com reply now gets server.port 443 from its scheme.
  • Tests that pass unchanged, because the values they assert do not change:
    • tests/test_ai_client_telemetry.py:87-120, whose _reply fake (:57-72) sets no origin, so it pins the fallback above; :143-176 (stream spans); :185-201; :242-268;
    • tests/test_ai_gateway.py:452-473 (backend_host stays example.com), tests/test_ai_gateway_base_url.py:28-32 (gateway.example.com:4000) and tests/test_ai_client_providers_and_embeddings.py:258-260 ("11434" in client.backend_host).
  • Release.
    • New tests are offline and take well under 1 s together.
    • uv run devops ci passes.
      • the credential fix;
      • the server.port change;
      • the three label changes (lowercased host, IDN A-label, explicit :80/:443 dropped), which start new ledger server groups and counter series for such configs;
      • that earlier ledger rows and response-cache entries keep the text they were written with, and that devops ai cost reset (commands/ai_cost.py:691-713) and devops ai cache clear (commands/ai_cache.py:75-93) remove them.
  • Pending a person: if ai.ollama_urls, ai.gateway_url or ai.api_base_url ever held user:password, rotate that password. Ledger rows, response-cache entries and exported counter series written before this change hold it (see the key question on rows already written).

Key questions

Strategic alignment

Proposed design

Problem statement

The AI client stores server URLs as display strings built with urlparse().netloc. When a configured URL carries userinfo, those credentials appear in terminal output, the spend ledger, metric labels, span events, and the on-disk response cache. Two string parsers read host data back out of that text. The fix replaces the string data model with httpx2.URL(base).origin, a structured value that never carries userinfo, and deletes the parse-back helpers.

Acceptance criteria

  • Criterion 1: LLMResponse gains a field origin of type httpx2.Origin | None.
    Verification: Inspect ai/client/models.py for the new field. Run the test suite for the models module.
  • Criterion 2: The Ollama, OpenAI-compatible and Claude paths set origin with httpx2.URL(url).origin from the URL they called.
    Verification: Inspect ai/client/ollama.py, ai/client/openai.py and ai/client/claude.py for the origin assignment. Run tests/test_ai_gateway.py and tests/test_ai_client_providers_and_embeddings.py.
  • Criterion 3: Replies with no endpoint leave origin as None.
    Verification: Run tests/test_ai_client_telemetry.py:87-120, whose _reply fake sets no origin.
  • Criterion 4: One label helper takes a host, a port and an optional scheme and returns httpx2.URL(scheme=scheme, host=host, port=port).netloc.decode('ascii').
    Verification: Inspect ai/client/models.py or ai/client/unified.py for the helper. Run the table test.
  • Criterion 5: LLMClient derives its configured origins once with httpx2.URL(u).origin.
    Verification: Inspect ai/client/unified.py for the origin derivation in init or a property. Run tests/test_ai_gateway_base_url.py.
  • Criterion 6: A configured URL that httpx2 cannot parse displays the constant CONST_AI_BACKEND_HOST_UNKNOWN, never the raw value.
    Verification: Add a test with a malformed URL and assert the display is 'unknown'. Run tests/test_ai_gateway.py.
  • Criterion 7: The functions _extract_server_from_backend_info and _server_span_attributes are deleted.
    Verification: Run: rg -n '_extract_server_from_backend_info|_server_span_attributes' src/devops_cli/ai/client/unified.py. Expect no output.
  • Criterion 8: The ledger server value, the counter server label, and span server.address and server.port come from an origin, not from parsed display text.
    Verification: Run tests/test_ai_client_telemetry.py and tests/test_telemetry.py. Inspect the span attributes in the test output.
  • Criterion 9: A dispatched reply with no origin falls back to the client's configured origins for the ledger and counter, and to the configured origin for span attributes when exactly one is set.
    Verification: Run tests/test_ai_client_telemetry.py:87-120. Assert the fallback values match the configured label.
  • Criterion 10: server.port is set whenever server.address is, including the scheme default (443 for https, 80 for http).
    Verification: Run the table test row for claude default https://api.anthropic.com and assert server.port is 443.
  • Criterion 11: The table test is offline, parametrised, and makes no DNS query.
    Verification: Run the new table test. Confirm no network calls by running with --timeout or in an air-gapped environment. The test uses route_client or httpx2.MockTransport.
  • Criterion 12: In the userinfo rows, the mocked server receives Authorization: Basic dXNlcjpwdw==, and the password appears in none of: backend_info, the ledger record, counter labels, span attributes, or the response-cache entry.
    Verification: Run the table test userinfo rows. Assert the password string is absent from all listed outputs.
  • Criterion 13: The served_by displays in the cost report and the review benchmark read the value through one parse and show no userinfo.
    Verification: Run tests/test_ai_served_by.py:173-190 and tests/test_review_profile.py:373-379. Add and pass the new userinfo rows.
  • Criterion 14: No construct left: rg for urlparse, _extract_server_from_backend_info, _server_span_attributes in the named files prints nothing. rg for .netloc prints only the label helper's call.
    Verification: Run: rg -n 'urlparse|_extract_server_from_backend_info|_server_span_attributes' src/devops_cli/ai/client/unified.py src/devops_cli/ai/client/ollama.py src/devops_cli/ai/client/openai.py src/devops_cli/ai/client/claude.py src/devops_cli/commands/ai_cost.py src/devops_cli/commands/review.py. Expect no output. Run: rg -n '.netloc' src/devops_cli/ai/client src/devops_cli/commands/ai_cost.py src/devops_cli/commands/review.py. Expect only the label helper.
  • Criterion 15: tests/test_ai_client_telemetry.py:204-225 is rewritten over origins. tests/test_telemetry.py:270-310 and :588-608 carry an origin and the port-less reply gets server.port 443.
    Verification: Run: uv run pytest tests/test_ai_client_telemetry.py tests/test_telemetry.py -v. All pass.
  • Criterion 16: Tests that pass unchanged: tests/test_ai_client_telemetry.py:87-120, :143-176, :185-201, :242-268; tests/test_ai_gateway.py:452-473; tests/test_ai_gateway_base_url.py:28-32; tests/test_ai_client_providers_and_embeddings.py:258-260.
    Verification: Run: uv run pytest tests/test_ai_client_telemetry.py tests/test_ai_gateway.py tests/test_ai_gateway_base_url.py tests/test_ai_client_providers_and_embeddings.py -v. All pass without modification.
  • Criterion 17: The full CI pipeline passes.
    Verification: Run: uv run devops ci. All checks pass.

Key questions

Open questions

  • It is public in httpx2 2.13.0, which is already a direct dependency. It carries scheme, host and port, and never userinfo.
  • Not a str: text invites the next parse-back.
  • Not pydantic's URL types: for http://example.com\@169.254.169.254/, pydantic reads host example.com while httpx2 sends to 169.254.169.254 (offline probe). The field must come from the parser of the client that sends.
  • That equals today's text except in the three cases above: the host is lowercased, an IDN host prints as its A-label, and an explicit default port is dropped. Configs with such URLs start new ledger server groups and new counter series; every other config keeps its values.
  • Pricing overrides are stored and looked up lowercased (ai/spend/pricing.py:402, :272, :324), so the lowercased host does not change which devops ai cost set-price override applies. An override keyed on an explicit :80/:443 or on a Unicode host name stops matching.
  • Pricing's local-server check (ai/spend/pricing.py:104-127) lowercases and reads only the host, so the case and default-port changes do not move it. An IDN host's A-label no longer equals the Unicode name _is_configured_ai_server_host (pricing.py:63-87) reads from settings, so an IDN-named configured server could be priced as remote. Proposed: accept this edge, and let the changelog name it.
  • Printing the full origin (http://example.com:11434) would show the scheme, but it would split ledger history and relabel series for every config. The field already holds the scheme for any reader that needs it.
  • Userinfo is working Basic-auth configuration (see Problem).
  • Whether URL settings reject or validate userinfo belongs with refactor(config): a settings field is the one declaration, and the option, env and config-show registries become views of it #1000's validated URL settings.
  • The stream span names no server; tests/test_ai_client_telemetry.py:143-176 pin this.
  • The ledger server is the configured labels joined with ", ", which pricing already reads as a list (ai/spend/pricing.py:120-127).
  • The stream span opens (unified.py:821) before a URL is leased (ai/client/ollama.py:419-422), so naming the leased server is a separate change.
  • Rewriting stored backend_info would need the parse-back this item deletes, and the project is pre-1.0.
  • The pull request body tells anyone who configured userinfo to rotate the password, run devops ai cost reset and run devops ai cache clear.
  • The served_by displays already read through the new parse, so old served_by values show no userinfo.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/security
    scope/ai
    scope/security
    scope/telemetry
    priority/p2-medium
    on Oct 3, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Refine cannot set this item to Ready at 69636b6 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

    • It is public in httpx2 2.13.0, which is already a direct dependency. It carries scheme, host and port, and never userinfo.
    • Not a str: text invites the next parse-back.
    • Not pydantic's URL types: for http://example.com\@169.254.169.254/, pydantic reads host example.com while httpx2 sends to 169.254.169.254 (offline probe). The field must come from the parser of the client that sends.
    • That equals today's text except in the three cases above: the host is lowercased, an IDN host prints as its A-label, and an explicit default port is dropped. Configs with such URLs start new ledger server groups and new counter series; every other config keeps its values.
    • Pricing overrides are stored and looked up lowercased (ai/spend/pricing.py:402, :272, :324), so the lowercased host does not change which devops ai cost set-price override applies. An override keyed on an explicit :80/:443 or on a Unicode host name stops matching.
    • Pricing's local-server check (ai/spend/pricing.py:104-127) lowercases and reads only the host, so the case and default-port changes do not move it. An IDN host's A-label no longer equals the Unicode name _is_configured_ai_server_host (pricing.py:63-87) reads from settings, so an IDN-named configured server could be priced as remote. Proposed: accept this edge, and let the changelog name it.
    • Printing the full origin (http://example.com:11434) would show the scheme, but it would split ledger history and relabel series for every config. The field already holds the scheme for any reader that needs it.
    • Userinfo is working Basic-auth configuration (see Problem).
    • Whether URL settings reject or validate userinfo belongs with refactor(config): a settings field is the one declaration, and the option, env and config-show registries become views of it #1000's validated URL settings.
    • The stream span names no server; tests/test_ai_client_telemetry.py:143-176 pin this.
    • The ledger server is the configured labels joined with ", ", which pricing already reads as a list (ai/spend/pricing.py:120-127).
    • The stream span opens (unified.py:821) before a URL is leased (ai/client/ollama.py:419-422), so naming the leased server is a separate change.
    • Rewriting stored backend_info would need the parse-back this item deletes, and the project is pre-1.0.
    • The pull request body tells anyone who configured userinfo to rotate the password, run devops ai cost reset and run devops ai cache clear.
    • The served_by displays already read through the new parse, so old served_by values show no userinfo.
  3. added
    status/new
    value/medium
    effort/medium
    on Oct 28, 2026
  4. added 3 commits that reference this issue on Oct 28, 2026
    3ec083f
    d5ffd97
    57725c3
  5. 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-mediumscope/aiscope/securityscope/telemetrytype/securityvalue/medium

    Type

    No type

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions