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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ 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 whatever the key's PII mode (masking sends the
prompt to the analyzer, and the provider can't read it anyway), and never falls
back to a provider without that verification. The code the Tinfoil enclave
must run is pinned in this binary (`tinfoilsh/confidential-model-router`):
`TINFOIL_CONFIG_REPO` is operator configuration outside the measurements, so
a different value refuses these requests rather than trusting it. 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,31 @@
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) {
// Tinfoil's claim depends on which repo it verifies; see the tinfoil
// package. This test is about which adapters can make it at all.
t.Setenv("TINFOIL_CONFIG_REPO", "")
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)
}
}
}
53 changes: 49 additions & 4 deletions apps/gateway/internal/adapters/providers/tinfoil/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,41 @@ func NewAdapterWithFactory(timeout time.Duration, factory VerifiedClientFactory,
return &Adapter{timeout: timeout, factory: factory, logger: l}
}

// AttestedConfigRepo is the only code a Tinfoil enclave may run to serve
// attested inference: Tinfoil's model router, as its GitHub release workflow
// signs it. It is compiled in, so it is covered by this enclave's
// measurements. TINFOIL_CONFIG_REPO is operator configuration outside them,
// and a repo chosen there could be code that reads the prompt.
const AttestedConfigRepo = "tinfoilsh/confidential-model-router"

// 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. It is
// false when the SDK will verify against another repo, so attested inference
// is refused before any credit is held; newVerifiedClient checks the repo the
// client actually verified.
func (a *Adapter) AttestedTransport() bool {
return effectiveConfigRepo() == AttestedConfigRepo
}

func configuredRepo() string { return strings.TrimSpace(os.Getenv("TINFOIL_CONFIG_REPO")) }
func configuredEnclave() string { return strings.TrimSpace(os.Getenv("TINFOIL_ENCLAVE_HOST")) }
func configuredBundleURL() string {
return strings.TrimRight(strings.TrimSpace(os.Getenv("TINFOIL_ATTESTATION_BUNDLE_URL")), "/")
}

// effectiveConfigRepo is the repo SDKClientFactory's client verifies against.
// tinfoil-go uses TINFOIL_CONFIG_REPO only with an enclave host or an
// attestation bundle URL; otherwise it picks a router itself and verifies
// Tinfoil's default repo.
func effectiveConfigRepo() string {
repo := configuredRepo()
if repo == "" || (configuredEnclave() == "" && configuredBundleURL() == "") {
return AttestedConfigRepo
}
return repo
}

func missingProviderCredentialError() *domain.GatewayError {
return domain.ErrInternal("an internal error occurred").WithMeta(
"provider", providerName,
Expand All @@ -78,6 +113,16 @@ func (a *Adapter) newVerifiedClient(ctx context.Context, model domain.PublicMode
if verified == nil || verified.HTTPClient() == nil || verified.GroundTruth() == nil {
return nil, nil, domain.ErrProviderUnavailable(providerName).WithMeta("verification_error", "verified client returned no ground truth")
}
// Attested inference sends the prompt only to the pinned code. Nothing
// has been sent yet. AttestedTransport already refused a configured
// repo, so a mismatch here is the SDK verifying something else: Nexus's
// fault, not the request's.
if domain.AttestedInferenceRequired(ctx) && verified.Repo() != AttestedConfigRepo {
return nil, nil, domain.ErrProviderUnavailable(providerName).WithMeta(
"fault", domain.FaultNexus,
"reason", "Tinfoil enclave verified against an unpinned repository",
)
}
return verified, proofFromVerifiedClient(verified), nil
}

Expand Down Expand Up @@ -208,10 +253,10 @@ func (SDKClientFactory) NewVerifiedClient(_ context.Context, model domain.Public
opts := []tinfoilsdk.ClientOption{
tinfoilsdk.WithTransport(tinfoilsdk.TransportEHBP),
}
if repo := strings.TrimSpace(os.Getenv("TINFOIL_CONFIG_REPO")); repo != "" {
if repo := configuredRepo(); repo != "" {
opts = append(opts, tinfoilsdk.WithRepo(repo))
}
if enclave := strings.TrimSpace(os.Getenv("TINFOIL_ENCLAVE_HOST")); enclave != "" {
if enclave := configuredEnclave(); enclave != "" {
opts = append(opts, tinfoilsdk.WithEnclave(enclave))
}
// ProviderConfig.BaseURL is the OpenAI-compatible catalog URL. Do not treat
Expand All @@ -221,8 +266,8 @@ func (SDKClientFactory) NewVerifiedClient(_ context.Context, model domain.Public
if proxyBaseURL := configuredProxyBaseURL(); proxyBaseURL != "" {
opts = append(opts, tinfoilsdk.WithBaseURL(proxyBaseURL))
}
if bundleURL := strings.TrimSpace(os.Getenv("TINFOIL_ATTESTATION_BUNDLE_URL")); bundleURL != "" {
opts = append(opts, tinfoilsdk.WithAttestationBundleURL(strings.TrimRight(bundleURL, "/")))
if bundleURL := configuredBundleURL(); bundleURL != "" {
opts = append(opts, tinfoilsdk.WithAttestationBundleURL(bundleURL))
}

client, err := tinfoilsdk.NewClientWithOptions(opts...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func (f *fakeFactory) NewVerifiedClient(context.Context, domain.PublicModel) (Ve
type fakeVerifiedClient struct {
httpClient *http.Client
groundTruth *verifierclient.GroundTruth
// repo is the repository the client verified against; empty is Tinfoil's.
repo string
}

func fakeClient(httpClient *http.Client) *fakeVerifiedClient {
Expand All @@ -239,6 +241,9 @@ func (c *fakeVerifiedClient) Enclave() string {
}

func (c *fakeVerifiedClient) Repo() string {
if c.repo != "" {
return c.repo
}
return "tinfoilsh/confidential-model-router"
}

Expand Down
111 changes: 111 additions & 0 deletions apps/gateway/internal/adapters/providers/tinfoil/attested_repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package tinfoil

import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/dappnode/dappnode-nexus-gateway/pkg/domain"
)

// Attested inference trusts the Tinfoil enclave for the code it runs, so that
// code is pinned in this binary, not taken from TINFOIL_CONFIG_REPO.

func TestAttestedTransportOnlyForThePinnedRepository(t *testing.T) {
for _, tc := range []struct {
repo, enclave, bundle string
want bool
}{
{"", "inference.tinfoil.sh", "", true},
{AttestedConfigRepo, "inference.tinfoil.sh", "", true},
{" " + AttestedConfigRepo + " ", "inference.tinfoil.sh", "", true},
{"nexus/leaky-router", "leaky.example", "", false},
{"tinfoilsh/confidential-model-router-dev", "inference.tinfoil.sh", "", false},
{"nexus/leaky-router", "", "https://bundle.example", false},
// Without an enclave host or bundle URL the SDK ignores the repo and
// verifies Tinfoil's default, so the request is safe to serve.
{"nexus/leaky-router", "", "", true},
} {
t.Setenv("TINFOIL_CONFIG_REPO", tc.repo)
t.Setenv("TINFOIL_ENCLAVE_HOST", tc.enclave)
t.Setenv("TINFOIL_ATTESTATION_BUNDLE_URL", tc.bundle)
if got := NewAdapter(time.Second).AttestedTransport(); got != tc.want {
t.Errorf("repo=%q enclave=%q bundle=%q: AttestedTransport() = %v, want %v", tc.repo, tc.enclave, tc.bundle, got, tc.want)
}
}
}

// providerServer counts the requests that reach the provider.
func providerServer(t *testing.T) (*httptest.Server, *int) {
t.Helper()
sent := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
sent++
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"id":"cmpl-1","choices":[{"message":{"role":"assistant","content":"hi"}}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`)
}))
t.Cleanup(server.Close)
t.Setenv("TINFOIL_CONFIG_REPO", "")
t.Setenv("TINFOIL_API_KEY", "test-key")
t.Setenv("TINFOIL_PROXY_BASE_URL", server.URL)
return server, &sent
}

func TestAttestedInferenceRefusesAnEnclaveVerifiedAgainstAnotherRepository(t *testing.T) {
server, sent := providerServer(t)
client := fakeClient(server.Client())
client.repo = "nexus/leaky-router"
adapter := NewAdapterWithFactory(time.Second, &fakeFactory{client: client})
ctx := domain.WithAttestedInference(context.Background())

_, _, err := adapter.Complete(ctx, simpleRequest(), simpleModel(server.URL))
assertUnpinnedRepositoryRefused(t, err)
_, err = adapter.Stream(ctx, simpleRequest(), simpleModel(server.URL))
assertUnpinnedRepositoryRefused(t, err)
if *sent != 0 {
t.Fatalf("the prompt reached the provider %d times", *sent)
}
}

func TestAttestedInferenceSendsToThePinnedRepository(t *testing.T) {
server, sent := providerServer(t)
adapter := NewAdapterWithFactory(time.Second, &fakeFactory{client: fakeClient(server.Client())})

_, proof, err := adapter.Complete(domain.WithAttestedInference(context.Background()), simpleRequest(), simpleModel(server.URL))
if err != nil {
t.Fatalf("Complete returned error: %v", err)
}
if *sent != 1 || proof == nil {
t.Fatalf("sent = %d, proof = %v; want one request with a proof", *sent, proof)
}
}

// Requests that don't ask for attested inference keep today's behaviour,
// whatever repository the operator configured.
func TestOrdinaryRequestsIgnoreTheRepositoryPin(t *testing.T) {
server, sent := providerServer(t)
client := fakeClient(server.Client())
client.repo = "tinfoilsh/another-router"

if _, _, err := NewAdapterWithFactory(time.Second, &fakeFactory{client: client}).Complete(context.Background(), simpleRequest(), simpleModel(server.URL)); err != nil {
t.Fatalf("Complete returned error: %v", err)
}
if *sent != 1 {
t.Fatalf("sent = %d, want 1", *sent)
}
}

func assertUnpinnedRepositoryRefused(t *testing.T, err error) {
t.Helper()
var gwErr *domain.GatewayError
if !errors.As(err, &gwErr) || gwErr.Code != domain.ErrCodeProviderUnavailable {
t.Fatalf("want %s, got %T %[2]v", domain.ErrCodeProviderUnavailable, err)
}
if domain.FaultOf(err) != domain.FaultNexus {
t.Fatalf("fault = %q, want %q: a misconfigured repository is Nexus's fault", domain.FaultOf(err), domain.FaultNexus)
}
}
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