Path: blob/dev/pkg/protocols/file/request_test.go
2070 views
package file12import (3"archive/zip"4"bytes"5"compress/gzip"6"context"7"os"8"path/filepath"9"testing"1011"github.com/stretchr/testify/require"1213"github.com/projectdiscovery/nuclei/v3/pkg/model"14"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"15"github.com/projectdiscovery/nuclei/v3/pkg/operators"16"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"17"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"18"github.com/projectdiscovery/nuclei/v3/pkg/output"19"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"20"github.com/projectdiscovery/nuclei/v3/pkg/testutils"21permissionutil "github.com/projectdiscovery/utils/permission"22)2324func zipFile(t *testing.T, fileName string, data []byte) []byte {25var b bytes.Buffer26w := zip.NewWriter(&b)27w1, err := w.Create(fileName)28require.NoError(t, err)29_, err = w1.Write(data)30require.NoError(t, err)31err = w.Close()32require.NoError(t, err)33return b.Bytes()34}3536func gzipFile(t *testing.T, data []byte) []byte {37var b bytes.Buffer38w := gzip.NewWriter(&b)39_, err := w.Write(data)40require.NoError(t, err)41err = w.Close()42require.NoError(t, err)43return b.Bytes()44}4546func TestFileExecuteWithResults(t *testing.T) {47var testCaseBase = []byte("TEST\r\n1.1.1.1\r\n")48const testCaseBaseFilename = "config.yaml"49var testCases = []struct {50fileName string51data []byte52}{53{54fileName: testCaseBaseFilename,55data: testCaseBase,56},57{58fileName: testCaseBaseFilename + ".gz",59data: gzipFile(t, testCaseBase),60},61{62fileName: "config.yaml.zip",63data: zipFile(t, testCaseBaseFilename, testCaseBase),64},65}6667for _, tt := range testCases {68options := testutils.DefaultOptions6970testutils.Init(options)71templateID := "testing-file"72executerOpts := testutils.NewMockExecuterOptions(options, &testutils.TemplateInfo{73ID: templateID,74Info: model.Info{SeverityHolder: severity.Holder{Severity: severity.Low}, Name: "test"},75})7677request := &Request{78ID: templateID,79MaxSize: "1Gb",80NoRecursive: false,81Extensions: []string{"all"},82DenyList: []string{".go"},83Archive: true,84Operators: operators.Operators{85Matchers: []*matchers.Matcher{{86Name: "test",87Part: "raw",88Type: matchers.MatcherTypeHolder{MatcherType: matchers.WordsMatcher},89Words: []string{"1.1.1.1"},90}},91Extractors: []*extractors.Extractor{{92Part: "raw",93Type: extractors.ExtractorTypeHolder{ExtractorType: extractors.RegexExtractor},94Regex: []string{"[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"},95}},96},97options: executerOpts,98}99err := request.Compile(executerOpts)100require.Nil(t, err, "could not compile file request")101102tempDir, err := os.MkdirTemp("", "test-*")103require.Nil(t, err, "could not create temporary directory")104defer func() {105_ = os.RemoveAll(tempDir)106}()107108files := map[string][]byte{109tt.fileName: tt.data,110}111for k, v := range files {112err = os.WriteFile(filepath.Join(tempDir, k), v, permissionutil.TempFilePermission)113require.Nil(t, err, "could not write temporary file")114}115116var finalEvent *output.InternalWrappedEvent117t.Run("valid", func(t *testing.T) {118metadata := make(output.InternalEvent)119previous := make(output.InternalEvent)120ctxArgs := contextargs.NewWithInput(context.Background(), tempDir)121err := request.ExecuteWithResults(ctxArgs, metadata, previous, func(event *output.InternalWrappedEvent) {122finalEvent = event123})124require.Nil(t, err, "could not execute file request")125})126require.NotNil(t, finalEvent, "could not get event output from request")127require.Equal(t, 1, len(finalEvent.Results), "could not get correct number of results")128require.Equal(t, "test", finalEvent.Results[0].MatcherName, "could not get correct matcher name of results")129require.Equal(t, 1, len(finalEvent.Results[0].ExtractedResults), "could not get correct number of extracted results")130require.Equal(t, "1.1.1.1", finalEvent.Results[0].ExtractedResults[0], "could not get correct extracted results")131finalEvent = nil132}133}134135136