Files
archie/internal/platform/auditjson/auditjson.go
T

143 lines
4.2 KiB
Go

// Package auditjson stores the exact request and response of a call for
// audit: the message as JSON, with credentials removed and an oversized body
// capped rather than dropped silently.
package auditjson
import (
"crypto/sha256"
"encoding/hex"
"encoding/json/jsontext"
json "encoding/json/v2"
"strings"
"unicode/utf8"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
// MaxBytes is the most of one message that is stored. A larger message keeps
// its first MaxBytes, with its full length and SHA-256.
const MaxBytes = 1 << 20
// redacted replaces a credential.
const redacted = "[redacted]"
// Payload is one stored message. JSON is the message itself when it fits under
// MaxBytes. A message that doesn't fit keeps its first MaxBytes as Text, and
// Length and SHA256 describe the whole of it.
type Payload struct {
JSON jsontext.Value `json:"json,omitzero"`
Text string `json:"text,omitzero"`
Truncated bool `json:"truncated,omitzero"`
Length int64 `json:"length,omitzero"`
SHA256 string `json:"sha256,omitzero"`
}
// FromProto renders a protobuf message as protojson, with credentials removed
// and the result capped. The message is what core sent or received on the
// wire, never anything a connector resolved internally.
func FromProto(msg proto.Message) Payload {
raw, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(msg)
if err != nil {
return Payload{Text: "unrenderable: " + err.Error()}
}
return FromJSON(raw)
}
// FromValue encodes v as JSON, with credentials removed and the result capped.
func FromValue(v any) Payload {
raw, err := json.Marshal(v)
if err != nil {
return Payload{Text: "unrenderable: " + err.Error()}
}
return FromJSON(raw)
}
// FromJSON redacts credentials in raw and caps the result.
func FromJSON(raw jsontext.Value) Payload {
clean := Redact(raw)
if len(clean) <= MaxBytes {
return Payload{JSON: clean}
}
sum := sha256.Sum256(clean)
kept := MaxBytes
for kept > 0 && !utf8.RuneStart(clean[kept]) {
kept--
}
return Payload{
Text: string(clean[:kept]), Truncated: true,
Length: int64(len(clean)), SHA256: hex.EncodeToString(sum[:]),
}
}
// Redact replaces every credential in a JSON value with a marker. A key that
// names a credential is redacted whole; a value that reads as an authorisation
// header is redacted wherever it appears. A payload that can't be decoded is
// replaced rather than stored unchanged, because its credentials can't be
// found.
func Redact(raw jsontext.Value) jsontext.Value {
var v any
if err := json.Unmarshal(raw, &v); err != nil {
return jsontext.Value(`"[unreadable payload]"`)
}
redactValue(v)
out, err := json.Marshal(v)
if err != nil {
return jsontext.Value(`"[unreadable payload]"`)
}
return out
}
func redactValue(v any) {
switch x := v.(type) {
case map[string]any:
for key, value := range x {
if sensitiveKey(key) {
x[key] = redacted
continue
}
if s, ok := value.(string); ok && looksSecret(s) {
x[key] = redacted
continue
}
redactValue(value)
}
case []any:
for _, value := range x {
redactValue(value)
}
}
}
// sensitiveKey reports whether a JSON key names a credential. The match is
// deliberately broad: an audit that keeps a credential is worse than one that
// hides a harmless field.
func sensitiveKey(key string) bool {
k := strings.ToLower(key)
switch {
case strings.Contains(k, "authorization"),
strings.Contains(k, "cookie"),
strings.Contains(k, "password"),
strings.Contains(k, "passwd"),
strings.Contains(k, "secret"),
strings.Contains(k, "privatekey"),
strings.Contains(k, "private_key"),
strings.Contains(k, "credential"),
strings.Contains(k, "apikey"),
strings.Contains(k, "api_key"),
strings.Contains(k, "api-key"):
return true
}
compact := strings.NewReplacer("_", "", "-", "").Replace(k)
return compact == "token" || strings.HasSuffix(compact, "token")
}
// looksSecret reports whether a value is an authorisation header, whatever
// key it arrived under.
func looksSecret(value string) bool {
s := strings.TrimSpace(value)
lower := strings.ToLower(s)
return strings.HasPrefix(lower, "bearer ") || strings.HasPrefix(lower, "basic ") ||
strings.Contains(s, "-----BEGIN ")
}