2021-02-27 15:24:22 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-11-24 15:09:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-02-27 15:24:22 +00:00
|
|
|
"github.com/projectdiscovery/gologger"
|
2021-06-30 13:09:01 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
|
2021-11-04 21:31:41 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
|
2021-02-27 15:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDownloadReleaseAndUnzipAddition(t *testing.T) {
|
|
|
|
gologger.DefaultLogger.SetWriter(&testutils.NoopWriter{})
|
|
|
|
|
|
|
|
templatesDirectory, err := ioutil.TempDir("", "template-*")
|
|
|
|
require.Nil(t, err, "could not create temp directory")
|
|
|
|
defer os.RemoveAll(templatesDirectory)
|
|
|
|
|
2021-07-03 10:50:19 +00:00
|
|
|
r := &Runner{templatesConfig: &config.Config{TemplatesDirectory: templatesDirectory}, options: testutils.DefaultOptions}
|
2021-02-27 15:24:22 +00:00
|
|
|
|
|
|
|
newTempDir, err := ioutil.TempDir("", "new-tmp-*")
|
|
|
|
require.Nil(t, err, "could not create temp directory")
|
|
|
|
defer os.RemoveAll(newTempDir)
|
|
|
|
|
2021-11-24 15:09:38 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(newTempDir, "base.yaml"), []byte("id: test"), os.ModePerm)
|
2021-02-27 15:24:22 +00:00
|
|
|
require.Nil(t, err, "could not create base file")
|
2021-11-24 15:09:38 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(newTempDir, "new.yaml"), []byte("id: test"), os.ModePerm)
|
2021-02-27 15:24:22 +00:00
|
|
|
require.Nil(t, err, "could not create new file")
|
2022-02-22 07:40:57 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(newTempDir, ".new-additions"), []byte("new.yaml"), os.ModePerm)
|
|
|
|
require.Nil(t, err, "could not create new file")
|
2021-02-27 15:24:22 +00:00
|
|
|
|
|
|
|
err = zipFromDirectory("new.zip", newTempDir)
|
|
|
|
require.Nil(t, err, "could not create new zip from directory")
|
|
|
|
defer os.Remove("new.zip")
|
|
|
|
|
|
|
|
ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "new.zip")
|
|
|
|
}))
|
|
|
|
defer ts2.Close()
|
|
|
|
|
2022-02-22 07:40:57 +00:00
|
|
|
results, err := r.downloadReleaseAndUnzip(context.Background(), "1.0.1", ts2.URL)
|
2021-02-27 15:24:22 +00:00
|
|
|
require.Nil(t, err, "could not download release and unzip")
|
|
|
|
|
|
|
|
require.Equal(t, "new.yaml", results.additions[0], "could not get correct new addition")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDownloadReleaseAndUnzipDeletion(t *testing.T) {
|
|
|
|
gologger.DefaultLogger.SetWriter(&testutils.NoopWriter{})
|
|
|
|
|
|
|
|
baseTemplates, err := ioutil.TempDir("", "old-temp-*")
|
|
|
|
require.Nil(t, err, "could not create temp directory")
|
|
|
|
defer os.RemoveAll(baseTemplates)
|
|
|
|
|
2021-11-24 15:09:38 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(baseTemplates, "base.yaml"), []byte("id: test"), os.ModePerm)
|
2021-02-27 15:24:22 +00:00
|
|
|
require.Nil(t, err, "could not create write base file")
|
2022-02-22 07:40:57 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(baseTemplates, ".new-additions"), []byte("base.yaml"), os.ModePerm)
|
|
|
|
require.Nil(t, err, "could not create new file")
|
2021-02-27 15:24:22 +00:00
|
|
|
|
|
|
|
err = zipFromDirectory("base.zip", baseTemplates)
|
|
|
|
require.Nil(t, err, "could not create zip from directory")
|
|
|
|
defer os.Remove("base.zip")
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "base.zip")
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
templatesDirectory, err := ioutil.TempDir("", "template-*")
|
|
|
|
require.Nil(t, err, "could not create temp directory")
|
|
|
|
defer os.RemoveAll(templatesDirectory)
|
|
|
|
|
2021-07-03 10:50:19 +00:00
|
|
|
r := &Runner{templatesConfig: &config.Config{TemplatesDirectory: templatesDirectory}, options: testutils.DefaultOptions}
|
2021-02-27 15:24:22 +00:00
|
|
|
|
|
|
|
results, err := r.downloadReleaseAndUnzip(context.Background(), "1.0.0", ts.URL)
|
|
|
|
require.Nil(t, err, "could not download release and unzip")
|
|
|
|
require.Equal(t, "base.yaml", results.additions[0], "could not get correct base addition")
|
|
|
|
|
|
|
|
newTempDir, err := ioutil.TempDir("", "new-tmp-*")
|
|
|
|
require.Nil(t, err, "could not create temp directory")
|
|
|
|
defer os.RemoveAll(newTempDir)
|
|
|
|
|
2022-02-22 07:40:57 +00:00
|
|
|
err = ioutil.WriteFile(filepath.Join(newTempDir, ".new-additions"), []byte(""), os.ModePerm)
|
|
|
|
require.Nil(t, err, "could not create new file")
|
|
|
|
|
2021-02-27 15:24:22 +00:00
|
|
|
err = zipFromDirectory("new.zip", newTempDir)
|
|
|
|
require.Nil(t, err, "could not create new zip from directory")
|
|
|
|
defer os.Remove("new.zip")
|
|
|
|
|
|
|
|
ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
http.ServeFile(w, r, "new.zip")
|
|
|
|
}))
|
|
|
|
defer ts2.Close()
|
|
|
|
|
|
|
|
results, err = r.downloadReleaseAndUnzip(context.Background(), "1.0.1", ts2.URL)
|
|
|
|
require.Nil(t, err, "could not download release and unzip")
|
|
|
|
|
|
|
|
require.Equal(t, "base.yaml", results.deletions[0], "could not get correct new deletions")
|
|
|
|
}
|
|
|
|
|
2021-12-20 13:11:55 +00:00
|
|
|
func TestCalculateTemplateAbsolutePathPositiveScenario(t *testing.T) {
|
2021-11-25 12:46:45 +00:00
|
|
|
configuredTemplateDirectory := filepath.Join(os.TempDir(), "templates")
|
|
|
|
defer os.RemoveAll(configuredTemplateDirectory)
|
|
|
|
|
|
|
|
t.Run("positive scenarios", func(t *testing.T) {
|
|
|
|
zipFilePathsExpectedPathsMap := map[string]string{
|
|
|
|
"nuclei-templates/cve/test.yaml": filepath.Join(configuredTemplateDirectory, "cve/test.yaml"),
|
|
|
|
"nuclei-templates/cve/test/test.yaml": filepath.Join(configuredTemplateDirectory, "cve/test/test.yaml"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for filePathFromZip, expectedTemplateAbsPath := range zipFilePathsExpectedPathsMap {
|
|
|
|
calculatedTemplateAbsPath, skipFile, err := calculateTemplateAbsolutePath(filePathFromZip, configuredTemplateDirectory)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, expectedTemplateAbsPath, calculatedTemplateAbsPath)
|
|
|
|
require.False(t, skipFile)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-27 15:24:22 +00:00
|
|
|
func zipFromDirectory(zipPath, directory string) error {
|
|
|
|
file, err := os.Create(zipPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
w := zip.NewWriter(file)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
walker := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
f, err := w.Create(strings.TrimPrefix(path, directory))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return filepath.Walk(directory, walker)
|
|
|
|
}
|