mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Every org writes a record on every call, and the Record filter stored each model request's body in it, redacted only of credentials: every customer's prompts, kept whether or not their org had asked for them to be, and handed on to the record commit. A request to a model surface is now recorded as the sha256 and size of its body. The trail still says that a model was called and with which body; what the caller said is kept only where their org asked for it. Claude-Session: https://claude.ai/code/session_013cvpQYtJPnTNfWzbqwtBJZ
127 lines
4.3 KiB
Go
127 lines
4.3 KiB
Go
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package object
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/printer"
|
|
"go/token"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Saving a provider carries the upstream key in the request body on its way to
|
|
// being sealed. A record of that request keeps a copy, and a copy in a row is a
|
|
// copy every reader of the table can have — so the body is redacted before it is
|
|
// stored rather than on the way out.
|
|
func TestARecordedBodyKeepsNoCredential(t *testing.T) {
|
|
for _, body := range []string{
|
|
`{"name":"openrouter","clientSecret":"sk-or-v1-REALKEY"}`,
|
|
`{"api_key":"sk-proj-REALKEY"}`,
|
|
`{"token":"hf_REALKEY"}`,
|
|
`{"password":"correct-horse"}`,
|
|
} {
|
|
got := RedactBody(body)
|
|
for _, secret := range []string{"sk-or-v1-REALKEY", "sk-proj-REALKEY", "hf_REALKEY", "correct-horse"} {
|
|
if strings.Contains(got, secret) {
|
|
t.Fatalf("a credential survived into the record: %s", got)
|
|
}
|
|
}
|
|
if !strings.Contains(got, "redacted") {
|
|
t.Fatalf("nothing was redacted in %q -> %q", body, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The paired control: a body carrying no credential is stored as it was, so the
|
|
// record still answers what the request actually said.
|
|
func TestAnOrdinaryBodyIsKeptWhole(t *testing.T) {
|
|
const body = `{"model":"zen5","messages":[{"role":"user","content":"hello"}]}`
|
|
if got := RedactBody(body); got != body {
|
|
t.Fatalf("an ordinary body was altered:\n got %s\nwant %s", got, body)
|
|
}
|
|
}
|
|
|
|
// The redactor only helps if the write calls it. A test that exercises
|
|
// RedactBody alone passes just as well when NewRecord stores the body raw —
|
|
// which is exactly how the raw store survived having a redactor in the tree.
|
|
func TestTheRecordWriteRedactsWhatItStores(t *testing.T) {
|
|
fs := token.NewFileSet()
|
|
f, err := parser.ParseFile(fs, "record.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var fn *ast.FuncDecl
|
|
ast.Inspect(f, func(n ast.Node) bool {
|
|
if d, ok := n.(*ast.FuncDecl); ok && d.Name.Name == "NewRecord" {
|
|
fn = d
|
|
}
|
|
return true
|
|
})
|
|
if fn == nil {
|
|
t.Fatal("NewRecord not found — this test has stopped covering the write")
|
|
}
|
|
var body strings.Builder
|
|
if err := printer.Fprint(&body, fs, fn); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src := body.String()
|
|
if !strings.Contains(src, "ctx.Body()") {
|
|
t.Fatal("NewRecord no longer reads the body — delete this test or repoint it")
|
|
}
|
|
if !strings.Contains(src, "RedactBody(") {
|
|
t.Fatal("NewRecord stores the request body without redacting it")
|
|
}
|
|
}
|
|
|
|
// A prompt never reaches the audit trail: every org writes a record on every call,
|
|
// and a customer's words are kept only where their org asked for them to be.
|
|
func TestAPromptIsRecordedAsItsDigestOnly(t *testing.T) {
|
|
for _, path := range []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/embeddings", "/v1/chat"} {
|
|
if !promptBearing(path) {
|
|
t.Errorf("%s is not treated as a prompt", path)
|
|
}
|
|
}
|
|
for _, path := range []string{"/v1/chats", "/v1/ai/providers", "/v1/ai/router/policy"} {
|
|
if promptBearing(path) {
|
|
t.Errorf("%s is treated as a prompt", path)
|
|
}
|
|
}
|
|
got := bodyDigest([]byte(`{"messages":[{"role":"user","content":"my secret plan"}]}`))
|
|
if strings.Contains(got, "secret") || !strings.Contains(got, `"sha256":"`) {
|
|
t.Fatalf("digest = %s", got)
|
|
}
|
|
}
|
|
|
|
// The rule only helps if the write calls it, for the reason the redaction test gives.
|
|
func TestTheRecordWriteKeepsOnlyAPromptsDigest(t *testing.T) {
|
|
fs := token.NewFileSet()
|
|
f, err := parser.ParseFile(fs, "record.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var src strings.Builder
|
|
ast.Inspect(f, func(n ast.Node) bool {
|
|
if d, ok := n.(*ast.FuncDecl); ok && d.Name.Name == "NewRecord" {
|
|
_ = printer.Fprint(&src, fs, d)
|
|
}
|
|
return true
|
|
})
|
|
if !strings.Contains(src.String(), "promptBearing(") || !strings.Contains(src.String(), "bodyDigest(") {
|
|
t.Fatal("NewRecord stores a prompt's body")
|
|
}
|
|
}
|