|
| 1 | +package output |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/trufflesecurity/trufflehog/v3/pkg/context" |
| 13 | + "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" |
| 14 | +) |
| 15 | + |
| 16 | +// MarkdownPrinter renders TruffleHog findings into a Markdown document with |
| 17 | +// dedicated tables for verified and unverified secrets. It buffers the rows |
| 18 | +// while scanning and flushes the final report when Close is invoked. |
| 19 | +type MarkdownPrinter struct { |
| 20 | + mu sync.Mutex |
| 21 | + out io.Writer |
| 22 | + verified []markdownRow |
| 23 | + unverified []markdownRow |
| 24 | +} |
| 25 | + |
| 26 | +// markdownRow represents a single table entry in the Markdown report. |
| 27 | +type markdownRow struct { |
| 28 | + Detector string |
| 29 | + File string |
| 30 | + Line string |
| 31 | + Redacted string |
| 32 | +} |
| 33 | + |
| 34 | +// NewMarkdownPrinter builds a MarkdownPrinter that writes to out. When out is |
| 35 | +// nil, stdout is used. |
| 36 | +func NewMarkdownPrinter(out io.Writer) *MarkdownPrinter { |
| 37 | + if out == nil { |
| 38 | + out = os.Stdout |
| 39 | + } |
| 40 | + return &MarkdownPrinter{out: out} |
| 41 | +} |
| 42 | + |
| 43 | +// Print collects each result so the final Markdown doc can include per-section |
| 44 | +// counts and tables before the buffered results are rendered in Close. |
| 45 | +func (p *MarkdownPrinter) Print(_ context.Context, r *detectors.ResultWithMetadata) error { |
| 46 | + file := "n/a" |
| 47 | + line := "n/a" |
| 48 | + |
| 49 | + meta, err := structToMap(r.SourceMetadata.Data) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("could not marshal result: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + for _, data := range meta { |
| 55 | + for k, v := range data { |
| 56 | + if k == "line" { |
| 57 | + if l, ok := v.(float64); ok { |
| 58 | + line = fmt.Sprintf("%d", int64(l)) |
| 59 | + } |
| 60 | + } |
| 61 | + if k == "file" { |
| 62 | + if filename, ok := v.(string); ok { |
| 63 | + file = filename |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + row := markdownRow{ |
| 70 | + Detector: r.DetectorType.String(), |
| 71 | + File: file, |
| 72 | + Line: line, |
| 73 | + Redacted: sanitize(r.Redacted), |
| 74 | + } |
| 75 | + |
| 76 | + p.mu.Lock() |
| 77 | + defer p.mu.Unlock() |
| 78 | + |
| 79 | + if r.Verified { |
| 80 | + p.verified = append(p.verified, row) |
| 81 | + } else { |
| 82 | + p.unverified = append(p.unverified, row) |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// Close renders the buffered findings to Markdown. Close should be invoked by |
| 88 | +// the output manager once scanning finishes. |
| 89 | +func (p *MarkdownPrinter) Close() error { |
| 90 | + p.mu.Lock() |
| 91 | + defer p.mu.Unlock() |
| 92 | + |
| 93 | + doc := renderMarkdown(p.verified, p.unverified) |
| 94 | + if doc == "" { |
| 95 | + return nil |
| 96 | + } |
| 97 | + if _, err := fmt.Fprint(p.out, doc); err != nil { |
| 98 | + return fmt.Errorf("write markdown: %w", err) |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// renderMarkdown mirrors templates/trufflehog_report.py by emitting a title, |
| 104 | +// optional sections for verified/unverified findings, and per-section counts. |
| 105 | +func renderMarkdown(verified, unverified []markdownRow) string { |
| 106 | + if len(verified) == 0 && len(unverified) == 0 { |
| 107 | + return "" |
| 108 | + } |
| 109 | + |
| 110 | + var buf bytes.Buffer |
| 111 | + buf.WriteString("# TruffleHog Findings\n\n") |
| 112 | + writeSection := func(title string, rows []markdownRow) { |
| 113 | + if len(rows) == 0 { |
| 114 | + return |
| 115 | + } |
| 116 | + sort.SliceStable(rows, func(i, j int) bool { |
| 117 | + if rows[i].Detector != rows[j].Detector { |
| 118 | + return rows[i].Detector < rows[j].Detector |
| 119 | + } |
| 120 | + if rows[i].File != rows[j].File { |
| 121 | + return rows[i].File < rows[j].File |
| 122 | + } |
| 123 | + return rows[i].Line < rows[j].Line |
| 124 | + }) |
| 125 | + |
| 126 | + fmt.Fprintf(&buf, "## %s (%d)\n", title, len(rows)) |
| 127 | + buf.WriteString("| Detector | File | Line | Redacted |\n") |
| 128 | + buf.WriteString("| --- | --- | --- | --- |\n") |
| 129 | + for _, row := range rows { |
| 130 | + fmt.Fprintf(&buf, "| %s | %s | %s | %s |\n", row.Detector, row.File, row.Line, row.Redacted) |
| 131 | + } |
| 132 | + buf.WriteString("\n") |
| 133 | + } |
| 134 | + |
| 135 | + writeSection("Verified Findings", append([]markdownRow(nil), verified...)) |
| 136 | + writeSection("Unverified Findings", append([]markdownRow(nil), unverified...)) |
| 137 | + |
| 138 | + return strings.TrimRight(buf.String(), "\n") + "\n" |
| 139 | +} |
| 140 | + |
| 141 | +var sanitizer = strings.NewReplacer("\r", " ", "\n", " ", "|", "\\|") |
| 142 | + |
| 143 | +func sanitize(value string) string { return sanitizer.Replace(value) } |
0 commit comments