From 938bd260dcd9bef31fcc0c85070302d107166fa0 Mon Sep 17 00:00:00 2001 From: AI 13rac1 Date: Sat, 14 Nov 2026 03:29:25 +0000 Subject: [PATCH 1/2] Refuse endpoints outside a provider's E2EE coverage with one clear answer Each provider's E2EE covers some endpoints only. Teep refused audio transcription with E2EE enabled with a message that named no reason ("audio transcription requires TLS-level E2EE (pinned provider)"), and had no route for NEAR's image edits, privacy and responses endpoints, which NEAR's E2EE does not cover either. With E2EE enabled, an endpoint outside the provider's E2EE coverage is now refused, for every provider alike, and the user sees why. Changes: - RequestEncryptor declares its coverage (CoversE2EE). The declarations state the existing behavior: NEAR covers chat, completions (NEAR AI direct only), embeddings, image generation, rerank and score; Tinfoil EHBP covers chat, embeddings, responses and speech; Chutes covers chat and embeddings; Venice and E2EE v2 cover chat. - With E2EE enabled, a request on an uncovered endpoint is refused with HTTP 403 before anything is sent. The error has type e2ee_not_covered, names the provider and the endpoint, and states what the user sets to send it without E2EE: e2ee = false, and e2ee_usable in allow_fail where the policy enforces it. It states attested TLS only for a provider that uses TLS binding. One warning is logged, with no request content. This replaces the audio refusal. With E2EE enabled, a provider without an encryptor covers no endpoint. - New routes for /v1/privacy/classify, /v1/privacy/redact, /v1/images/edits and /v1/responses on neardirect and nearcloud, and audio on nearcloud. They are plain routes with e2ee = false, and refused with E2EE enabled. - The dashboard counts the refusals for each provider and lists the endpoints that its E2EE covers and does not. - The API support document states the rule, the new routes and a section on why teep refused a request. To verify: go test ./internal/proxy -run 'TestUncoveredEndpoint|TestNotCovered|TestE2EECoverage'. Co-Authored-By: Claude Opus 5.5 --- docs/api_support.md | 53 ++++- internal/e2ee/session.go | 4 + internal/provider/aci/e2ee.go | 10 + internal/provider/chutes/e2ee.go | 10 + internal/provider/neardirect/e2ee.go | 10 + internal/provider/provider.go | 12 ++ internal/provider/tinfoil/e2ee.go | 10 + internal/provider/venice/e2ee.go | 10 + internal/proxy/aciv2_cache_test.go | 3 + internal/proxy/dashboard.go | 19 +- internal/proxy/integration_tinfoil_test.go | 8 +- internal/proxy/proxy.go | 168 ++++++++++++++-- internal/proxy/proxy_test.go | 3 + internal/proxy/relay_internal_test.go | 3 + internal/proxy/signing_key_miss_test.go | 3 + internal/proxy/templates/dashboard.html | 2 + internal/proxy/uncovered_test.go | 216 +++++++++++++++++++++ 17 files changed, 509 insertions(+), 35 deletions(-) create mode 100644 internal/proxy/uncovered_test.go diff --git a/docs/api_support.md b/docs/api_support.md index f7a40e37..cf94b7a9 100644 --- a/docs/api_support.md +++ b/docs/api_support.md @@ -30,6 +30,9 @@ These are the OpenAI-style endpoints that clients use for inference: | `/v1/images/generations` | POST | Image generation | | `/v1/rerank` | POST | Document reranking | | `/v1/score` | POST | Text similarity scoring | +| `/v1/images/edits` | POST | Image edits (multipart) | +| `/v1/privacy/classify` | POST | PII span classification | +| `/v1/privacy/redact` | POST | PII redaction | | `/v1/models` | GET | List available models | `/v1/models` is a proxy-aggregated endpoint that returns the combined model list from all configured providers. Each model's `id` field is rewritten to `provider:upstreamID` (e.g. `venice:e2ee-qwen3-5-122b-a10b`, `neardirect:Qwen/Qwen3-VL-30B-A3B-Instruct`) so clients can route requests to the correct provider. NanoGPT lists only its TEE models, whose IDs start with `TEE/`; its other models serve no attestation. It is not included in the per-provider matrices below because it is handled entirely by the proxy, does not forward requests to individual providers, and is not E2EE-encrypted (GET request, no sensitive data). @@ -92,25 +95,31 @@ This matrix applies to OpenAI-compatible inference endpoints. | Endpoint | NearDirect | NearCloud | Chutes | Venice | Phala Cloud | NanoGPT | Tinfoil Cloud | Tinfoil Direct | |---|---|---|---|---|---|---|---|---| | Chat completions | Yes | Yes | Yes | Yes | Yes | Yes | Yes | Yes | -| Responses | — | — | — | — | — | — | Yes | Yes | +| Responses | Yes | Yes | — | — | — | — | Yes | Yes | | Embeddings | Yes | Yes | Yes | — | Yes | — | Yes | Yes | -| Audio transcriptions | Yes | — | — | — | — | — | Yes | Yes | +| Audio transcriptions | Yes | Yes | — | — | — | — | Yes | Yes | | Text-to-speech | — | — | — | — | — | — | Yes | Yes | | Image generation | Yes | Yes | — | — | — | — | — | — | | Reranking | Yes | Yes | — | — | — | — | — | — | | Score | Yes | Yes | — | — | — | — | — | — | | Completions | Yes | — | — | — | — | — | — | — | +| Image edits | Yes | Yes | — | — | — | — | — | — | +| Privacy classify and redact | Yes | Yes | — | — | — | — | — | — | **—** = Not implemented. Provider returns HTTP 400. +With E2EE enabled, an endpoint outside the provider's E2EE coverage is refused. See the E2EE support matrix and [why teep refused my request](#why-teep-refused-my-request). + ## E2EE Support Matrix | Endpoint | NearDirect | NearCloud | Chutes | Venice | Phala Cloud | NanoGPT | Tinfoil Cloud | Tinfoil Direct | |---|---|---|---|---|---|---|---|---| | Chat completions | Encrypted | Encrypted | Encrypted | Encrypted | Encrypted (ACI/1 models) | Encrypted (ACI/1 models) | Encrypted | Encrypted | -| Responses | — | — | — | — | — | — | Encrypted | Encrypted | +| Responses | Refused when E2EE is enabled | Refused when E2EE is enabled | — | — | — | — | Encrypted | Encrypted | | Embeddings | Encrypted | Encrypted | Encrypted | — | Fail closed when E2EE is enabled; plaintext when disabled | — | Encrypted | Encrypted | -| Audio transcriptions | Plaintext (pinned) | — | — | — | — | — | Fail closed when E2EE is enabled; plaintext when disabled | Fail closed when E2EE is enabled; plaintext when disabled | +| Audio transcriptions | Refused when E2EE is enabled | Refused when E2EE is enabled | — | — | — | — | Refused when E2EE is enabled | Refused when E2EE is enabled | +| Image edits | Refused when E2EE is enabled | Refused when E2EE is enabled | — | — | — | — | — | — | +| Privacy classify and redact | Refused when E2EE is enabled | Refused when E2EE is enabled | — | — | — | — | — | — | | Text-to-speech | — | — | — | — | — | — | Encrypted | Encrypted | | Image generation | Encrypted | Encrypted | — | — | — | — | — | — | | Reranking | Encrypted | Encrypted | — | — | — | — | — | — | @@ -118,12 +127,34 @@ This matrix applies to OpenAI-compatible inference endpoints. | Completions | Encrypted | — | — | — | — | — | — | — | **Encrypted** = E2EE is applied to request and response fields. +**Refused when E2EE is enabled** = the provider's E2EE does not cover the endpoint, so with E2EE enabled teep refuses it with HTTP 403 and sends nothing. With `e2ee = false` for the provider, teep sends it over the attested, pinned TLS connection. **Fail closed** = Proxy rejects the request with an error because E2EE is not supported for this endpoint in the end-to-end path, either because the proxy does not implement E2EE field dispatch for that request type or because the upstream TEE cannot decrypt it. **Plaintext (pinned)** = Request and response transit in plaintext, but the connection is TLS-pinned to the attested TEE (no E2EE field encryption). **Request encrypted; response plaintext (pinned)** = Request fields are encrypted end-to-end, but upstream currently returns plaintext response fields for this endpoint. This is a known upstream NearAI limitation (inference-proxy/cloud-api score response path), not a teep policy preference. Responses still transit on pinned TLS to attested TEEs. **Encrypted (ACI/1 models)** = E2EE v2 of the private-ai-gateway encrypts the request and response content fields when the model answers with the gateway's ACI/1 attestation. A model that answers with another attestation format fails closed when E2EE is enabled. **—** = Endpoint not available for this provider. +## Why teep refused my request + +With E2EE enabled, an endpoint outside the provider's E2EE coverage is refused. +Teep sends nothing and answers HTTP 403 with an error of type +`e2ee_not_covered` that names the provider and the endpoint: + +```json +{"error": {"type": "e2ee_not_covered", "provider": "", "endpoint": "/v1/audio/transcriptions", + "message": "teep did not send this request. E2EE is enabled for provider , and its end-to-end encryption does not cover /v1/audio/transcriptions. ..."}} +``` + +Nothing is wrong with teep or with the provider: you asked for end-to-end +encryption, and this endpoint cannot be encrypted end to end. To send it +without E2EE, set `e2ee = false` for the provider in your teep config. If the +provider's policy enforces `e2ee_usable`, also add `e2ee_usable` to its +`allow_fail` list. The request then uses TLS, which attestation binds only for +a provider that uses TLS binding (NearDirect, NearCloud, Phala Cloud and +Tinfoil); for Venice, NanoGPT and Chutes it is WebPKI TLS only. Each refusal logs one warning with the provider and the +endpoint, and the dashboard counts the refusals for each provider and lists the +endpoints that its E2EE covers and does not. + ## Provider Details **Teep E2EE Header:** Teep automatically sets `X-Encrypt-All-Fields: true` on all E2EE-enabled requests to NearDirect and NearCloud. This enables full-field encryption of all sensitive request and response fields. The encryption coverage documented below reflects teep's behavior with this header active. @@ -257,7 +288,10 @@ another endpoint; see [models the probe does not cover](providers/near/near_atte |---|---|---|---| | Chat completions | `/v1/chat/completions` | Yes | Sensitive fields encrypted; streaming forced when E2EE active | | Embeddings | `/v1/embeddings` | Yes | Sensitive fields encrypted; supports string and array input formats | -| Audio transcriptions | `/v1/audio/transcriptions` | Requires E2EE disabled | Multipart requests use attested TLS to the model TEE. The proxy rejects this endpoint when field-level E2EE is enabled | +| Audio transcriptions | `/v1/audio/transcriptions` | Refused when E2EE is enabled | Multipart requests use attested TLS to the model TEE when E2EE is disabled | +| Image edits | `/v1/images/edits` | Refused when E2EE is enabled | Multipart; attested TLS to the model TEE when E2EE is disabled | +| Privacy classify and redact | `/v1/privacy/classify`, `/v1/privacy/redact` | Refused when E2EE is enabled | NEAR does not decrypt an encrypted `input` on these endpoints (2026-11-14). Attested TLS to the model TEE when E2EE is disabled | +| Responses | `/v1/responses` | Refused when E2EE is enabled | NEAR model hosts answer 404 (2026-11-14); with E2EE disabled teep relays NEAR's answer | | Image generation | `/v1/images/generations` | Yes | Sensitive fields encrypted; `prompt`, `b64_json`, and `revised_prompt` encrypted | | Reranking | `/v1/rerank` | Yes | Sensitive fields encrypted; `query` and `documents[]` encrypted | | Score | `/v1/score` | Request only | Request fields (`text_1`, `text_2`) encrypted; response `data[].score` currently plaintext (known upstream NearAI limitation) | @@ -277,7 +311,10 @@ another endpoint; see [models the probe does not cover](providers/near/near_atte |---|---|---|---| | Chat completions | `/v1/chat/completions` | Yes | Gateway forwards E2EE headers to model TEE | | Embeddings | `/v1/embeddings` | Yes | Gateway forwards E2EE headers to model TEE | -| Audio transcriptions | Not configured | Not supported | The proxy has no NearCloud audio route | +| Audio transcriptions | `/v1/audio/transcriptions` | Refused when E2EE is enabled | With E2EE disabled, attested TLS to the gateway TEE, which reads the request | +| Image edits | `/v1/images/edits` | Refused when E2EE is enabled | With E2EE disabled, attested TLS to the gateway TEE | +| Privacy classify and redact | `/v1/privacy/classify`, `/v1/privacy/redact` | Refused when E2EE is enabled | With E2EE disabled, attested TLS to the gateway TEE | +| Responses | `/v1/responses` | Refused when E2EE is enabled | The gateway implements this stateful API itself and stores responses; with E2EE it fails for NEAR's own models (2026-11-14). With E2EE disabled, attested TLS to the gateway TEE | | Image generation | `/v1/images/generations` | Yes | Gateway forwards E2EE headers to model TEE | | Reranking | `/v1/rerank` | Yes | Gateway forwards E2EE headers to model TEE | | Score | `/v1/score` | Request only | Request fields (`text_1`, `text_2`) encrypted; response `data[].score` currently plaintext (known upstream NearAI limitation) | @@ -399,7 +436,7 @@ Venice only exposes chat completions. No other endpoints are available. | Chat completions | `/v1/chat/completions` | Yes | Full-body EHBP encryption; streaming and non-streaming supported | | Responses | `/v1/responses` | Yes | Full-body EHBP encryption; streaming and non-streaming supported | | Embeddings | `/v1/embeddings` | Yes | Full-body EHBP encryption | -| Audio transcriptions | `/v1/audio/transcriptions` | No when E2EE is enabled | Multipart route is implemented, but the proxy rejects Tinfoil multipart requests when E2EE is enabled. With E2EE disabled, it forwards over attested, SPKI-pinned TLS | +| Audio transcriptions | `/v1/audio/transcriptions` | Refused when E2EE is enabled | Tinfoil's E2EE (EHBP) in teep does not cover this multipart endpoint, so teep refuses it with HTTP 403 when E2EE is enabled. With E2EE disabled, it forwards over attested, SPKI-pinned TLS | | Text-to-speech | `/v1/audio/speech` | Yes | Full-body EHBP encryption | **E2EE field coverage:** EHBP encrypts the entire HTTP request and response body. There are **no field-level encryption gaps** — all request fields (messages, tools, parameters) and all response fields (content, tool_calls, usage) are encrypted by construction. Adding new OpenAI API fields requires zero changes to the encryption layer. @@ -425,7 +462,7 @@ Venice only exposes chat completions. No other endpoints are available. | Chat completions | `/v1/chat/completions` | Yes | Full-body EHBP encryption; streaming and non-streaming supported | | Responses | `/v1/responses` | Yes | Full-body EHBP encryption; streaming and non-streaming supported | | Embeddings | `/v1/embeddings` | Yes | Full-body EHBP encryption | -| Audio transcriptions | `/v1/audio/transcriptions` | No when E2EE is enabled | Multipart route is implemented, but the proxy rejects Tinfoil multipart requests when E2EE is enabled. With E2EE disabled, it forwards over attested, SPKI-pinned TLS | +| Audio transcriptions | `/v1/audio/transcriptions` | Refused when E2EE is enabled | Tinfoil's E2EE (EHBP) in teep does not cover this multipart endpoint, so teep refuses it with HTTP 403 when E2EE is enabled. With E2EE disabled, it forwards over attested, SPKI-pinned TLS | | Text-to-speech | `/v1/audio/speech` | Yes | Full-body EHBP encryption | **E2EE field coverage:** Identical to Tinfoil Cloud — full-body EHBP, no field-level gaps. diff --git a/internal/e2ee/session.go b/internal/e2ee/session.go index c75ed42c..693d79a7 100644 --- a/internal/e2ee/session.go +++ b/internal/e2ee/session.go @@ -34,6 +34,10 @@ const ( EndpointSpeech EndpointType = "speech" // EndpointCompletions is /v1/completions. EndpointCompletions EndpointType = "completions" + // EndpointPrivacy is /v1/privacy/classify and /v1/privacy/redact. + EndpointPrivacy EndpointType = "privacy" + // EndpointImagesEdits is /v1/images/edits (multipart). + EndpointImagesEdits EndpointType = "images_edits" ) // EncField* constants identify the dot-notation sub-field paths within inference diff --git a/internal/provider/aci/e2ee.go b/internal/provider/aci/e2ee.go index 1e171fa4..cf452dfa 100644 --- a/internal/provider/aci/e2ee.go +++ b/internal/provider/aci/e2ee.go @@ -226,3 +226,13 @@ func parsesAsJSONArray(s string) bool { t := strings.TrimLeft(s, " \t\r\n") return strings.HasPrefix(t, "[") && json.Valid([]byte(t)) } + +// CoversE2EE reports whether this E2EE covers endpoint. E2EE v2 is defined for chat completions only. +func (a *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { + switch endpoint { + case e2ee.EndpointChat: + return true + default: + return false + } +} diff --git a/internal/provider/chutes/e2ee.go b/internal/provider/chutes/e2ee.go index da4631f0..70a76459 100644 --- a/internal/provider/chutes/e2ee.go +++ b/internal/provider/chutes/e2ee.go @@ -45,3 +45,13 @@ func (c *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, _ e2 } return e2ee.EncryptResult{Body: encPayload, Chutes: meta}, nil } + +// CoversE2EE reports whether this E2EE covers endpoint. Chutes E2EE encrypts the whole body of chat and embeddings requests. +func (c *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { + switch endpoint { + case e2ee.EndpointChat, e2ee.EndpointEmbeddings: + return true + default: + return false + } +} diff --git a/internal/provider/neardirect/e2ee.go b/internal/provider/neardirect/e2ee.go index 951edcbf..33015ef9 100644 --- a/internal/provider/neardirect/e2ee.go +++ b/internal/provider/neardirect/e2ee.go @@ -60,3 +60,13 @@ func (n *E2EE) EncryptRequestWithModelKey(body []byte, modelKey e2ee.NearModelKe } return e2ee.EncryptResult{Body: encBody, Session: session}, nil } + +// CoversE2EE reports whether this E2EE covers endpoint. NEAR E2EE covers chat, completions, embeddings, image generation, rerank, and score (request fields only). +func (n *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { + switch endpoint { + case e2ee.EndpointChat, e2ee.EndpointCompletions, e2ee.EndpointEmbeddings, e2ee.EndpointImages, e2ee.EndpointRerank, e2ee.EndpointScore: + return true + default: + return false + } +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 17ea4c81..27b852a5 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -67,6 +67,9 @@ type RequestPreparer interface { // - Tinfoil/EHBP: EHBP (full-body state, decrypted before relay) type RequestEncryptor interface { EncryptRequest(body []byte, raw *attestation.RawAttestation, endpoint e2ee.EndpointType) (e2ee.EncryptResult, error) + // CoversE2EE reports whether the provider's E2EE covers endpoint. With + // E2EE enabled, teep refuses an endpoint that it does not cover. + CoversE2EE(endpoint e2ee.EndpointType) bool } // ModelLister fetches the list of available models from a provider. @@ -142,6 +145,15 @@ type Provider struct { // CompletionsPath is the upstream API path for text completions // (e.g. "/v1/completions"). Empty means unsupported. CompletionsPath string + // PrivacyClassifyPath and PrivacyRedactPath are the upstream API paths + // of privacy classification and redaction (e.g. "/v1/privacy/classify"). + // Empty means unsupported. + PrivacyClassifyPath string + PrivacyRedactPath string + + // ImagesEditsPath is the upstream API path for image edits + // (e.g. "/v1/images/edits", multipart). Empty means unsupported. + ImagesEditsPath string // ResponsesPath is the upstream API path for responses // (e.g. "/v1/responses"). Empty means unsupported. diff --git a/internal/provider/tinfoil/e2ee.go b/internal/provider/tinfoil/e2ee.go index e71580cb..5c0d79b0 100644 --- a/internal/provider/tinfoil/e2ee.go +++ b/internal/provider/tinfoil/e2ee.go @@ -39,3 +39,13 @@ func (t *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, _ e2 reader := session.EncryptRequest(bytes.NewReader(body)) return e2ee.EncryptResult{BodyReader: reader, EHBP: session}, nil } + +// CoversE2EE reports whether this E2EE covers endpoint. EHBP encrypts the whole body of chat, embeddings, responses and speech requests; teep sends no multipart audio body with EHBP. +func (t *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { + switch endpoint { + case e2ee.EndpointChat, e2ee.EndpointEmbeddings, e2ee.EndpointResponses, e2ee.EndpointSpeech: + return true + default: + return false + } +} diff --git a/internal/provider/venice/e2ee.go b/internal/provider/venice/e2ee.go index 4d14867e..59c58eb5 100644 --- a/internal/provider/venice/e2ee.go +++ b/internal/provider/venice/e2ee.go @@ -127,3 +127,13 @@ func veniceContentPlaintext(raw json.RawMessage, idx int) ([]byte, error) { } return nil, fmt.Errorf("message %d: unsupported content type (starts with %q)", idx, raw[0]) } + +// CoversE2EE reports whether this E2EE covers endpoint. Venice E2EE covers chat only. +func (v *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { + switch endpoint { + case e2ee.EndpointChat: + return true + default: + return false + } +} diff --git a/internal/proxy/aciv2_cache_test.go b/internal/proxy/aciv2_cache_test.go index 70914593..28f3d291 100644 --- a/internal/proxy/aciv2_cache_test.go +++ b/internal/proxy/aciv2_cache_test.go @@ -132,3 +132,6 @@ func TestE2EEConfirmedRequiresDecryptedField(t *testing.T) { t.Error("sessions whose relay enforces encrypted fields must be confirmed") } } + +// CoversE2EE covers every endpoint, so the test reaches EncryptRequest. +func (*formatRecordingEncryptor) CoversE2EE(e2ee.EndpointType) bool { return true } diff --git a/internal/proxy/dashboard.go b/internal/proxy/dashboard.go index 9a27a419..66642d98 100644 --- a/internal/proxy/dashboard.go +++ b/internal/proxy/dashboard.go @@ -97,8 +97,15 @@ func dashFactorStatus(s attestation.Status) string { type dashboardProvider struct { Upstream string `json:"upstream"` E2EE string `json:"e2ee"` - Requests int64 `json:"requests"` - Errors int64 `json:"errors"` + // E2EECovers and E2EEDoesNotCover list the endpoint paths that the + // provider's E2EE covers and does not; empty when E2EE is off. + E2EECovers []string `json:"e2ee_covers,omitempty"` + E2EEDoesNotCover []string `json:"e2ee_does_not_cover,omitempty"` + // RefusedNotCovered counts the requests refused because E2EE is enabled + // and the provider's E2EE does not cover their endpoint. + RefusedNotCovered int64 `json:"refused_not_covered"` + Requests int64 `json:"requests"` + Errors int64 `json:"errors"` } type dashboardRequests struct { @@ -207,9 +214,13 @@ func (s *Server) buildDashboardData() dashboardData { if p.E2EE { e2eeStatus = "enabled" } + covered, notCovered := e2eeCoverage(p) providers[name] = dashboardProvider{ - Upstream: p.BaseURL, - E2EE: e2eeStatus, + Upstream: p.BaseURL, + E2EE: e2eeStatus, + E2EECovers: covered, + E2EEDoesNotCover: notCovered, + RefusedNotCovered: s.stats.notCovered.get(name), } } diff --git a/internal/proxy/integration_tinfoil_test.go b/internal/proxy/integration_tinfoil_test.go index d0dd79b9..e68f2396 100644 --- a/internal/proxy/integration_tinfoil_test.go +++ b/internal/proxy/integration_tinfoil_test.go @@ -733,11 +733,11 @@ func assertAudioE2EEFailClosed(t *testing.T, resp *http.Response) { if err != nil { t.Fatalf("read audio E2EE body: %v", err) } - if resp.StatusCode != http.StatusBadRequest { - t.Fatalf("status = %d, want 400; body=%s", resp.StatusCode, body) + if resp.StatusCode != http.StatusForbidden { + t.Fatalf("status = %d, want 403; body=%s", resp.StatusCode, body) } - if !strings.Contains(string(body), "audio transcription requires TLS-level E2EE") { - t.Fatalf("body = %q, want fail-closed E2EE diagnostic", diagnosticBodySnippet(body)) + if !strings.Contains(string(body), `"type":"e2ee_not_covered"`) || !strings.Contains(string(body), "does not cover /v1/audio/transcriptions") { + t.Fatalf("body = %q, want the e2ee_not_covered refusal", diagnosticBodySnippet(body)) } } diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 63e59b79..1d30c2ee 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -129,6 +129,33 @@ type stats struct { modelsMu sync.RWMutex models map[string]*modelStats + + // notCovered counts the requests refused because the provider's E2EE + // does not cover their endpoint. + notCovered notCoveredStats +} + +// notCoveredStats counts, per provider, the requests that teep refused +// because E2EE is enabled and the provider's E2EE does not cover the +// endpoint. +type notCoveredStats struct { + mu sync.Mutex + counts map[string]int64 +} + +func (n *notCoveredStats) add(name string) { + n.mu.Lock() + defer n.mu.Unlock() + if n.counts == nil { + n.counts = make(map[string]int64) + } + n.counts[name]++ +} + +func (n *notCoveredStats) get(name string) int64 { + n.mu.Lock() + defer n.mu.Unlock() + return n.counts[name] } // modelStats holds per-model counters. @@ -566,6 +593,9 @@ func New(cfg *config.Config) (*Server, error) { s.mux.HandleFunc("POST /v1/rerank", s.handleEndpoint(&rerankEndpoint)) s.mux.HandleFunc("POST /v1/score", s.handleEndpoint(&scoreEndpoint)) s.mux.HandleFunc("POST /v1/completions", s.handleEndpoint(&completionsEndpoint)) + s.mux.HandleFunc("POST /v1/privacy/classify", s.handleEndpoint(&privacyClassifyEndpoint)) + s.mux.HandleFunc("POST /v1/privacy/redact", s.handleEndpoint(&privacyRedactEndpoint)) + s.mux.HandleFunc("POST /v1/images/edits", s.handleEndpoint(&imagesEditsEndpoint)) s.mux.HandleFunc("POST /v1/responses", s.handleEndpoint(&responsesEndpoint)) s.mux.HandleFunc("POST /v1/audio/speech", s.handleEndpoint(&speechEndpoint)) s.mux.HandleFunc("GET /v1/{$}", handleV1Help) @@ -750,6 +780,10 @@ func fromConfig( // NEAR model hosts encrypt completions; NEAR's gateway does not, so // nearcloud has no completions route. p.CompletionsPath = "/v1/completions" + p.PrivacyClassifyPath = "/v1/privacy/classify" + p.PrivacyRedactPath = "/v1/privacy/redact" + p.ImagesEditsPath = "/v1/images/edits" + p.ResponsesPath = "/v1/responses" rdVerifier := neardirect.ReportDataVerifier{} attester := neardirect.NewAttester(cp.BaseURL, cp.APIKey, offline) p.Attester = attester @@ -769,6 +803,11 @@ func fromConfig( p.EmbeddingsPath = "/v1/embeddings" p.RerankPath = "/v1/rerank" p.ScorePath = "/v1/score" + p.AudioPath = "/v1/audio/transcriptions" + p.PrivacyClassifyPath = "/v1/privacy/classify" + p.PrivacyRedactPath = "/v1/privacy/redact" + p.ImagesEditsPath = "/v1/images/edits" + p.ResponsesPath = "/v1/responses" p.Encryptor = neardirect.NewE2EE() rdVerifier := neardirect.ReportDataVerifier{} p.Attester = nearcloud.NewAttester(cp.APIKey, offline) @@ -1594,11 +1633,92 @@ type endpointConfig struct { // contentType is the default Content-Type for upstream requests. // If empty, the original request's Content-Type is preserved. contentType string +} + +// routedEndpoints lists every endpoint that teep routes, in display order. +func routedEndpoints() []*endpointConfig { + return []*endpointConfig{&chatEndpoint, &completionsEndpoint, &responsesEndpoint, &embeddingsEndpoint, &imagesEndpoint, &imagesEditsEndpoint, + &rerankEndpoint, &scoreEndpoint, &audioEndpoint, &speechEndpoint, &privacyClassifyEndpoint, &privacyRedactEndpoint} +} + +// e2eeCoverage splits the endpoints that prov serves into those its E2EE +// covers and those it does not. Both are nil when E2EE is off. +func e2eeCoverage(prov *provider.Provider) (covered, notCovered []string) { + if !prov.E2EE { + return nil, nil + } + for _, ep := range routedEndpoints() { + path := ep.endpointPath(prov) + if path == "" { + continue + } + if endpointCovered(prov, ep) { + covered = append(covered, path) + } else { + notCovered = append(notCovered, path) + } + } + return covered, notCovered +} + +// endpointCovered reports whether prov sends ep: E2EE is off, or its +// encryptor covers the endpoint. With E2EE on, a provider without an +// encryptor covers no endpoint. +func endpointCovered(prov *provider.Provider, ep *endpointConfig) bool { + if !prov.E2EE { + return true + } + return prov.Encryptor != nil && prov.Encryptor.CoversE2EE(ep.endpointType) +} + +// notCoveredMessage is the refusal message for an endpoint outside the +// provider's E2EE coverage. It states the transport that the request uses +// without E2EE: attested TLS only for a provider that uses TLS binding. +func notCoveredMessage(prov *provider.Provider, path string) string { + transport := " The request then goes over a TLS connection that attestation does not bind." + if prov.UsesTLSBinding { + transport = " The request then goes over the attested TLS connection." + } + return "teep did not send this request. E2EE is enabled for provider " + prov.Name + + ", and its end-to-end encryption does not cover " + path + + ". Nothing is wrong with teep or with the provider: you asked for end-to-end encryption, and this endpoint cannot be encrypted end to end." + + " To send it without E2EE, set e2ee = false for the provider in your teep config, and add e2ee_usable to its allow_fail list if its policy enforces that factor." + + transport +} + +// refuseUncoveredEndpoint refuses, with HTTP 403, a request whose endpoint +// the provider's E2EE does not cover while E2EE is enabled, and reports +// whether it did. The request is not sent. The log holds no request content. +func (s *Server) refuseUncoveredEndpoint(ctx context.Context, w http.ResponseWriter, path string, ep *endpointConfig, prov *provider.Provider, upstreamModel string) bool { + if endpointCovered(prov, ep) { + return false + } + s.stats.notCovered.add(prov.Name) + s.logInferenceBlock(ctx, "e2ee_not_covered", path, prov.Name, upstreamModel, http.StatusForbidden, + errors.New("E2EE enabled, endpoint not covered by the provider's E2EE, not sent")) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusForbidden) + if err := json.NewEncoder(w).Encode(notCoveredError{Error: notCoveredDetail{ + Message: notCoveredMessage(prov, path), + Type: "e2ee_not_covered", + Provider: prov.Name, + Endpoint: path, + }}); err != nil { + slog.ErrorContext(ctx, "e2ee_not_covered response encode", "err", err) + } + return true +} + +// notCoveredError is the JSON body of an e2ee_not_covered refusal. +type notCoveredError struct { + Error notCoveredDetail `json:"error"` +} - // preRouteGuard is an optional check run after model resolution but before - // routing. Returns an error message and true to block the request. - // Nil means no guard. - preRouteGuard func(prov *provider.Provider) (errMsg string, block bool) +type notCoveredDetail struct { + Message string `json:"message"` + Type string `json:"type"` + Provider string `json:"provider"` + Endpoint string `json:"endpoint"` } // parseChatRequest extracts model and stream flag from a chat completions JSON body. @@ -1681,15 +1801,29 @@ var ( endpointPath: func(p *provider.Provider) string { return p.AudioPath }, unsupported: "audio transcription", parseRequest: parseAudioModelRequest, - preRouteGuard: func(prov *provider.Provider) (string, bool) { - // Non-pinned E2EE providers (Chutes, nearcloud) require body encryption, - // which doesn't support multipart. Fail closed to prevent silently - // sending plaintext. - if prov.E2EE { - return "audio transcription requires TLS-level E2EE (pinned provider)", true - } - return "", false - }, + } + privacyClassifyEndpoint = endpointConfig{ + name: "privacy_classify", + endpointType: e2ee.EndpointPrivacy, + endpointPath: func(p *provider.Provider) string { return p.PrivacyClassifyPath }, + unsupported: "privacy classification", + parseRequest: parseJSONModelRequest, + contentType: "application/json", + } + privacyRedactEndpoint = endpointConfig{ + name: "privacy_redact", + endpointType: e2ee.EndpointPrivacy, + endpointPath: func(p *provider.Provider) string { return p.PrivacyRedactPath }, + unsupported: "privacy redaction", + parseRequest: parseJSONModelRequest, + contentType: "application/json", + } + imagesEditsEndpoint = endpointConfig{ + name: "images_edits", + endpointType: e2ee.EndpointImagesEdits, + endpointPath: func(p *provider.Provider) string { return p.ImagesEditsPath }, + unsupported: "image edits", + parseRequest: parseAudioModelRequest, // multipart with a model field, as audio } completionsEndpoint = endpointConfig{ name: "completions", @@ -1809,12 +1943,8 @@ func (s *Server) endpointHandler(ep *endpointConfig, observe func(*attestation.V logChatRequestStats(ctx, &s.reasoningStripLogs, model, prov.Name, upstreamModel, r.URL.Path, body, reasoningStats, reasoningRepair) } - if ep.preRouteGuard != nil { - if errMsg, block := ep.preRouteGuard(prov); block { - s.logInferenceBlock(ctx, "pre_route_guard", ep.name, prov.Name, upstreamModel, http.StatusBadRequest, errors.New(errMsg)) - http.Error(w, errMsg, http.StatusBadRequest) - return - } + if s.refuseUncoveredEndpoint(ctx, w, r.URL.Path, ep, prov, upstreamModel) { + return } var attestDur, e2eeDur, upstreamDur time.Duration diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index 0b5140ce..53e2c177 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -4474,3 +4474,6 @@ func TestMetrics(t *testing.T) { } } } + +// CoversE2EE covers every endpoint, so the test reaches EncryptRequest. +func (passthroughEncryptor) CoversE2EE(e2ee.EndpointType) bool { return true } diff --git a/internal/proxy/relay_internal_test.go b/internal/proxy/relay_internal_test.go index 407d5e7e..55ede2a6 100644 --- a/internal/proxy/relay_internal_test.go +++ b/internal/proxy/relay_internal_test.go @@ -2149,3 +2149,6 @@ func TestVerifySupplyChain_GatewayDigestsRequireVerifiedBinding(t *testing.T) { }) } } + +// CoversE2EE covers every endpoint, so the test reaches EncryptRequest. +func (*mockRequestEncryptor) CoversE2EE(e2ee.EndpointType) bool { return true } diff --git a/internal/proxy/signing_key_miss_test.go b/internal/proxy/signing_key_miss_test.go index e9cb36ba..a3700c76 100644 --- a/internal/proxy/signing_key_miss_test.go +++ b/internal/proxy/signing_key_miss_test.go @@ -86,6 +86,9 @@ func (e *recordingEncryptor) EncryptRequest(_ []byte, raw *attestation.RawAttest return e2ee.EncryptResult{Body: []byte("encrypted")}, nil } +// CoversE2EE covers every endpoint, so the test reaches EncryptRequest. +func (*recordingEncryptor) CoversE2EE(e2ee.EndpointType) bool { return true } + func (e *recordingEncryptor) encrypted() []string { e.mu.Lock() defer e.mu.Unlock() diff --git a/internal/proxy/templates/dashboard.html b/internal/proxy/templates/dashboard.html index 8cb21c1d..b131c0fe 100644 --- a/internal/proxy/templates/dashboard.html +++ b/internal/proxy/templates/dashboard.html @@ -548,6 +548,8 @@

Endpoints

var errCls = (p.errors || 0) > 0 ? ' class="bad"' : ''; h += '' + esc(names[i]) + '' + esc(p.upstream) + '' + esc(p.e2ee) + + ((p.e2ee_does_not_cover || []).length ? '
not covered by E2EE (refused): ' + + esc(p.e2ee_does_not_cover.join(", ")) + (p.refused_not_covered ? ' · refused: ' + esc(p.refused_not_covered) : '') + '
' : '') + '' + esc(p.requests || 0) + '' + esc(p.errors || 0) + ''; } el("prov-rows").innerHTML = h; diff --git a/internal/proxy/uncovered_test.go b/internal/proxy/uncovered_test.go new file mode 100644 index 00000000..6952b556 --- /dev/null +++ b/internal/proxy/uncovered_test.go @@ -0,0 +1,216 @@ +package proxy + +import ( + "bytes" + "encoding/json" + "io" + "mime/multipart" + "net/http" + "net/http/httptest" + "slices" + "strings" + "sync" + "testing" + + "github.com/13rac1/teep/internal/config" + "github.com/13rac1/teep/internal/e2ee" + "github.com/13rac1/teep/internal/provider" + "github.com/13rac1/teep/internal/tlsct/testtls" +) + +// newCoverageServer returns an offline server with one provider, E2EE on. +func newCoverageServer(t *testing.T, name string) *Server { + t.Helper() + s, err := New(&config.Config{Offline: true, Providers: map[string]*config.Provider{ + name: {Name: name, BaseURL: "https://example.com", APIKey: "test-key", E2EE: true}, + }}) + if err != nil { + t.Fatal(err) + } + t.Cleanup(s.Close) + return s +} + +// uncoveredRequest builds a request for path: multipart for the file +// endpoints, JSON for the others. +func uncoveredRequest(path, model string) *http.Request { + var body io.Reader = strings.NewReader(`{"model":"` + model + `","input":"hello"}`) + contentType := "application/json" + if path == "/v1/images/edits" || path == "/v1/audio/transcriptions" { + var buf bytes.Buffer + mw := multipart.NewWriter(&buf) + _ = mw.WriteField("model", model) + fw, _ := mw.CreateFormFile("file", "in.bin") + _, _ = fw.Write([]byte("data")) + _ = mw.Close() + body, contentType = &buf, mw.FormDataContentType() + } + req := httptest.NewRequest(http.MethodPost, path, body) + req.Header.Set("Content-Type", contentType) + return req +} + +// With E2EE enabled, an endpoint outside the provider's E2EE coverage is +// refused with 403, an e2ee_not_covered body that names the provider, the +// endpoint and the setting, and a counted refusal; no request is sent. +func TestUncoveredEndpointRefused(t *testing.T) { + near := []string{"/v1/privacy/classify", "/v1/privacy/redact", "/v1/responses", "/v1/images/edits", "/v1/audio/transcriptions"} + tests := []struct { + provider string + paths []string + }{ + {"neardirect", near}, + {"nearcloud", near}, + {"phalacloud", []string{"/v1/embeddings"}}, + {"tinfoil_v3_cloud", []string{"/v1/audio/transcriptions"}}, + } + for _, tt := range tests { + t.Run(tt.provider, func(t *testing.T) { + s := newCoverageServer(t, tt.provider) + for i, path := range tt.paths { + t.Run(strings.TrimPrefix(path, "/v1/"), func(t *testing.T) { + rec := httptest.NewRecorder() + s.mux.ServeHTTP(rec, uncoveredRequest(path, tt.provider+":m")) + if rec.Code != http.StatusForbidden { + t.Fatalf("status %d, want 403; body %s", rec.Code, rec.Body.String()) + } + var out notCoveredError + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("body %s: %v", rec.Body.String(), err) + } + e := out.Error + if e.Type != "e2ee_not_covered" || e.Provider != tt.provider || e.Endpoint != path || + e.Message != notCoveredMessage(s.providers[tt.provider], path) { + t.Errorf("error = %+v", e) + } + if got := s.stats.notCovered.get(tt.provider); got != int64(i+1) { + t.Errorf("refused count = %d, want %d", got, i+1) + } + }) + } + }) + } +} + +// The message names the setting, and states attested TLS only for a +// provider that uses TLS binding. +func TestNotCoveredMessage(t *testing.T) { + bound := notCoveredMessage(&provider.Provider{Name: "p", UsesTLSBinding: true}, "/v1/x") + unbound := notCoveredMessage(&provider.Provider{Name: "p"}, "/v1/x") + for _, m := range []string{bound, unbound} { + if !strings.Contains(m, "set e2ee = false for the provider") || !strings.Contains(m, "e2ee_usable") || + !strings.Contains(m, "provider p") || !strings.Contains(m, "/v1/x") { + t.Errorf("message %q lacks the setting, the provider or the endpoint", m) + } + } + if !strings.Contains(bound, "attested TLS connection") { + t.Errorf("TLS-binding message %q does not state attested TLS", bound) + } + if strings.Contains(unbound, "attested TLS") || !strings.Contains(unbound, "attestation does not bind") { + t.Errorf("message without TLS binding %q claims attested TLS", unbound) + } +} + +// With e2ee = false a new route is a plain route: the body is sent as it +// is over the attested TLS connection, and the answer is relayed. +func TestUncoveredEndpointWithoutE2EE(t *testing.T) { + testtls.RunWithFallbackRoot(t, func(t *testing.T, authority *testtls.Authority) { + t.Helper() + const body = `{"model":"model","input":"hello"}` + upstream, fp := newTLSBindingTestServerWithHandler(t, authority, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + got, _ := io.ReadAll(io.LimitReader(r.Body, 1<<20)) + if string(got) != body || r.URL.Path != "/v1/privacy/classify" { + t.Errorf("upstream got %s at %s", got, r.URL.Path) + } + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"data":[]}`) + })) + server, input, _ := nearCompletionAuthorization(t, "neardirect", upstream.URL, fp) + prov := *input.provider + prov.E2EE = false + request := *input + request.provider = &prov + request.body = []byte(body) + request.path = "/v1/privacy/classify" + request.endpoint = e2ee.EndpointPrivacy + rec := newInferenceRecorder() + out, err := server.inferAuthorized(t.Context(), rec, &request) + if err != nil || out.status != "ok" || rec.Body.String() != `{"data":[]}` { + t.Fatalf("status %q err %v body %s", out.status, err, rec.Body.String()) + } + }) +} + +// The coverage split follows each encryptor's declaration, a covered +// endpoint is not refused, and with E2EE on a provider without an +// encryptor covers nothing. +func TestE2EECoverage(t *testing.T) { + tests := []struct { + provider string + covered []string + notCovered []string + }{ + {"neardirect", []string{"/v1/chat/completions", "/v1/completions", "/v1/embeddings", "/v1/images/generations", "/v1/rerank", "/v1/score"}, + []string{"/v1/responses", "/v1/images/edits", "/v1/audio/transcriptions", "/v1/privacy/classify", "/v1/privacy/redact"}}, + {"nearcloud", []string{"/v1/chat/completions", "/v1/embeddings", "/v1/images/generations", "/v1/rerank", "/v1/score"}, + []string{"/v1/responses", "/v1/images/edits", "/v1/audio/transcriptions", "/v1/privacy/classify", "/v1/privacy/redact"}}, + {"tinfoil_v3_cloud", []string{"/v1/chat/completions", "/v1/responses", "/v1/embeddings", "/v1/audio/speech"}, + []string{"/v1/audio/transcriptions"}}, + {"phalacloud", []string{"/v1/chat/completions"}, []string{"/v1/embeddings"}}, + {"chutes", []string{"/v1/chat/completions", "/v1/embeddings"}, nil}, + {"venice", []string{"/api/v1/chat/completions"}, nil}, + {"nanogpt", []string{"/v1/chat/completions"}, nil}, + } + for _, tt := range tests { + t.Run(tt.provider, func(t *testing.T) { + s := newCoverageServer(t, tt.provider) + prov := s.providers[tt.provider] + covered, notCovered := e2eeCoverage(prov) + if !slices.Equal(covered, tt.covered) || !slices.Equal(notCovered, tt.notCovered) { + t.Errorf("coverage = %v / %v, want %v / %v", covered, notCovered, tt.covered, tt.notCovered) + } + data := s.buildDashboardData() + if p := data.Providers[tt.provider]; !slices.Equal(p.E2EECovers, tt.covered) || !slices.Equal(p.E2EEDoesNotCover, tt.notCovered) { + t.Errorf("dashboard provider = %+v", p) + } + off := *prov + off.E2EE = false + if c, n := e2eeCoverage(&off); c != nil || n != nil { + t.Errorf("E2EE off: coverage = %v / %v, want none", c, n) + } + for _, ep := range routedEndpoints() { + if !endpointCovered(&off, ep) { + t.Errorf("E2EE off: %s refused", ep.name) + } + } + none := *prov + none.Encryptor = nil + c, n := e2eeCoverage(&none) + if c != nil || len(n) != len(tt.covered)+len(tt.notCovered) { + t.Errorf("no encryptor: coverage = %v / %v, want every endpoint not covered", c, n) + } + }) + } +} + +// Concurrent refusals of several providers are all counted. +func TestNotCoveredStatsConcurrent(t *testing.T) { + var st notCoveredStats + names := []string{"a", "b", "c"} + const perName = 200 + var wg sync.WaitGroup + for _, name := range names { + for range perName { + wg.Go(func() { + st.add(name) + _ = st.get(name) + }) + } + } + wg.Wait() + for _, name := range names { + if got := st.get(name); got != perName { + t.Errorf("%s: count = %d, want %d", name, got, perName) + } + } +} From dd94aec3b0e7059bb246f490827a034f00765305 Mon Sep 17 00:00:00 2001 From: AI 13rac1 Date: Sat, 14 Nov 2026 03:58:47 +0000 Subject: [PATCH 2/2] Make every encryptor refuse the endpoints it does not declare The Tinfoil and Chutes encryptors ignored their endpoint argument, so their CoversE2EE declaration was the only check of which endpoints they encrypt. A wrong entry there would send a body that the provider cannot use through EHBP or Chutes E2EE with no other check to stop it. Each encryptor now checks its own CoversE2EE first and returns e2ee.ErrEndpointNotCovered before it reads the key or the body. A test runs every endpoint type through each production encryptor and fails when the declaration and the encryption disagree. The dashboard also lists the endpoints that each provider's E2EE covers, next to those it does not. To verify: go test ./internal/proxy -run TestEncryptorsRefuseUncoveredEndpoints. Co-Authored-By: Claude Opus 5.5 --- internal/e2ee/session.go | 10 ++++++- internal/provider/aci/e2ee.go | 4 +-- internal/provider/chutes/e2ee.go | 11 +++++--- internal/provider/neardirect/e2ee.go | 9 +++++-- internal/provider/tinfoil/e2ee.go | 11 +++++--- internal/provider/venice/e2ee.go | 4 +-- internal/provider/venice/e2ee_test.go | 5 ++-- internal/proxy/templates/dashboard.html | 1 + internal/proxy/uncovered_test.go | 36 +++++++++++++++++++++++++ 9 files changed, 76 insertions(+), 15 deletions(-) diff --git a/internal/e2ee/session.go b/internal/e2ee/session.go index 693d79a7..196c735e 100644 --- a/internal/e2ee/session.go +++ b/internal/e2ee/session.go @@ -8,7 +8,10 @@ // Dependency flow: attestation → e2ee → provider → proxy → cmd package e2ee -import "io" +import ( + "errors" + "io" +) // EndpointType identifies the canonical OpenAI-compatible endpoint for field // encryption policy routing. These types are independent of provider-specific @@ -40,6 +43,11 @@ const ( EndpointImagesEdits EndpointType = "images_edits" ) +// ErrEndpointNotCovered is returned by an encryptor for an endpoint that its +// E2EE protocol does not support. The encryptor returns it before it reads +// the key or the body. +var ErrEndpointNotCovered = errors.New("E2EE does not support this endpoint") + // EncField* constants identify the dot-notation sub-field paths within inference // API JSON structures that are subject to field-level encryption. Each constant // value is the exact path string passed to Decryptor.IsResponseFieldEncrypted diff --git a/internal/provider/aci/e2ee.go b/internal/provider/aci/e2ee.go index cf452dfa..bf8d00ef 100644 --- a/internal/provider/aci/e2ee.go +++ b/internal/provider/aci/e2ee.go @@ -30,8 +30,8 @@ func NewE2EE() *E2EE { return &E2EE{now: time.Now} } // v2 encrypts to. The body's top-level model is bound into every field's // associated data, so a party that changes it makes the request fail. func (a *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, endpoint e2ee.EndpointType) (e2ee.EncryptResult, error) { - if endpoint != e2ee.EndpointChat { - return e2ee.EncryptResult{}, fmt.Errorf("E2EE v2 unsupported endpoint %q", endpoint) + if !a.CoversE2EE(endpoint) { + return e2ee.EncryptResult{}, fmt.Errorf("E2EE v2: endpoint %q: %w", endpoint, e2ee.ErrEndpointNotCovered) } if raw.BackendFormat != attestation.FormatACI1 { return e2ee.EncryptResult{}, fmt.Errorf("E2EE v2 requires an ACI/1 attestation, got format %q", raw.BackendFormat) diff --git a/internal/provider/chutes/e2ee.go b/internal/provider/chutes/e2ee.go index 70a76459..8dc84e00 100644 --- a/internal/provider/chutes/e2ee.go +++ b/internal/provider/chutes/e2ee.go @@ -2,6 +2,7 @@ package chutes import ( "errors" + "fmt" "github.com/13rac1/teep/internal/attestation" "github.com/13rac1/teep/internal/e2ee" @@ -20,8 +21,11 @@ func NewE2EE() *E2EE { // (ML-KEM-768 + ChaCha20-Poly1305). // Returns ChutesE2EE for the Preparer to inject headers. // Requires raw.InstanceID, raw.E2ENonce, and raw.ChuteID from attestation. -// The endpoint parameter is not used; Chutes uses full-body encryption for all endpoints. -func (c *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, _ e2ee.EndpointType) (e2ee.EncryptResult, error) { +// An endpoint outside CoversE2EE fails with e2ee.ErrEndpointNotCovered. +func (c *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, endpoint e2ee.EndpointType) (e2ee.EncryptResult, error) { + if !c.CoversE2EE(endpoint) { + return e2ee.EncryptResult{}, fmt.Errorf("chutes E2EE: endpoint %q: %w", endpoint, e2ee.ErrEndpointNotCovered) + } encPayload, session, err := e2ee.EncryptChatRequestChutes(body, raw.SigningKey) if err != nil { return e2ee.EncryptResult{}, err @@ -46,7 +50,8 @@ func (c *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, _ e2 return e2ee.EncryptResult{Body: encPayload, Chutes: meta}, nil } -// CoversE2EE reports whether this E2EE covers endpoint. Chutes E2EE encrypts the whole body of chat and embeddings requests. +// CoversE2EE reports whether this E2EE covers endpoint. Chutes E2EE +// encrypts the whole body of chat and embeddings requests. func (c *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { switch endpoint { case e2ee.EndpointChat, e2ee.EndpointEmbeddings: diff --git a/internal/provider/neardirect/e2ee.go b/internal/provider/neardirect/e2ee.go index 33015ef9..facb93d2 100644 --- a/internal/provider/neardirect/e2ee.go +++ b/internal/provider/neardirect/e2ee.go @@ -27,6 +27,9 @@ func NewE2EE() *E2EE { return &E2EE{} } // // Unsupported endpoints fail closed. func (n *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, endpoint e2ee.EndpointType) (e2ee.EncryptResult, error) { + if !n.CoversE2EE(endpoint) { + return e2ee.EncryptResult{}, fmt.Errorf("NearAI E2EE: endpoint %q: %w", endpoint, e2ee.ErrEndpointNotCovered) + } modelKey, err := e2ee.ParseNearModelKey(raw.SigningKey) if err != nil { return e2ee.EncryptResult{}, err @@ -52,7 +55,7 @@ func (n *E2EE) EncryptRequestWithModelKey(body []byte, modelKey e2ee.NearModelKe case e2ee.EndpointCompletions: encrypt = e2ee.EncryptCompletionsNearCloud default: - return e2ee.EncryptResult{}, fmt.Errorf("NearAI E2EE not supported for endpoint %q", endpoint) + return e2ee.EncryptResult{}, fmt.Errorf("NearAI E2EE: endpoint %q: %w", endpoint, e2ee.ErrEndpointNotCovered) } encBody, session, err := encrypt(body, modelKey) if err != nil { @@ -61,7 +64,9 @@ func (n *E2EE) EncryptRequestWithModelKey(body []byte, modelKey e2ee.NearModelKe return e2ee.EncryptResult{Body: encBody, Session: session}, nil } -// CoversE2EE reports whether this E2EE covers endpoint. NEAR E2EE covers chat, completions, embeddings, image generation, rerank, and score (request fields only). +// CoversE2EE reports whether this E2EE covers endpoint. NEAR E2EE covers +// chat, completions, embeddings, image generation, rerank, and score (request +// fields only). func (n *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { switch endpoint { case e2ee.EndpointChat, e2ee.EndpointCompletions, e2ee.EndpointEmbeddings, e2ee.EndpointImages, e2ee.EndpointRerank, e2ee.EndpointScore: diff --git a/internal/provider/tinfoil/e2ee.go b/internal/provider/tinfoil/e2ee.go index 5c0d79b0..b61a9850 100644 --- a/internal/provider/tinfoil/e2ee.go +++ b/internal/provider/tinfoil/e2ee.go @@ -20,8 +20,11 @@ func NewE2EE() *E2EE { return &E2EE{} } // EncryptRequest encrypts the request body for Tinfoil EHBP // (HPKE X25519 + AES-256-GCM full-body encryption). // The raw.SigningKey must be a 64 hex char (32 byte) X25519 public key. -// The endpoint parameter is not used; EHBP uses full-body encryption for all endpoints. -func (t *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, _ e2ee.EndpointType) (e2ee.EncryptResult, error) { +// An endpoint outside CoversE2EE fails with e2ee.ErrEndpointNotCovered. +func (t *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, endpoint e2ee.EndpointType) (e2ee.EncryptResult, error) { + if !t.CoversE2EE(endpoint) { + return e2ee.EncryptResult{}, fmt.Errorf("tinfoil E2EE: endpoint %q: %w", endpoint, e2ee.ErrEndpointNotCovered) + } if raw.SigningKey == "" { return e2ee.EncryptResult{}, errors.New("tinfoil E2EE: missing HPKE public key in attestation") } @@ -40,7 +43,9 @@ func (t *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, _ e2 return e2ee.EncryptResult{BodyReader: reader, EHBP: session}, nil } -// CoversE2EE reports whether this E2EE covers endpoint. EHBP encrypts the whole body of chat, embeddings, responses and speech requests; teep sends no multipart audio body with EHBP. +// CoversE2EE reports whether this E2EE covers endpoint. EHBP encrypts the +// whole body of chat, embeddings, responses and speech requests; teep sends +// no multipart audio body with EHBP. func (t *E2EE) CoversE2EE(endpoint e2ee.EndpointType) bool { switch endpoint { case e2ee.EndpointChat, e2ee.EndpointEmbeddings, e2ee.EndpointResponses, e2ee.EndpointSpeech: diff --git a/internal/provider/venice/e2ee.go b/internal/provider/venice/e2ee.go index 59c58eb5..6738ca46 100644 --- a/internal/provider/venice/e2ee.go +++ b/internal/provider/venice/e2ee.go @@ -22,8 +22,8 @@ func NewE2EE() *E2EE { return &E2EE{} } // The endpoint must be EndpointChat; Venice E2EE is only defined for // /api/v1/chat/completions. func (v *E2EE) EncryptRequest(body []byte, raw *attestation.RawAttestation, endpoint e2ee.EndpointType) (e2ee.EncryptResult, error) { - if endpoint != e2ee.EndpointChat { - return e2ee.EncryptResult{}, fmt.Errorf("venice E2EE unsupported endpoint %q", endpoint) + if !v.CoversE2EE(endpoint) { + return e2ee.EncryptResult{}, fmt.Errorf("venice E2EE: endpoint %q: %w", endpoint, e2ee.ErrEndpointNotCovered) } session, err := e2ee.NewVeniceSession() diff --git a/internal/provider/venice/e2ee_test.go b/internal/provider/venice/e2ee_test.go index b7017b8d..9612080d 100644 --- a/internal/provider/venice/e2ee_test.go +++ b/internal/provider/venice/e2ee_test.go @@ -3,6 +3,7 @@ package venice_test import ( "encoding/hex" "encoding/json" + "errors" "testing" "github.com/13rac1/teep/internal/attestation" @@ -149,8 +150,8 @@ func TestVeniceE2EE_EncryptRequest_UnsupportedEndpoint(t *testing.T) { if err == nil { t.Fatal("expected error for unsupported endpoint") } - if got := err.Error(); got != "venice E2EE unsupported endpoint \"embeddings\"" { - t.Fatalf("error = %q, want %q", got, "venice E2EE unsupported endpoint \"embeddings\"") + if !errors.Is(err, e2ee.ErrEndpointNotCovered) { + t.Fatalf("error = %v, want e2ee.ErrEndpointNotCovered", err) } } diff --git a/internal/proxy/templates/dashboard.html b/internal/proxy/templates/dashboard.html index b131c0fe..888d8805 100644 --- a/internal/proxy/templates/dashboard.html +++ b/internal/proxy/templates/dashboard.html @@ -548,6 +548,7 @@

Endpoints

var errCls = (p.errors || 0) > 0 ? ' class="bad"' : ''; h += '' + esc(names[i]) + '' + esc(p.upstream) + '' + esc(p.e2ee) + + ((p.e2ee_covers || []).length ? '
covered by E2EE: ' + esc(p.e2ee_covers.join(", ")) + '
' : '') + ((p.e2ee_does_not_cover || []).length ? '
not covered by E2EE (refused): ' + esc(p.e2ee_does_not_cover.join(", ")) + (p.refused_not_covered ? ' · refused: ' + esc(p.refused_not_covered) : '') + '
' : '') + '' + esc(p.requests || 0) + '' + esc(p.errors || 0) + ''; diff --git a/internal/proxy/uncovered_test.go b/internal/proxy/uncovered_test.go index 6952b556..70a7602a 100644 --- a/internal/proxy/uncovered_test.go +++ b/internal/proxy/uncovered_test.go @@ -3,6 +3,7 @@ package proxy import ( "bytes" "encoding/json" + "errors" "io" "mime/multipart" "net/http" @@ -12,9 +13,15 @@ import ( "sync" "testing" + "github.com/13rac1/teep/internal/attestation" "github.com/13rac1/teep/internal/config" "github.com/13rac1/teep/internal/e2ee" "github.com/13rac1/teep/internal/provider" + "github.com/13rac1/teep/internal/provider/aci" + "github.com/13rac1/teep/internal/provider/chutes" + "github.com/13rac1/teep/internal/provider/neardirect" + "github.com/13rac1/teep/internal/provider/tinfoil" + "github.com/13rac1/teep/internal/provider/venice" "github.com/13rac1/teep/internal/tlsct/testtls" ) @@ -214,3 +221,32 @@ func TestNotCoveredStatsConcurrent(t *testing.T) { } } } + +// Each production encryptor refuses, before it reads the key or the body, +// every endpoint outside its declared coverage, so the declaration and the +// encryption cannot disagree. +func TestEncryptorsRefuseUncoveredEndpoints(t *testing.T) { + encryptors := map[string]provider.RequestEncryptor{ + "aci": aci.NewE2EE(), + "chutes": chutes.NewE2EE(), + "neardirect": neardirect.NewE2EE(), + "tinfoil": tinfoil.NewE2EE(), + "venice": venice.NewE2EE(), + } + endpoints := []e2ee.EndpointType{ + e2ee.EndpointChat, e2ee.EndpointEmbeddings, e2ee.EndpointImages, e2ee.EndpointRerank, + e2ee.EndpointScore, e2ee.EndpointAudio, e2ee.EndpointResponses, e2ee.EndpointSpeech, + e2ee.EndpointCompletions, e2ee.EndpointPrivacy, e2ee.EndpointImagesEdits, + } + for name, enc := range encryptors { + t.Run(name, func(t *testing.T) { + for _, ep := range endpoints { + _, err := enc.EncryptRequest([]byte(`{}`), &attestation.RawAttestation{}, ep) + notCovered := errors.Is(err, e2ee.ErrEndpointNotCovered) + if enc.CoversE2EE(ep) == notCovered { + t.Errorf("%s: CoversE2EE = %v, EncryptRequest error = %v", ep, enc.CoversE2EE(ep), err) + } + } + }) + } +}