Validate byte-preserving record fields as base64 strings

This commit is contained in:
2026-11-03 00:18:47 +00:00
parent 2a7bed87db
commit e0019abe31
5 changed files with 83 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package publish
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
@@ -14,6 +15,50 @@ import (
"testing"
)
func TestLoadEvidenceValidatesKnownByteFieldsAsBase64Strings(t *testing.T) {
for _, test := range []struct{ name, value, want, failure string }{
{"valid", `"djEuMi4z"`, "v1.2.3", ""},
{"empty", `""`, "", ""},
{"malformed base64", `"not base64!"`, "", "base64"},
{"numeric array", `[118,49,46,50,46,51]`, "", "type"},
{"object", `{}`, "", "type"},
{"boolean", `true`, "", "type"},
{"number", `123`, "", "type"},
{"null", `null`, "", "schema"},
} {
t.Run(test.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "record.json")
contents := `{"format_version":2,"evidence":{},"evidence_bytes":{"tag":` + test.value + `}}`
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
t.Fatal(err)
}
loaded, err := LoadEvidence(path)
if test.failure == "" {
if err != nil || loaded.Tag != test.want {
t.Fatalf("byte string = %#v, %v", loaded, err)
}
return
}
var typed *RecoveryError
if !errors.As(err, &typed) || typed.Kind != "record_format" || typed.Unwrap() == nil {
t.Fatalf("byte format failure = %v", err)
}
switch test.failure {
case "type":
var cause *json.UnmarshalTypeError
if !errors.As(err, &cause) {
t.Fatalf("lost JSON type cause: %v", err)
}
case "base64":
var cause base64.CorruptInputError
if !errors.As(err, &cause) {
t.Fatalf("lost base64 cause: %v", err)
}
}
})
}
}
func TestRecordsRoundTripNonUTF8Evidence(t *testing.T) {
repo := recordRepo(t)
raw := "path-\xff"