Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/cmd/integration-test/templates-dir-env.go
2843 views
1
package main
2
3
import (
4
"os"
5
"path/filepath"
6
7
osutils "github.com/projectdiscovery/utils/os"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
10
"github.com/projectdiscovery/utils/errkit"
11
)
12
13
// isNotLinux returns true if not running on Linux (used to skip tests on non-Linux OS)
14
var isNotLinux = func() bool { return !osutils.IsLinux() }
15
16
var templatesDirEnvTestCases = []TestCaseInfo{
17
{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvBasicTest{}, DisableOn: isNotLinux},
18
{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvAbsolutePathTest{}, DisableOn: isNotLinux},
19
{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvRelativePathTest{}, DisableOn: isNotLinux},
20
{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvPrecedenceTest{}, DisableOn: isNotLinux},
21
{Path: "protocols/dns/cname-fingerprint.yaml", TestCase: &templatesDirEnvCustomTemplatesTest{}, DisableOn: isNotLinux},
22
}
23
24
// copyTemplateToDir copies a template file to a destination directory, preserving the directory structure
25
func copyTemplateToDir(templatePath, destDir string) error {
26
// Read the template file
27
templateData, err := os.ReadFile(templatePath)
28
if err != nil {
29
return errkit.Wrap(err, "failed to read template file")
30
}
31
32
// Create the destination path preserving directory structure
33
destPath := filepath.Join(destDir, templatePath)
34
destDirPath := filepath.Dir(destPath)
35
36
// Create the destination directory if it doesn't exist
37
if err := os.MkdirAll(destDirPath, 0755); err != nil {
38
return errkit.Wrap(err, "failed to create destination directory")
39
}
40
41
// Write the template file
42
if err := os.WriteFile(destPath, templateData, 0644); err != nil {
43
return errkit.Wrap(err, "failed to write template file")
44
}
45
46
return nil
47
}
48
49
// templatesDirEnvBasicTest tests basic functionality of NUCLEI_TEMPLATES_DIR
50
type templatesDirEnvBasicTest struct{}
51
52
// Execute executes a test case and returns an error if occurred
53
func (h *templatesDirEnvBasicTest) Execute(filePath string) error {
54
tempdir, err := os.MkdirTemp("", "nuclei-templates-dir-env-*")
55
if err != nil {
56
return errkit.Wrap(err, "failed to create temp dir")
57
}
58
defer func() {
59
_ = os.RemoveAll(tempdir)
60
}()
61
62
// Copy template to temp directory
63
if err := copyTemplateToDir(filePath, tempdir); err != nil {
64
return err
65
}
66
67
// Set NUCLEI_TEMPLATES_DIR and run nuclei
68
envVars := []string{"NUCLEI_TEMPLATES_DIR=" + tempdir}
69
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")
70
if err != nil {
71
return err
72
}
73
74
return expectResultsCount(results, 1)
75
}
76
77
// templatesDirEnvAbsolutePathTest tests that absolute paths work correctly
78
type templatesDirEnvAbsolutePathTest struct{}
79
80
// Execute executes a test case and returns an error if occurred
81
func (h *templatesDirEnvAbsolutePathTest) Execute(filePath string) error {
82
tempdir, err := os.MkdirTemp("", "nuclei-templates-dir-env-abs-*")
83
if err != nil {
84
return errkit.Wrap(err, "failed to create temp dir")
85
}
86
defer func() {
87
_ = os.RemoveAll(tempdir)
88
}()
89
90
// Get absolute path
91
absTempDir, err := filepath.Abs(tempdir)
92
if err != nil {
93
return errkit.Wrap(err, "failed to get absolute path")
94
}
95
96
// Copy template to temp directory
97
if err := copyTemplateToDir(filePath, absTempDir); err != nil {
98
return err
99
}
100
101
// Set NUCLEI_TEMPLATES_DIR with absolute path and run nuclei
102
envVars := []string{"NUCLEI_TEMPLATES_DIR=" + absTempDir}
103
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")
104
if err != nil {
105
return err
106
}
107
108
return expectResultsCount(results, 1)
109
}
110
111
// templatesDirEnvRelativePathTest tests that relative paths are resolved correctly
112
type templatesDirEnvRelativePathTest struct{}
113
114
// Execute executes a test case and returns an error if occurred
115
func (h *templatesDirEnvRelativePathTest) Execute(filePath string) error {
116
// Create temp directory in current working directory
117
tempdir, err := os.MkdirTemp(".", "nuclei-templates-dir-env-rel-*")
118
if err != nil {
119
return errkit.Wrap(err, "failed to create temp dir")
120
}
121
defer func() {
122
_ = os.RemoveAll(tempdir)
123
}()
124
125
// Get relative path (just the directory name)
126
relPath := filepath.Base(tempdir)
127
128
// Copy template to temp directory
129
if err := copyTemplateToDir(filePath, tempdir); err != nil {
130
return err
131
}
132
133
// Set NUCLEI_TEMPLATES_DIR with relative path and run nuclei
134
// Note: The implementation should convert relative paths to absolute
135
envVars := []string{"NUCLEI_TEMPLATES_DIR=" + relPath}
136
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")
137
if err != nil {
138
return err
139
}
140
141
return expectResultsCount(results, 1)
142
}
143
144
// templatesDirEnvPrecedenceTest tests that -ud flag takes precedence over NUCLEI_TEMPLATES_DIR
145
type templatesDirEnvPrecedenceTest struct{}
146
147
// Execute executes a test case and returns an error if occurred
148
func (h *templatesDirEnvPrecedenceTest) Execute(filePath string) error {
149
// Create two temp directories
150
envTempDir, err := os.MkdirTemp("", "nuclei-templates-dir-env-*")
151
if err != nil {
152
return errkit.Wrap(err, "failed to create env temp dir")
153
}
154
defer func() {
155
_ = os.RemoveAll(envTempDir)
156
}()
157
158
flagTempDir, err := os.MkdirTemp("", "nuclei-templates-dir-flag-*")
159
if err != nil {
160
return errkit.Wrap(err, "failed to create flag temp dir")
161
}
162
defer func() {
163
_ = os.RemoveAll(flagTempDir)
164
}()
165
166
// Copy template to flag temp directory (this should be used due to precedence)
167
if err := copyTemplateToDir(filePath, flagTempDir); err != nil {
168
return err
169
}
170
171
// Set NUCLEI_TEMPLATES_DIR to envTempDir (should be ignored due to -ud flag)
172
envVars := []string{"NUCLEI_TEMPLATES_DIR=" + envTempDir}
173
// Use -ud flag which should take precedence
174
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com", "-ud", flagTempDir)
175
if err != nil {
176
return err
177
}
178
179
return expectResultsCount(results, 1)
180
}
181
182
// templatesDirEnvCustomTemplatesTest tests that custom template subdirectories are correctly set
183
type templatesDirEnvCustomTemplatesTest struct{}
184
185
// Execute executes a test case and returns an error if occurred
186
func (h *templatesDirEnvCustomTemplatesTest) Execute(filePath string) error {
187
tempdir, err := os.MkdirTemp("", "nuclei-templates-dir-custom-*")
188
if err != nil {
189
return errkit.Wrap(err, "failed to create temp dir")
190
}
191
defer func() {
192
_ = os.RemoveAll(tempdir)
193
}()
194
195
// Create custom template subdirectories structure
196
customDirs := []string{"github", "s3", "gitlab", "azure"}
197
for _, dir := range customDirs {
198
customDirPath := filepath.Join(tempdir, dir)
199
if err := os.MkdirAll(customDirPath, 0755); err != nil {
200
return errkit.Wrap(err, "failed to create custom template directory")
201
}
202
}
203
204
// Copy template to temp directory
205
if err := copyTemplateToDir(filePath, tempdir); err != nil {
206
return err
207
}
208
209
// Set NUCLEI_TEMPLATES_DIR and run nuclei
210
envVars := []string{"NUCLEI_TEMPLATES_DIR=" + tempdir}
211
results, err := testutils.RunNucleiBareArgsAndGetResults(debug, envVars, "-t", filePath, "-u", "8x8exch02.8x8.com")
212
if err != nil {
213
return err
214
}
215
216
return expectResultsCount(results, 1)
217
}
218
219