From da23d7df473b976a3144f68c071c213b2ba15893 Mon Sep 17 00:00:00 2001 From: "Dr. Mohammed Lubbad" Date: Sat, 21 Nov 2026 16:57:19 +0300 Subject: [PATCH] fix(report): preserve finding ID suffix and handle Unicode combining/BiDi marks in FindingFilename --- report/findings.go | 75 +++++++++++++++++++++++++++++++++++++------ report/report_test.go | 46 ++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 9 deletions(-) diff --git a/report/findings.go b/report/findings.go index 7c3aa86..e3edc0e 100644 --- a/report/findings.go +++ b/report/findings.go @@ -9,8 +9,11 @@ import ( "sort" "strings" "time" + "unicode" + "unicode/utf8" "github.com/Autumn-27/artex/db" + "golang.org/x/text/unicode/norm" ) // Rendering for the findings page "Export": renders a batch of findings table rows @@ -122,21 +125,75 @@ func SingleFindingMarkdown(f *db.DBFinding, generatedAt time.Time) string { var unsafeFilenameChars = regexp.MustCompile(`[^\p{Han}\p{L}\p{N}._-]+`) +const maxFindingFilenameBaseBytes = 120 + +// stripCombiningAndFormat drops uncombined non-spacing marks (e.g. Arabic +// diacritics after NFC composition) and format controls (e.g. BiDi RLO/LRO) +// so they are omitted rather than turned into underscores between letters. +func stripCombiningAndFormat(s string) string { + s = norm.NFC.String(s) + return strings.Map(func(r rune) rune { + if unicode.Is(unicode.Mn, r) || unicode.Is(unicode.Cf, r) { + return -1 + } + return r + }, s) +} + +func truncateUTF8Bytes(s string, maxBytes int) string { + if maxBytes <= 0 { + return "" + } + if len(s) <= maxBytes { + return s + } + n := 0 + for i := range s { + if i > maxBytes { + break + } + n = i + } + if n == 0 && len(s) > 0 { + _, size := utf8.DecodeRuneInString(s) + if size <= maxBytes { + return s[:size] + } + return "" + } + return s[:n] +} + // FindingFilename generates a safe .md filename for "one file per vulnerability", e.g. -// `critical_SQL_injection_#123.md`. Strips path separators and control characters to avoid illegal paths inside the zip. +// `critical_SQL_injection_123.md`. Strips path separators, combining diacritics, and +// control characters to avoid illegal paths inside the zip while preserving the ID suffix. func FindingFilename(f *db.DBFinding) string { - sev := nz(f.Severity, "info") - title := findingTitle(f) - name := fmt.Sprintf("%s_%s_#%d", sev, title, f.ID) - name = unsafeFilenameChars.ReplaceAllString(name, "_") - name = strings.Trim(name, "._") - if name == "" { + sev := unsafeFilenameChars.ReplaceAllString(stripCombiningAndFormat(nz(f.Severity, "info")), "_") + sev = strings.Trim(sev, "._") + if sev == "" { + sev = "info" + } + title := unsafeFilenameChars.ReplaceAllString(stripCombiningAndFormat(findingTitle(f)), "_") + title = strings.Trim(title, "._") + suffix := fmt.Sprintf("_%d", f.ID) + prefix := sev + if title != "" { + prefix = sev + "_" + title + } + budget := maxFindingFilenameBaseBytes - len(suffix) + if len(prefix) > budget { + prefix = strings.TrimRight(truncateUTF8Bytes(prefix, budget), "._") + } + var name string + if prefix == "" { name = fmt.Sprintf("finding_%d", f.ID) + } else { + name = prefix + suffix } // Defensive: strip one more path layer to rule out zip slip. name = path.Base(name) - if len(name) > 120 { - name = strings.ToValidUTF8(name[:120], "") + if name == "" || name == "." || name == ".." { + name = fmt.Sprintf("finding_%d", f.ID) } return name + ".md" } diff --git a/report/report_test.go b/report/report_test.go index 88069b1..a9579e4 100644 --- a/report/report_test.go +++ b/report/report_test.go @@ -355,3 +355,49 @@ func TestFindingsCSVNeutralizesFormulas(t *testing.T) { t.Errorf("plain severity %q should not be prefixed", row[3]) } } + +func TestFindingFilenameUnicodeAndIDSuffix(t *testing.T) { + // Vocalized Arabic should strip combining diacritics cleanly rather than inserting underscores. + arabic := FindingFilename(&db.DBFinding{ + ID: 42, + Severity: "critical", + Name: "ثَغْرَة حَقْن SQL", + }) + if arabic != "critical_ثغرة_حقن_SQL_42.md" { + t.Fatalf("arabic filename = %q, want %q", arabic, "critical_ثغرة_حقن_SQL_42.md") + } + + // Composed Latin/Turkish letters stay intact after NFC normalization. + turkish := FindingFilename(&db.DBFinding{ + ID: 9, + Severity: "medium", + Name: "Gu\u0308venlik Ac\u0327ıg\u0306ı", + }) + if turkish != "medium_Güvenlik_Açığı_9.md" { + t.Fatalf("turkish filename = %q, want %q", turkish, "medium_Güvenlik_Açığı_9.md") + } + + // BiDi RLO control characters are stripped without breaking words. + bidi := FindingFilename(&db.DBFinding{ + ID: 15, + Severity: "high", + Name: "upload\u202Egpj.exe", + }) + if strings.ContainsRune(bidi, '\u202E') || bidi != "high_uploadgpj.exe_15.md" { + t.Fatalf("bidi filename = %q", bidi) + } + + // Long multi-byte titles stay valid UTF-8, within 120 bytes before .md, and keep distinct ID suffixes. + longPrefix := strings.Repeat("اختراق_صلاحيات_", 10) + f1 := FindingFilename(&db.DBFinding{ID: 101, Severity: "high", Name: longPrefix + "أ"}) + f2 := FindingFilename(&db.DBFinding{ID: 102, Severity: "high", Name: longPrefix + "ب"}) + for _, name := range []string{f1, f2} { + base := strings.TrimSuffix(name, ".md") + if len(base) > maxFindingFilenameBaseBytes { + t.Fatalf("filename base %q exceeds %d bytes (%d)", base, maxFindingFilenameBaseBytes, len(base)) + } + } + if !strings.HasSuffix(f1, "_101.md") || !strings.HasSuffix(f2, "_102.md") || f1 == f2 { + t.Fatalf("expected preserved distinct ID suffixes: f1=%q f2=%q", f1, f2) + } +}