Resolve, target facts, invoke, lookup, deliver and parse record the protobuf wire messages as JSON on the context recorder the owning module supplies. Credentials are redacted and a body over 1 MiB keeps its prefix, full length and SHA-256. Judgement stores the resolve and target facts in the decision body and appends the executed invoke to it.
81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package auditjson
|
|
|
|
import (
|
|
"encoding/json/jsontext"
|
|
json "encoding/json/v2"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestStoredPayloadKeepsCredentialsOut guards the audit rule that a stored
|
|
// request or response never carries a credential, wherever it sits in the
|
|
// message and however the key is spelled.
|
|
func TestStoredPayloadKeepsCredentialsOut(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
secret string
|
|
// kept is text that must survive redaction, so a payload isn't
|
|
// blanked wholesale.
|
|
kept string
|
|
}{
|
|
{
|
|
name: "authorization header",
|
|
in: `{"headers":{"Authorization":"Bearer sk-live-123","Content-Type":"application/json"},"body":"ok"}`,
|
|
secret: "sk-live-123",
|
|
kept: "application/json",
|
|
},
|
|
{
|
|
name: "api key field",
|
|
in: `{"input":{"api_key":"key-abc","repository":"archie"}}`,
|
|
secret: "key-abc",
|
|
kept: "archie",
|
|
},
|
|
{
|
|
name: "nested password",
|
|
in: `{"request":{"password":"hunter2"},"target":"db"}`,
|
|
secret: "hunter2",
|
|
kept: "db",
|
|
},
|
|
{
|
|
name: "bearer value under an innocuous key",
|
|
in: `{"headers":{"X-Custom":"Bearer opaque-456"},"event":"push"}`,
|
|
secret: "opaque-456",
|
|
kept: "push",
|
|
},
|
|
{
|
|
name: "token field",
|
|
in: `{"access_token":"tok-789","scope":"read"}`,
|
|
secret: "tok-789",
|
|
kept: "read",
|
|
},
|
|
{
|
|
name: "camel case token field",
|
|
in: `{"accessToken":"camel-123","scope":"write"}`,
|
|
secret: "camel-123",
|
|
kept: "write",
|
|
},
|
|
{
|
|
name: "harmless fields survive",
|
|
in: `{"prompt_tokens":5,"idempotency_key":"k1","total_tokens":9}`,
|
|
secret: "",
|
|
kept: "idempotency_key",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
p := FromJSON(jsontext.Value(tt.in))
|
|
out, err := json.Marshal(p)
|
|
if err != nil {
|
|
t.Fatalf("marshal stored payload: %v", err)
|
|
}
|
|
if tt.secret != "" && strings.Contains(string(out), tt.secret) {
|
|
t.Fatalf("stored payload contains the credential %q: %s", tt.secret, out)
|
|
}
|
|
if !strings.Contains(string(out), tt.kept) {
|
|
t.Fatalf("stored payload dropped %q: %s", tt.kept, out)
|
|
}
|
|
})
|
|
}
|
|
}
|