Skip to content
Open
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ shared across its API keys; excess requests return 429 with
Live entries are never evicted to admit new requests. Metrics bind to loopback
in Nitro builds and retain their internal-network listener in ordinary builds.

A confidential request may set `"require_attested_inference": true` in its
encrypted envelope, next to `schema_version`, `request_id`, `issued_at` and
`request`. The enclave then lets only attested code read the prompt: itself
and a provider whose attestation it verifies before sending anything (today,
Tinfoil). It refuses routers such as `nexus/auto`, which send the prompt to
the Router, skips PII masking, which sends it to the analyzer, and never falls
back to a provider without that verification. Nor does it store a Tinfoil
proof of the request: the client keeps its own. A request it can't serve that
way returns 422 `attested_inference_unavailable` and nothing is sent upstream.
The field is inside the encrypted body, so nothing between the client and the
enclave can add or remove it, and a Gateway that predates it rejects the
envelope rather than ignoring it.

PII masking covers conversation text, tool results, assistant reasoning, tool-call
argument string values, and `user`. Tool definitions, names, IDs, schemas,
metadata, provider options, and image/audio content are not scanned. Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type confidentialEnvelope struct {
RequestID string `json:"request_id"`
IssuedAt string `json:"issued_at"`
Request json.RawMessage `json:"request"`
// RequireAttestedInference asks that only attested code read the prompt:
// this enclave and a provider it verifies by attestation. See
// domain.WithAttestedInference. Gateways that predate the field reject
// the envelope as unknown, so asking for it never silently does nothing.
RequireAttestedInference bool `json:"require_attested_inference,omitempty"`
}

// NewConfidentialChatCompletionsHandler creates a strict encrypted handler.
Expand Down Expand Up @@ -195,6 +200,9 @@ func (h *ConfidentialChatCompletionsHandler) handleDecrypted(w http.ResponseWrit
// The downstream handler rechecks authentication and remains responsible for
// OpenAI validation, generation, and streaming. Only replace the
// decrypted envelope with its exact nested OpenAI request object.
if envelope.RequireAttestedInference {
r = r.WithContext(domain.WithAttestedInference(r.Context()))
}
r.Body = io.NopCloser(bytes.NewReader(envelope.Request))
r.ContentLength = int64(len(envelope.Request))
r.Header.Del("Content-Length")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -267,6 +268,78 @@ func TestConfidentialChatCompletionsHandler_RejectsInvalidFreshnessAndReplay(t *
}
}

// The attested-inference requirement travels inside the encrypted envelope,
// so only the client that encrypted the request can set it, and the
// downstream handler sees it only when it was asked for.
func TestConfidentialChatCompletionsHandler_CarriesAttestedInferenceRequirement(t *testing.T) {
request := json.RawMessage(`{"model":"private/model","messages":[]}`)
envelopeWith := func(field string) []byte {
return []byte(fmt.Sprintf(`{"schema_version":%d,"request_id":%q,"issued_at":%q,"request":%s%s}`,
confidentialEnvelopeSchema, uuid.NewString(), time.Now().UTC().Format(time.RFC3339Nano), request, field))
}
tests := []struct {
name string
envelope []byte
wantStatus int
wantRequired bool
}{
{name: "required", envelope: envelopeWith(`,"require_attested_inference":true`), wantStatus: http.StatusOK, wantRequired: true},
{name: "explicitly not required", envelope: envelopeWith(`,"require_attested_inference":false`), wantStatus: http.StatusOK},
{name: "absent", envelope: envelopeWith(""), wantStatus: http.StatusOK},
{name: "not a boolean", envelope: envelopeWith(`,"require_attested_inference":"yes"`), wantStatus: http.StatusBadRequest},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
calls, required := 0, false
var body []byte
handler, clientIdentity := newConfidentialTestHandler(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
required = domain.AttestedInferenceRequired(r.Context())
body, _ = io.ReadAll(r.Body)
_, _ = io.WriteString(w, "ok")
}))
encrypted, requestContext, _ := newEncryptedConfidentialRequest(t, clientIdentity, test.envelope)
recorder := httptest.NewRecorder()

handler.Handle(recorder, encrypted)
_ = decryptConfidentialResponse(t, recorder, requestContext)

if recorder.Code != test.wantStatus {
t.Fatalf("status = %d, want %d", recorder.Code, test.wantStatus)
}
if test.wantStatus != http.StatusOK {
if calls != 0 {
t.Fatalf("downstream calls = %d, want 0", calls)
}
return
}
if required != test.wantRequired {
t.Fatalf("attested inference required = %v, want %v", required, test.wantRequired)
}
if !bytes.Equal(body, request) {
t.Fatalf("downstream body = %s, want only the nested request %s", body, request)
}
})
}
}

// A plaintext request can't carry the requirement: the header is ignored and
// the endpoint refuses plaintext before any handler runs.
func TestConfidentialChatCompletionsHandler_PlaintextCannotRequestAttestedInference(t *testing.T) {
calls := 0
handler, _ := newConfidentialTestHandler(t, http.HandlerFunc(func(http.ResponseWriter, *http.Request) { calls++ }))
request := httptest.NewRequest(http.MethodPost, "/v1/confidential/chat/completions",
strings.NewReader(`{"schema_version":1,"require_attested_inference":true}`))
request.Header.Set("Authorization", "Bearer nexus-test-key")
recorder := httptest.NewRecorder()

handler.Handle(recorder, request)

if recorder.Code != http.StatusBadRequest || calls != 0 {
t.Fatalf("status = %d, downstream calls = %d; want 400 and none", recorder.Code, calls)
}
}

func TestConfidentialChatCompletionsHandler_RejectsTamperingAndStaleKey(t *testing.T) {
tests := []struct {
name string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package providers_test

import (
"testing"
"time"

"github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/adapters/providers/openai"
"github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/adapters/providers/tinfoil"
"github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/adapters/providers/typesafe"
"github.com/dappnode/dappnode-nexus-gateway/apps/gateway/internal/application/ports"
)

// Attested inference sends a prompt only to adapters that claim an attested
// transport, so the claim must be exactly where verification happens.
func TestOnlyTinfoilClaimsAnAttestedTransport(t *testing.T) {
adapters := map[string]ports.Provider{
"tinfoil": tinfoil.NewAdapter(time.Second),
"openai": openai.NewAdapter(time.Second),
"typesafe": typesafe.NewAdapter(time.Second),
}
for name, adapter := range adapters {
attested, ok := adapter.(ports.AttestedProvider)
got := ok && attested.AttestedTransport()
if want := name == "tinfoil"; got != want {
t.Errorf("%s adapter attested transport = %v, want %v", name, got, want)
}
}
}
5 changes: 5 additions & 0 deletions apps/gateway/internal/adapters/providers/tinfoil/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func NewAdapterWithFactory(timeout time.Duration, factory VerifiedClientFactory,
return &Adapter{timeout: timeout, factory: factory, logger: l}
}

// AttestedTransport reports that every request is sent only through a client
// that has verified the Tinfoil enclave's attestation, over EHBP to the key
// that attestation binds, and that verification failure sends nothing.
func (a *Adapter) AttestedTransport() bool { return true }

func missingProviderCredentialError() *domain.GatewayError {
return domain.ErrInternal("an internal error occurred").WithMeta(
"provider", providerName,
Expand Down
10 changes: 10 additions & 0 deletions apps/gateway/internal/application/ports/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ type Provider interface {
Complete(ctx context.Context, raw []byte, model domain.PublicModel) ([]byte, *domain.TinfoilTransportProof, error)
}

// AttestedProvider is a provider adapter that sends a request only after it
// has verified the provider's hardware attestation, encrypted to a key bound
// to that attestation, so the provider's operator can't read the prompt
// either. It must fail closed: no verification, no request. Requests that
// require attested inference (domain.AttestedInferenceRequired) are sent only
// to providers whose adapter reports true.
type AttestedProvider interface {
AttestedTransport() bool
}

// ImageProvider generates images through the OpenAI images API. A provider
// adapter that doesn't implement it can't serve image models.
type ImageProvider interface {
Expand Down
Loading
Loading