Remove application credential substitution

This commit is contained in:
2026-11-16 03:37:29 +00:00
parent e34672f7fe
commit ffe0e3491e
37 changed files with 217 additions and 729 deletions

View File

@@ -43,7 +43,7 @@ func (f FileFailure) Error() string { return fmt.Sprintf("%s %q: %v", f.Kind, f.
func (f FileFailure) Unwrap() error { return f.Err }
// OutputSession owns checked destinations and per-file publication. Filesystem
// failures remain typed so CLI can present safe, redacted diagnostics.
// failures remain typed so CLI can present contextual diagnostics.
type OutputSession struct {
transcript string
reports [3]string
@@ -250,8 +250,8 @@ func (s *OutputSession) Initialize(initial ReportBundle, failed func([]FileFailu
}
// PrepareDebug creates optional initial diagnostics and returns any nonfatal problems.
func (s *OutputSession) PrepareDebug(workDir, runID string, metadata InvocationMetadata, effective []byte, secrets ...string) (*RunDirectory, []error) {
run, err := NewRunDirectory(workDir, runID, secrets...)
func (s *OutputSession) PrepareDebug(workDir, runID string, metadata InvocationMetadata, effective []byte) (*RunDirectory, []error) {
run, err := NewRunDirectory(workDir, runID)
if err != nil {
return nil, []error{err}
}

View File

@@ -2,13 +2,17 @@ package diagnostics
import (
"bytes"
"encoding/json"
"errors"
"io"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
)
func TestPrepareOutputAllowsInPlaceTranscriptButRejectsReportCollisions(t *testing.T) {
@@ -242,7 +246,7 @@ func TestDebugRunDirectoryUsesSuppliedIDAndIsRetained(t *testing.T) {
if _, err := os.Stat(parent); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("unexpected parent: %v", err)
}
run, err := NewRunDirectory(parent, "run-123", "secret")
run, err := NewRunDirectory(parent, "run-123")
if err != nil {
t.Fatal(err)
}
@@ -252,11 +256,11 @@ func TestDebugRunDirectoryUsesSuppliedIDAndIsRetained(t *testing.T) {
if _, err := NewRunDirectory(parent, "run-123"); err == nil {
t.Fatal("duplicate run ID accepted")
}
if err := run.WriteJSONArtifact(ArtifactCorrectionLedger, map[string]string{"proposal": "secret"}); err != nil {
if err := run.WriteJSONArtifact(ArtifactCorrectionLedger, map[string]string{"proposal": "edit"}); err != nil {
t.Fatal(err)
}
contents, err := os.ReadFile(filepath.Join(run.Path(), ArtifactCorrectionLedger))
if err != nil || strings.Contains(string(contents), "secret") {
if err != nil || string(contents) != "{\"proposal\":\"edit\"}\n" {
t.Fatalf("debug artifact: %s %v", contents, err)
}
if _, err := os.Stat(run.Path()); err != nil {
@@ -264,6 +268,48 @@ func TestDebugRunDirectoryUsesSuppliedIDAndIsRetained(t *testing.T) {
}
}
func TestDebugTranscriptSnapshotsPreserveSourceAndTypedViews(t *testing.T) {
run, err := NewRunDirectory(t.TempDir(), "run-transcripts")
if err != nil {
t.Fatal(err)
}
raw := []byte(" [\n {\"id\": 2, \"text\": \"literal\", \"speaker\": \"A\", \"start\": 0, \"end\": 1}\n ] ")
source, err := schema.ParseSourceTranscriptJSON(raw)
if err != nil {
t.Fatal(err)
}
if err := run.WriteSourceTranscript(source, raw); err != nil {
t.Fatal(err)
}
normalized := &schema.Transcript{Segments: []schema.Segment{{ID: 2, Text: "normalized"}}}
if err := run.WriteNormalizedTranscript(normalized); err != nil {
t.Fatal(err)
}
acquired, err := os.ReadFile(filepath.Join(run.Path(), ArtifactSourceTranscript))
if err != nil || !bytes.Equal(acquired, raw) {
t.Fatalf("acquired source = %q, error = %v", acquired, err)
}
for name, want := range map[string]string{ArtifactParsedSourceTranscript: "literal", ArtifactNormalizedTranscript: "normalized"} {
payload, err := os.ReadFile(filepath.Join(run.Path(), name))
if err != nil {
t.Fatal(err)
}
var transcript schema.Transcript
if err := json.Unmarshal(payload, &transcript); err != nil {
t.Fatal(err)
}
if len(transcript.Segments) != 1 || transcript.Segments[0].ID != 2 || transcript.Segments[0].Text != want {
t.Fatalf("%s: %s", name, payload)
}
}
if err := run.WriteJSONArtifact("invalid.json", math.NaN()); err == nil {
t.Fatal("expected serialization failure")
}
if _, err := os.Stat(filepath.Join(run.Path(), "invalid.json")); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("failed artifact persisted: %v", err)
}
}
func TestSelectionAndAbortPreserveTranscript(t *testing.T) {
dir := t.TempDir()
output := filepath.Join(dir, "output.json")
@@ -363,7 +409,7 @@ func TestOptionalDebugPreparationReportsProblems(t *testing.T) {
if run, problems := session.PrepareDebug(file, "run", InvocationMetadata{}, nil); run != nil || len(problems) == 0 {
t.Fatalf("run=%v problems=%v", run, problems)
}
run, problems := session.PrepareDebug(filepath.Join(dir, "debug"), "run", InvocationMetadata{Operation: "process"}, []byte("secret: sensitive\n"), "sensitive")
run, problems := session.PrepareDebug(filepath.Join(dir, "debug"), "run", InvocationMetadata{Operation: "process"}, []byte("version: 3\n"))
if len(problems) != 0 {
t.Fatal(problems)
}
@@ -371,8 +417,8 @@ func TestOptionalDebugPreparationReportsProblems(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(contents), "sensitive") {
t.Fatal("debug artifact contains secret")
if string(contents) != "version: 3\n" {
t.Fatalf("changed effective configuration: %q", contents)
}
}

View File

@@ -9,13 +9,11 @@ import (
"time"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/normalization"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
)
// RunDirectory represents a per-run diagnostics directory
type RunDirectory struct {
redactor redaction.Redactor
path string
createdAt time.Time
}
@@ -32,7 +30,7 @@ type InvocationMetadata struct {
}
// NewRunDirectory creates a new run directory under the configured work dir
func NewRunDirectory(workDir, runID string, secrets ...string) (*RunDirectory, error) {
func NewRunDirectory(workDir, runID string) (*RunDirectory, error) {
if strings.TrimSpace(workDir) == "" {
return nil, fmt.Errorf("debug work directory is required")
}
@@ -53,7 +51,6 @@ func NewRunDirectory(workDir, runID string, secrets ...string) (*RunDirectory, e
return &RunDirectory{
path: runPath,
redactor: redaction.New(secrets),
createdAt: time.Now().UTC(),
}, nil
}
@@ -77,7 +74,7 @@ func (r *RunDirectory) WriteInvocationMetadata(metadata InvocationMetadata) erro
}
path := filepath.Join(r.path, ArtifactInvocationMetadata)
bytes, err := r.redactor.JSON(metadata)
bytes, err := json.Marshal(metadata)
if err != nil {
return fmt.Errorf("failed to marshal invocation metadata: %w", err)
}
@@ -95,25 +92,14 @@ func (r *RunDirectory) WriteEffectiveConfig(effectiveYAML []byte) error {
// WriteSourceTranscript writes the source transcript artifact
func (r *RunDirectory) WriteSourceTranscript(transcript *schema.SourceTranscript, raw []byte) error {
// Preserve the source snapshot without copying unredacted input bytes.
var source any
decoder := json.NewDecoder(strings.NewReader(string(raw)))
decoder.UseNumber()
if err := decoder.Decode(&source); err != nil {
return fmt.Errorf("decode source snapshot: %w", err)
}
sourceBytes, err := r.redactor.JSON(source)
if err != nil {
return fmt.Errorf("redact source snapshot: %w", err)
}
sourcePath := filepath.Join(r.path, ArtifactSourceTranscript)
if err := os.WriteFile(sourcePath, append(sourceBytes, '\n'), 0o644); err != nil {
if err := os.WriteFile(sourcePath, raw, 0o644); err != nil {
return fmt.Errorf("failed to write source transcript: %w", err)
}
// Write parsed source for debugging
parsedPath := filepath.Join(r.path, ArtifactParsedSourceTranscript)
parsedBytes, err := r.redactor.JSON(transcript)
parsedBytes, err := json.Marshal(transcript)
if err != nil {
return fmt.Errorf("failed to marshal parsed source transcript: %w", err)
}
@@ -128,7 +114,7 @@ func (r *RunDirectory) WriteSourceTranscript(transcript *schema.SourceTranscript
// WriteNormalizedTranscript writes the normalized transcript artifact
func (r *RunDirectory) WriteNormalizedTranscript(transcript *schema.Transcript) error {
normalizedPath := filepath.Join(r.path, ArtifactNormalizedTranscript)
bytes, err := r.redactor.JSON(transcript)
bytes, err := json.Marshal(transcript)
if err != nil {
return fmt.Errorf("failed to serialize normalized transcript: %w", err)
}
@@ -141,7 +127,7 @@ func (r *RunDirectory) WriteNormalizedTranscript(transcript *schema.Transcript)
// WriteNormalizationSummary writes the normalization summary artifact
func (r *RunDirectory) WriteNormalizationSummary(summary *normalization.NormalizationSummary) error {
summaryPath := filepath.Join(r.path, ArtifactNormalizationSummary)
bytes, err := r.redactor.JSON(summary)
bytes, err := json.Marshal(summary)
if err != nil {
return fmt.Errorf("failed to marshal normalization summary: %w", err)
}
@@ -154,7 +140,7 @@ func (r *RunDirectory) WriteNormalizationSummary(summary *normalization.Normaliz
func (r *RunDirectory) WriteJSONArtifact(name string, payload any) error {
artifactPath := filepath.Join(r.path, name)
bytes, err := r.redactor.JSON(payload)
bytes, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal %s: %w", name, err)
}
@@ -165,13 +151,8 @@ func (r *RunDirectory) WriteJSONArtifact(name string, payload any) error {
return nil
}
// WriteYAMLArtifact writes a sanitized YAML diagnostic artifact.
// WriteYAMLArtifact writes the supplied YAML diagnostic artifact.
func (r *RunDirectory) WriteYAMLArtifact(name string, payload []byte) error {
var err error
payload, err = r.redactor.YAML(payload)
if err != nil {
return err
}
if err := os.WriteFile(filepath.Join(r.path, name), payload, 0o644); err != nil {
return fmt.Errorf("failed to write %s: %w", name, err)
}

View File

@@ -1,123 +0,0 @@
// Package redaction sanitizes diagnostic representations without modifying domain data.
package redaction
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
"gopkg.in/yaml.v3"
)
// Redactor replaces configured values in a single pass, longest matches first.
// Its zero value is usable and leaves strings unchanged.
type Redactor struct{ replacer *strings.Replacer }
func New(secrets []string) Redactor {
values := map[string]bool{}
for _, secret := range secrets {
if secret == "" {
continue
}
values[secret] = true
// Provider content may itself be serialized JSON inside a string field.
encoded, _ := json.Marshal(secret)
values[string(encoded[1:len(encoded)-1])] = true
}
keys := make([]string, 0, len(values))
for value := range values {
keys = append(keys, value)
}
sort.Slice(keys, func(i, j int) bool {
if len(keys[i]) == len(keys[j]) {
return keys[i] < keys[j]
}
return len(keys[i]) > len(keys[j])
})
pairs := make([]string, 0, 2*len(keys))
for _, key := range keys {
pairs = append(pairs, key, "[REDACTED]")
}
return Redactor{replacer: strings.NewReplacer(pairs...)}
}
func (r Redactor) String(value string) string {
if r.replacer == nil {
return value
}
return r.replacer.Replace(value)
}
// JSON serializes first, then redacts only decoded string values and keys.
// Numbers retain their exact representation. Failure never returns original data.
func (r Redactor) JSON(value any) ([]byte, error) {
raw, err := json.Marshal(value)
if err != nil {
return nil, fmt.Errorf("serialize diagnostic: %s", r.String(err.Error()))
}
decoder := json.NewDecoder(bytes.NewReader(raw))
decoder.UseNumber()
var decoded any
if err := decoder.Decode(&decoded); err != nil {
return nil, fmt.Errorf("decode diagnostic: %s", r.String(err.Error()))
}
safe, err := r.walk(decoded)
if err != nil {
return nil, err
}
return json.MarshalIndent(safe, "", " ")
}
func (r Redactor) walk(value any) (any, error) {
switch v := value.(type) {
case string:
return r.String(v), nil
case []any:
for i := range v {
var err error
v[i], err = r.walk(v[i])
if err != nil {
return nil, err
}
}
case map[string]any:
safe := make(map[string]any, len(v))
for key, item := range v {
safeKey := r.String(key)
if _, exists := safe[safeKey]; exists {
return nil, fmt.Errorf("redaction would produce duplicate object keys")
}
safeItem, err := r.walk(item)
if err != nil {
return nil, err
}
safe[safeKey] = safeItem
}
return safe, nil
}
return value, nil
}
// YAML preserves scalar types while sanitizing string nodes, including map keys.
func (r Redactor) YAML(raw []byte) ([]byte, error) {
var node yaml.Node
if err := yaml.Unmarshal(raw, &node); err != nil {
return nil, fmt.Errorf("decode diagnostic YAML: %s", r.String(err.Error()))
}
var visit func(*yaml.Node)
visit = func(n *yaml.Node) {
if n.Kind == yaml.ScalarNode && n.Tag == "!!str" {
n.Value = r.String(n.Value)
}
n.HeadComment = r.String(n.HeadComment)
n.LineComment = r.String(n.LineComment)
n.FootComment = r.String(n.FootComment)
for _, child := range n.Content {
visit(child)
}
}
visit(&node)
return yaml.Marshal(&node)
}

View File

@@ -1,81 +0,0 @@
package redaction_test
import (
"encoding/json"
"math"
"os"
"path/filepath"
"strings"
"testing"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
)
func TestStringRedactionPreservesSerializationAndDomainData(t *testing.T) {
secrets := []string{"2", `quoted"\key`, "overlap", "overlap-long", "REDACTED"}
r := redaction.New(secrets)
original := map[string]any{"number": 2, "large": json.Number("9007199254740993"), "bool": true, "text": `echo:2 quoted"\key overlap-long overlap`}
raw, err := r.JSON(original)
if err != nil {
t.Fatal(err)
}
var decoded map[string]any
if err := json.Unmarshal(raw, &decoded); err != nil {
t.Fatal(err)
}
if decoded["number"] != float64(2) || decoded["bool"] != true || !strings.Contains(string(raw), "9007199254740993") {
t.Fatalf("changed scalar meanings: %s", raw)
}
if decoded["text"] != "echo:[REDACTED] [REDACTED] [REDACTED] [REDACTED]" {
t.Fatalf("redaction: %s", raw)
}
if original["text"] != `echo:2 quoted"\key overlap-long overlap` {
t.Fatal("mutated domain data")
}
if raw, err := r.JSON(math.NaN()); err == nil || raw != nil {
t.Fatalf("unsafe fallback: %s %v", raw, err)
}
if raw, err := r.JSON(map[string]string{"overlap": "first", "overlap-long": "second"}); err == nil || raw != nil {
t.Fatalf("colliding redacted keys: %s %v", raw, err)
}
nested, _ := json.Marshal(map[string]string{"reason": `quoted"\key`})
raw, err = r.JSON(map[string]string{"final_content": string(nested)})
if err != nil || strings.Contains(string(raw), "key") {
t.Fatalf("nested content: %s %v", raw, err)
}
yaml, err := r.YAML([]byte("version: 2\nenabled: true\nmessage: echo:2\n"))
if err != nil || !strings.Contains(string(yaml), "version: 2") || !strings.Contains(string(yaml), "echo:[REDACTED]") {
t.Fatalf("YAML: %s %v", yaml, err)
}
}
func TestDebugWritersRedactSourceAndNormalizedSnapshots(t *testing.T) {
dir := t.TempDir()
run, err := diagnostics.NewRunDirectory(dir, "run-test", "2")
if err != nil {
t.Fatal(err)
}
if err := run.WriteSourceTranscript(&schema.SourceTranscript{}, []byte(`{"segments":[{"text":"echo:2","id":2}]}`)); err != nil {
t.Fatal(err)
}
if err := run.WriteNormalizedTranscript(&schema.Transcript{Segments: []schema.Segment{{ID: 2, Text: "echo:2"}}}); err != nil {
t.Fatal(err)
}
for _, path := range []string{filepath.Join(run.Path(), diagnostics.ArtifactSourceTranscript), filepath.Join(run.Path(), diagnostics.ArtifactNormalizedTranscript)} {
raw, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(raw), "echo:2") || !strings.Contains(string(raw), "echo:[REDACTED]") || !strings.Contains(string(raw), `"id": 2`) {
t.Fatalf("snapshot: %s", raw)
}
}
if err := run.WriteJSONArtifact("invalid.json", map[string]any{"secret": "echo:2", "invalid": math.NaN()}); err == nil {
t.Fatal("expected serialization failure")
}
if _, err := os.Stat(filepath.Join(run.Path(), "invalid.json")); !os.IsNotExist(err) {
t.Fatalf("failed artifact persisted: %v", err)
}
}

View File

@@ -5,8 +5,6 @@ import (
"html"
"sort"
"strings"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
)
// CompletionLine is the brief successful CLI summary. It uses the same counts
@@ -24,12 +22,10 @@ func count(value *int) int {
}
// Markdown renders a fixed-layout human report from one processing snapshot.
func Markdown(snapshot Snapshot, secrets ...string) []byte {
r := redaction.New(secrets)
safe := func(value string) string { return escapeMarkdown(r.String(value)) }
func Markdown(snapshot Snapshot) []byte {
var b strings.Builder
fmt.Fprintf(&b, "# Audita report: %s\n\n", safe(snapshot.RunID))
fmt.Fprintf(&b, "Status: %s\n\n", safe(snapshot.Status))
fmt.Fprintf(&b, "# Audita report: %s\n\n", escapeMarkdown(snapshot.RunID))
fmt.Fprintf(&b, "Status: %s\n\n", escapeMarkdown(snapshot.Status))
fmt.Fprintf(&b, "Corrections applied: %d \n", count(snapshot.Summary.CorrectionsApplied))
fmt.Fprintf(&b, "Work units: %s total; %s completed; %s interrupted; %s not started \n",
showCount(snapshot.Summary.WorkUnitsTotal), showCount(snapshot.Summary.WorkUnitsCompleted),
@@ -38,20 +34,20 @@ func Markdown(snapshot Snapshot, secrets ...string) []byte {
fmt.Fprintf(&b, "Recoveries: %s of %s model operations \n", showCount(snapshot.Summary.Recoveries), showCount(snapshot.Summary.LLMOperations))
fmt.Fprintf(&b, "Proposal responses: %s; with filtering: %s\n\n",
showCount(snapshot.Summary.ProposalResponses), showCount(snapshot.Summary.ProposalResponsesWithFiltering))
fmt.Fprintf(&b, "Transcript input: %s \n", safe(snapshot.TranscriptPath))
fmt.Fprintf(&b, "Transcript input: %s \n", escapeMarkdown(snapshot.TranscriptPath))
output := snapshot.OutputPath
if output == "" {
output = "stdout"
}
fmt.Fprintf(&b, "Transcript output: %s\n\n", safe(output))
fmt.Fprintf(&b, "Transcript output: %s\n\n", escapeMarkdown(output))
fmt.Fprintln(&b, "## Work-unit errors")
fmt.Fprintln(&b)
writeMatrix(&b, snapshot, r, false)
writeMatrix(&b, snapshot, false)
fmt.Fprintln(&b)
fmt.Fprintln(&b, "## Recoveries")
fmt.Fprintln(&b)
writeMatrix(&b, snapshot, r, true)
writeMatrix(&b, snapshot, true)
fmt.Fprintln(&b)
if len(snapshot.RepairOutcomes) > 0 {
fmt.Fprintln(&b, "## Correction repair")
@@ -59,14 +55,14 @@ func Markdown(snapshot Snapshot, secrets ...string) []byte {
fmt.Fprintln(&b, "| Stage | Chunk | Proposal index | Attempts | Outcome |")
fmt.Fprintln(&b, "| ---: | ---: | ---: | ---: | --- |")
for _, row := range snapshot.RepairOutcomes {
fmt.Fprintf(&b, "| %d | %d | %d | %d | %s |\n", row.StageIndex+1, row.ChunkIndex+1, row.ProposalIndex, row.Attempts, safe(row.Outcome))
fmt.Fprintf(&b, "| %d | %d | %d | %d | %s |\n", row.StageIndex+1, row.ChunkIndex+1, row.ProposalIndex, row.Attempts, escapeMarkdown(row.Outcome))
}
fmt.Fprintln(&b)
}
fmt.Fprintln(&b, "Legend: `—` = not started; `n*` = interrupted with n recorded errors or recoveries. Completed error cells are 0 or 1.")
fmt.Fprintln(&b)
writeReasons(&b, "Error reasons", errorReasons(snapshot.Errors), safe)
writeReasons(&b, "Recovery reasons", recoveryReasons(snapshot.Recoveries), safe)
writeReasons(&b, "Error reasons", errorReasons(snapshot.Errors))
writeReasons(&b, "Recovery reasons", recoveryReasons(snapshot.Recoveries))
fmt.Fprintln(&b, "A recovery can have multiple reasons; reason counts need not sum to the recovery total.")
fmt.Fprintln(&b)
fmt.Fprintln(&b, "Details: `errors.yml` and `recoveries.yml`.")
@@ -80,7 +76,7 @@ func showCount(value *int) string {
return fmt.Sprint(*value)
}
func writeMatrix(b *strings.Builder, snapshot Snapshot, redactor redaction.Redactor, recovery bool) {
func writeMatrix(b *strings.Builder, snapshot Snapshot, recovery bool) {
if len(snapshot.StageNames) == 0 || len(snapshot.WorkUnits) == 0 {
fmt.Fprintln(b, "No work units.")
return
@@ -88,7 +84,7 @@ func writeMatrix(b *strings.Builder, snapshot Snapshot, redactor redaction.Redac
columns := snapshot.StageNames
fmt.Fprint(b, "| Chunk |")
for _, name := range columns {
fmt.Fprintf(b, " %s |", stageCellEscaper.Replace(html.EscapeString(redactor.String(name))))
fmt.Fprintf(b, " %s |", stageCellEscaper.Replace(html.EscapeString(name)))
}
fmt.Fprint(b, "\n| --- |")
for range columns {
@@ -137,7 +133,7 @@ var stageCellEscaper = strings.NewReplacer(
"://", "&#58;//", "www.", "www&#46;", "@", "&#64;",
)
func writeReasons(b *strings.Builder, title string, counts map[string]int, safe func(string) string) {
func writeReasons(b *strings.Builder, title string, counts map[string]int) {
fmt.Fprintf(b, "### %s\n\n", title)
if len(counts) == 0 {
fmt.Fprintln(b, "None.")
@@ -150,7 +146,7 @@ func writeReasons(b *strings.Builder, title string, counts map[string]int, safe
}
sort.Strings(keys)
for _, key := range keys {
fmt.Fprintf(b, "- `%s`: %d\n", safe(key), counts[key])
fmt.Fprintf(b, "- `%s`: %d\n", escapeMarkdown(key), counts[key])
}
fmt.Fprintln(b)
}

View File

@@ -28,7 +28,7 @@ func TestMarkdownFixedLayoutAndCounts(t *testing.T) {
Errors: []ErrorRecord{{Scope: "work_unit", Failures: []Failure{{Reason: "transport_failed"}, {Reason: "transport_failed"}}}},
Recoveries: []RecoveryRecord{{Reasons: []string{"output_repaired", "proposals_filtered"}}, {Reasons: []string{"output_repaired"}}, {Reasons: []string{"service_recovered"}}},
}
got := Markdown(snapshot, "secret")
got := Markdown(snapshot)
text := string(got)
// Compare only complete table rows, preserving order and states while leaving
// prose and spacing outside the fixed matrices free to change.
@@ -45,7 +45,7 @@ func TestMarkdownFixedLayoutAndCounts(t *testing.T) {
if rows := markdownTableRows(text); !reflect.DeepEqual(rows, wantRows) {
t.Fatalf("matrices: got %v; want %v", rows, wantRows)
}
for _, fragment := range []string{"in\\|put\\`[REDACTED]\\`.json", "out<br>file.json", "`transport_failed`: 1", "`output_repaired`: 2", "`proposals_filtered`: 1", "`service_recovered`: 1"} {
for _, fragment := range []string{"in\\|put\\`secret\\`.json", "out<br>file.json", "`transport_failed`: 1", "`output_repaired`: 2", "`proposals_filtered`: 1", "`service_recovered`: 1"} {
if !strings.Contains(text, fragment) {
t.Fatalf("missing %q in:\n%s", fragment, text)
}
@@ -69,9 +69,6 @@ func TestMarkdownFixedLayoutAndCounts(t *testing.T) {
t.Fatalf("%s summary: got %v; want %v", label, got, want)
}
}
if strings.Contains(text, "secret") {
t.Fatal("secret leaked")
}
var decoded struct {
Summary Summary `yaml:"summary"`
}
@@ -88,12 +85,12 @@ func TestMarkdownFixedLayoutAndCounts(t *testing.T) {
}
func TestMarkdownNoWorkAndEscapedStageLabel(t *testing.T) {
text := string(Markdown(Snapshot{RunID: "r", StageNames: []string{"bad|label`\nsecret"}}, "secret"))
text := string(Markdown(Snapshot{RunID: "r", StageNames: []string{"bad|label`\nsecret"}}))
if len(markdownTableRows(text)) != 0 || strings.Contains(text, "secret") {
t.Fatal(text)
}
text = string(Markdown(Snapshot{RunID: "r", StageNames: []string{"bad|label`\nsecret"}, WorkUnits: []WorkUnit{{StageIndex: 0, ChunkIndex: 0, State: "completed"}}}, "secret"))
if !strings.Contains(text, "bad\\|label\\`<br>\\[REDACTED\\]") {
text = string(Markdown(Snapshot{RunID: "r", StageNames: []string{"bad|label`\nsecret"}, WorkUnits: []WorkUnit{{StageIndex: 0, ChunkIndex: 0, State: "completed"}}}))
if !strings.Contains(text, "bad\\|label\\`<br>secret") {
t.Fatal(text)
}
}

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"time"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/redaction"
"gopkg.in/yaml.v3"
)
@@ -178,17 +177,13 @@ func completionTime(t *time.Time) *string {
return &value
}
// YAML redacts only string nodes after typed serialization.
func YAML(document any, secrets ...string) ([]byte, error) {
// YAML encodes the report document.
func YAML(document any) ([]byte, error) {
raw, err := yaml.Marshal(document)
if err != nil {
return nil, fmt.Errorf("encode report YAML: %w", err)
}
safe, err := redaction.New(secrets).YAML(raw)
if err != nil {
return nil, fmt.Errorf("redact report YAML: %w", err)
}
return safe, nil
return raw, nil
}
// StageLabel identifies a configured entry by position, module, and optional ID.

View File

@@ -8,7 +8,7 @@ import (
"gopkg.in/yaml.v3"
)
func TestYAMLRedactsStringsWithoutChangingScalarTypes(t *testing.T) {
func TestYAMLPreservesScalarTypes(t *testing.T) {
zero, one := 0, 1
falseValue := false
snapshot := Snapshot{RunID: "secret\\\"key", Status: StatusCompleted, StartedAt: time.Date(2026, 9, 24, 0, 0, 0, 0, time.UTC),
@@ -16,18 +16,15 @@ func TestYAMLRedactsStringsWithoutChangingScalarTypes(t *testing.T) {
Errors: []ErrorRecord{{Scope: "run", Phase: "processing", Reason: "internal_failure", Message: "secret\\\"key 1"}},
Recoveries: []RecoveryRecord{{StageIndex: 0, Reasons: []string{"service_recovered"}, ExecutionCounts: ExecutionCounts{RetriesExhausted: &falseValue}}}}
for _, document := range []any{snapshot.ErrorsDocument(), snapshot.RecoveriesDocument()} {
payload, err := YAML(document, `secret\"key`, "1")
payload, err := YAML(document)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(payload), `secret\"key`) {
t.Fatalf("secret leaked: %s", payload)
}
var decoded map[string]any
if err := yaml.Unmarshal(payload, &decoded); err != nil {
t.Fatal(err)
}
if decoded["version"] != 1 || decoded["started_at"] != "2026-09-24T00:00:00Z" {
if decoded["run_id"] != snapshot.RunID || decoded["version"] != 1 || decoded["started_at"] != "2026-09-24T00:00:00Z" {
t.Fatalf("wrong envelope types: %#v", decoded)
}
summary := decoded["summary"].(map[string]any)
@@ -35,7 +32,7 @@ func TestYAMLRedactsStringsWithoutChangingScalarTypes(t *testing.T) {
t.Fatalf("changed count types: %#v", summary)
}
}
if payload, err := YAML(snapshot.RecoveriesDocument(), "1"); err != nil || !strings.Contains(string(payload), "retries_exhausted: false") {
if payload, err := YAML(snapshot.RecoveriesDocument()); err != nil || !strings.Contains(string(payload), "retries_exhausted: false") {
t.Fatalf("boolean changed: %s %v", payload, err)
}
}