Repository: safedep/vet
Pull request #435 (2025-03-29 08:05:56)

Fixed plaintext in sarif messages
Exported sarif message -
```
message": {
  "text": "Policy Violation Package langchain violates policy filter-query.",
  "markdown": "## Policy Violation\nPackage `langchain` violates policy `filter-query`.\n\n"
},
```

Reported in defectdojo -
![image](https://github.com/user-attachments/assets/470b1bbf-1ac6-40df-a6a1-2b7dad1f54de)



Patch:
diff --git a/pkg/reporter/markdown/markdown.go b/pkg/reporter/markdown/markdown.go
index e3e4197..f86b957 100644
--- a/pkg/reporter/markdown/markdown.go
+++ b/pkg/reporter/markdown/markdown.go
@@ -3,6 +3,7 @@ package markdown
 import (
 	"bytes"
 	"fmt"
+	"regexp"
 	"strings"
 )
 
@@ -84,3 +85,32 @@ func (mb *MarkdownBuilder) AddCollapsibleSection(section *MarkdownCollapsibleSec
 func (mb *MarkdownBuilder) Build() string {
 	return mb.content.String()
 }
+
+var (
+	headerBulletRegex   = regexp.MustCompile(`(?m)^(#{1,6}\s+|[-*]\s{1,}|\d+\.\s+|>\s+)`)
+	inlineCodeRegex     = regexp.MustCompile("`{1,3}([^`]*)`{1,3}")
+	horizontalRuleRegex = regexp.MustCompile(`(?m)^\s*(-{3,}|\*{3,}|\_{3,})\s*$`)
+	boldItalicRegex     = regexp.MustCompile(`(?:\*\*\*|___)(.*?)(?:\*\*\*|___)`)
+	boldRegex           = regexp.MustCompile(`(?:\*\*|__)(.*?)(?:\*\*|__)`)
+	italicRegex         = regexp.MustCompile(`(?:\*|_)(.*?)(?:\*|_)`)
+	strikethroughRegex  = regexp.MustCompile(`~~([^~]+)~~`)
+	inlineLinkRegex     = regexp.MustCompile(`\[([^\]]+)\]\((\S+?)\)`)
+	imageRegex          = regexp.MustCompile(`!\[([^\]]*)\]\((\S+?)\)`)
+	extraSpacesRegex    = regexp.MustCompile(`\s+`)
+)
+
+func (mb *MarkdownBuilder) BuildPlainText() string {
+	content := mb.content.String()
+	content = headerBulletRegex.ReplaceAllString(content, "")
+	content = inlineCodeRegex.ReplaceAllString(content, "$1")
+	content = horizontalRuleRegex.ReplaceAllString(content, "")
+	content = boldItalicRegex.ReplaceAllString(content, "$1")
+	content = boldRegex.ReplaceAllString(content, "$1")
+	content = italicRegex.ReplaceAllString(content, "$1")
+	content = strikethroughRegex.ReplaceAllString(content, "$1")
+	content = imageRegex.ReplaceAllString(content, "$1")
+	content = inlineLinkRegex.ReplaceAllString(content, "$1")
+	content = extraSpacesRegex.ReplaceAllString(content, " ")
+
+	return strings.Trim(content, " \n")
+}
diff --git a/pkg/reporter/sarif_builder.go b/pkg/reporter/sarif_builder.go
index 07c59bd..1b6088b 100644
--- a/pkg/reporter/sarif_builder.go
+++ b/pkg/reporter/sarif_builder.go
@@ -162,7 +162,7 @@ func (b *sarifBuilder) buildFilterResultMessageMarkdown(event *analyzer.Analyzer
 	// SARIF spec mandates that we provide text in addition to markdown
 	msg := sarif.NewMessage().
 		WithMarkdown(md.Build()).
-		WithText(md.Build())
+		WithText(md.BuildPlainText())
 
 	return msg
 }


Test patch:
diff --git a/pkg/reporter/markdown/markdown_test.go b/pkg/reporter/markdown/markdown_test.go
index d109cc0..619787f 100644
--- a/pkg/reporter/markdown/markdown_test.go
+++ b/pkg/reporter/markdown/markdown_test.go
@@ -63,3 +63,26 @@ func TestMarkdownBuilder(t *testing.T) {
 		})
 	}
 }
+
+func TestMarkdownBuildPlainText(t *testing.T) {
+	markdownText := "# Heading 1\n## Heading 2\n### Heading 3\n#### Heading 4\n" +
+		"##### Heading 5\n###### Heading 6\n- Bullet list item 1  \n* Bullet list item 2\n" +
+		"- Another >bullet\n1. Numbered list - item 1  \n2. Numbered list *item 2\n" +
+		"> Blockquote text  \n---\n___\n**Bold text1**\n__Bold text2__  \n*Italic text1*\n" +
+		"_Italic text2_ \n***Bold and Italic text*** \n___Bold and Italic text___\n" +
+		"~~Strikethrough text~~ \n Line #has `Inline code` betwee # n\n" +
+		"Line has ``Inline code with backticks`` betwee_n \n ```Inline code in triple backticks```\n" +
+		"```\nmultiline code\n```\n```python\nprint('Hello, Markdown!')\n```\n" +
+		"[Link text](https://example.com)\n![Image alt-text](https://example.com/image.jpg)\n" +
+		"Extra      spaces    in      line. \n*This should be ##italic* and **this should # be #bold**.\n" +
+		"~~This is crossed out~~ and `this is inline code`.\nEmoji: 🎉 🚀"
+
+	expectedPlainText := "Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Heading 6 Bullet list item 1 Bullet list item 2 Another >bullet Numbered list - item 1 Numbered list *item 2 Blockquote text Bold text1 Bold text2 Italic text1 Italic text2 Bold and Italic text Bold and Italic text Strikethrough text Line #has Inline code betwee # n Line has Inline code with backticks betwee_n Inline code in triple backticks multiline code python print('Hello, Markdown!') Link text Image alt-text Extra spaces in line. This should be ##italic and this should # be #bold. This is crossed out and this is inline code. Emoji: 🎉 🚀"
+
+	builder := NewMarkdownBuilder()
+	assert.NotNil(t, builder)
+
+	builder.AddRaw(markdownText)
+	plainText := builder.BuildPlainText()
+	assert.Equal(t, expectedPlainText, plainText)
+}