Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display the description result incorrectly if the vulnerability contains quotation (AST-40454) #833

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions internal/commands/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"encoding/json"
"fmt"
"html"
"log"
"net/url"
"os"
Expand Down Expand Up @@ -1559,7 +1560,21 @@ func exportSonarResults(targetFile string, results *wrappers.ScanResultsCollecti
_ = f.Close()
return nil
}

// Function to decode HTML entities in the ScanResultsCollection
func decodeHTMLEntitiesInResults(results *wrappers.ScanResultsCollection) {
for _, result := range results.Results {
result.Description = html.UnescapeString(result.Description)
result.DescriptionHTML = html.UnescapeString(result.DescriptionHTML)
for _, node := range result.ScanResultData.Nodes {
node.FullName = html.UnescapeString(node.FullName)
node.Name = html.UnescapeString(node.Name)
}
}
}

func exportJSONResults(targetFile string, results *wrappers.ScanResultsCollection) error {
decodeHTMLEntitiesInResults(results)
var err error
var resultsJSON []byte
log.Println("Creating JSON Report: ", targetFile)
Expand Down
37 changes: 37 additions & 0 deletions internal/commands/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ func TestRunGetResultsByScanIdJsonFormat(t *testing.T) {
removeFileBySuffix(t, printer.FormatJSON)
}

func TestDecodeHTMLEntitiesInResults(t *testing.T) {
// Setup: Creating test data with HTML entities
results := createTestScanResultsCollection()

decodeHTMLEntitiesInResults(results)

expectedFullName := `SomeClass<T>`
expectedName := `Name with "quotes"`

if results.Results[0].ScanResultData.Nodes[0].FullName != expectedFullName {
t.Errorf("expected FullName to be %q, got %q", expectedFullName, results.Results[0].ScanResultData.Nodes[0].FullName)
}

if results.Results[0].ScanResultData.Nodes[0].Name != expectedName {
t.Errorf("expected Name to be %q, got %q", expectedName, results.Results[0].ScanResultData.Nodes[0].Name)
}
}

func TestRunGetResultsByScanIdJsonFormatWithContainers(t *testing.T) {
clearFlags()
mock.Flag = wrappers.FeatureFlagResponseModel{Name: wrappers.ContainerEngineCLIEnabled, Status: true}
Expand Down Expand Up @@ -310,6 +328,25 @@ func TestRunGetResultsByScanIdSummaryMarkdownFormat(t *testing.T) {
removeFileBySuffix(t, "md")
}

func createTestScanResultsCollection() *wrappers.ScanResultsCollection {
return &wrappers.ScanResultsCollection{
Results: []*wrappers.ScanResult{
{
Description: "Vulnerability in SomeComponent",
DescriptionHTML: "Description with quotes",
ScanResultData: wrappers.ScanResultData{
Nodes: []*wrappers.ScanResultNode{
{
FullName: "SomeClass&lt;T&gt;",
Name: "Name with &quot;quotes&quot;",
},
},
},
},
},
}
}

func removeFileBySuffix(t *testing.T, suffix string) {
removeFile(t, fileName, suffix)
}
Expand Down
Loading