Path: blob/dev/cmd/integration-test/templates-dir-env.go
2843 views
package main12import (3"os"4"path/filepath"56osutils "github.com/projectdiscovery/utils/os"78"github.com/projectdiscovery/nuclei/v3/pkg/testutils"9"github.com/projectdiscovery/utils/errkit"10)1112// isNotLinux returns true if not running on Linux (used to skip tests on non-Linux OS)13var isNotLinux = func() bool { return !osutils.IsLinux() }1415var templatesDirEnvTestCases = []TestCaseInfo{16{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvBasicTest{}, DisableOn: isNotLinux},17{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvAbsolutePathTest{}, DisableOn: isNotLinux},18{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvRelativePathTest{}, DisableOn: isNotLinux},19{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvPrecedenceTest{}, DisableOn: isNotLinux},20{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvCustomTemplatesTest{}, DisableOn: isNotLinux},21}2223// copyTemplateToDir copies a template file to a destination directory, preserving the directory structure24func copyTemplateToDir(templatePath, destDir string) error {25// Read the template file26templateData, err := os.ReadFile(templatePath)27if err != nil {28return errkit.Wrap(err, "failed to read template file")29}3031// Create the destination path preserving directory structure32destPath := filepath.Join(destDir, templatePath)33destDirPath := filepath.Dir(destPath)3435// Create the destination directory if it doesn't exist36if err := os.MkdirAll(destDirPath, 0755); err != nil {37return errkit.Wrap(err, "failed to create destination directory")38}3940// Write the template file41if err := os.WriteFile(destPath, templateData, 0644); err != nil {42return errkit.Wrap(err, "failed to write template file")43}4445return nil46}4748// templatesDirEnvBasicTest tests basic functionality of NUCLEI_TEMPLATES_DIR49type templatesDirEnvBasicTest struct{}5051// Execute executes a test case and returns an error if occurred52func (h *templatesDirEnvBasicTest) Execute(filePath string) error {53tempdir, err := os.MkdirTemp("", "nuclei-templates-dir-env-*")54if err != nil {55return errkit.Wrap(err, "failed to create temp dir")56}57defer func() {58_ = os.RemoveAll(tempdir)59}()6061// Copy template to temp directory62if err := copyTemplateToDir(filePath, tempdir); err != nil {63return err64}6566// Set NUCLEI_TEMPLATES_DIR and run nuclei67envVars := []string{"NUCLEI_TEMPLATES_DIR=" + tempdir}68results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")69if err != nil {70return err71}7273return expectResultsCount(results, 1)74}7576// templatesDirEnvAbsolutePathTest tests that absolute paths work correctly77type templatesDirEnvAbsolutePathTest struct{}7879// Execute executes a test case and returns an error if occurred80func (h *templatesDirEnvAbsolutePathTest) Execute(filePath string) error {81tempdir, err := os.MkdirTemp("", "nuclei-templates-dir-env-abs-*")82if err != nil {83return errkit.Wrap(err, "failed to create temp dir")84}85defer func() {86_ = os.RemoveAll(tempdir)87}()8889// Get absolute path90absTempDir, err := filepath.Abs(tempdir)91if err != nil {92return errkit.Wrap(err, "failed to get absolute path")93}9495// Copy template to temp directory96if err := copyTemplateToDir(filePath, absTempDir); err != nil {97return err98}99100// Set NUCLEI_TEMPLATES_DIR with absolute path and run nuclei101envVars := []string{"NUCLEI_TEMPLATES_DIR=" + absTempDir}102results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")103if err != nil {104return err105}106107return expectResultsCount(results, 1)108}109110// templatesDirEnvRelativePathTest tests that relative paths are resolved correctly111type templatesDirEnvRelativePathTest struct{}112113// Execute executes a test case and returns an error if occurred114func (h *templatesDirEnvRelativePathTest) Execute(filePath string) error {115// Create temp directory in current working directory116tempdir, err := os.MkdirTemp(".", "nuclei-templates-dir-env-rel-*")117if err != nil {118return errkit.Wrap(err, "failed to create temp dir")119}120defer func() {121_ = os.RemoveAll(tempdir)122}()123124// Get relative path (just the directory name)125relPath := filepath.Base(tempdir)126127// Copy template to temp directory128if err := copyTemplateToDir(filePath, tempdir); err != nil {129return err130}131132// Set NUCLEI_TEMPLATES_DIR with relative path and run nuclei133// Note: The implementation should convert relative paths to absolute134envVars := []string{"NUCLEI_TEMPLATES_DIR=" + relPath}135results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")136if err != nil {137return err138}139140return expectResultsCount(results, 1)141}142143// templatesDirEnvPrecedenceTest tests that -ud flag takes precedence over NUCLEI_TEMPLATES_DIR144type templatesDirEnvPrecedenceTest struct{}145146// Execute executes a test case and returns an error if occurred147func (h *templatesDirEnvPrecedenceTest) Execute(filePath string) error {148// Create two temp directories149envTempDir, err := os.MkdirTemp("", "nuclei-templates-dir-env-*")150if err != nil {151return errkit.Wrap(err, "failed to create env temp dir")152}153defer func() {154_ = os.RemoveAll(envTempDir)155}()156157flagTempDir, err := os.MkdirTemp("", "nuclei-templates-dir-flag-*")158if err != nil {159return errkit.Wrap(err, "failed to create flag temp dir")160}161defer func() {162_ = os.RemoveAll(flagTempDir)163}()164165// Copy template to flag temp directory (this should be used due to precedence)166if err := copyTemplateToDir(filePath, flagTempDir); err != nil {167return err168}169170// Set NUCLEI_TEMPLATES_DIR to envTempDir (should be ignored due to -ud flag)171envVars := []string{"NUCLEI_TEMPLATES_DIR=" + envTempDir}172// Use -ud flag which should take precedence173results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com", "-ud", flagTempDir)174if err != nil {175return err176}177178return expectResultsCount(results, 1)179}180181// templatesDirEnvCustomTemplatesTest tests that custom template subdirectories are correctly set182type templatesDirEnvCustomTemplatesTest struct{}183184// Execute executes a test case and returns an error if occurred185func (h *templatesDirEnvCustomTemplatesTest) Execute(filePath string) error {186tempdir, err := os.MkdirTemp("", "nuclei-templates-dir-custom-*")187if err != nil {188return errkit.Wrap(err, "failed to create temp dir")189}190defer func() {191_ = os.RemoveAll(tempdir)192}()193194// Create custom template subdirectories structure195customDirs := []string{"github", "s3", "gitlab", "azure"}196for _, dir := range customDirs {197customDirPath := filepath.Join(tempdir, dir)198if err := os.MkdirAll(customDirPath, 0755); err != nil {199return errkit.Wrap(err, "failed to create custom template directory")200}201}202203// Copy template to temp directory204if err := copyTemplateToDir(filePath, tempdir); err != nil {205return err206}207208// Set NUCLEI_TEMPLATES_DIR and run nuclei209envVars := []string{"NUCLEI_TEMPLATES_DIR=" + tempdir}210results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")211if err != nil {212return err213}214215return expectResultsCount(results, 1)216}217218219