Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/cmd/integration-test/code.go
2070 views
1
package main
2
3
import (
4
"errors"
5
"log"
6
"os"
7
"path/filepath"
8
9
osutils "github.com/projectdiscovery/utils/os"
10
11
"github.com/projectdiscovery/nuclei/v3/pkg/templates"
12
"github.com/projectdiscovery/nuclei/v3/pkg/templates/signer"
13
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
14
)
15
16
var isCodeDisabled = func() bool { return osutils.IsWindows() && os.Getenv("CI") == "true" }
17
18
var codeTestCases = []TestCaseInfo{
19
{Path: "protocols/code/py-snippet.yaml", TestCase: &codeSnippet{}, DisableOn: isCodeDisabled},
20
{Path: "protocols/code/py-file.yaml", TestCase: &codeFile{}, DisableOn: isCodeDisabled},
21
{Path: "protocols/code/py-env-var.yaml", TestCase: &codeEnvVar{}, DisableOn: isCodeDisabled},
22
{Path: "protocols/code/unsigned.yaml", TestCase: &unsignedCode{}, DisableOn: isCodeDisabled},
23
{Path: "protocols/code/py-nosig.yaml", TestCase: &codePyNoSig{}, DisableOn: isCodeDisabled},
24
{Path: "protocols/code/py-interactsh.yaml", TestCase: &codeSnippet{}, DisableOn: isCodeDisabled},
25
{Path: "protocols/code/ps1-snippet.yaml", TestCase: &codeSnippet{}, DisableOn: func() bool { return !osutils.IsWindows() || isCodeDisabled() }},
26
{Path: "protocols/code/pre-condition.yaml", TestCase: &codePreCondition{}, DisableOn: isCodeDisabled},
27
}
28
29
const (
30
testCertFile = "protocols/keys/ci.crt"
31
testKeyFile = "protocols/keys/ci-private-key.pem"
32
)
33
34
var testcertpath = ""
35
36
func init() {
37
if isCodeDisabled() {
38
// skip executing code protocol in CI on windows
39
return
40
}
41
// allow local file access to load content of file references in template
42
// in order to sign them for testing purposes
43
templates.TemplateSignerLFA()
44
45
tsigner, err := signer.NewTemplateSignerFromFiles(testCertFile, testKeyFile)
46
if err != nil {
47
panic(err)
48
}
49
50
testcertpath, _ = filepath.Abs(testCertFile)
51
52
for _, v := range codeTestCases {
53
templatePath := v.Path
54
testCase := v.TestCase
55
56
if v.DisableOn != nil && v.DisableOn() {
57
// skip ps1 test case on non-windows platforms
58
continue
59
}
60
61
templatePath, err := filepath.Abs(templatePath)
62
if err != nil {
63
panic(err)
64
}
65
66
// skip
67
// - unsigned test cases
68
if _, ok := testCase.(*unsignedCode); ok {
69
continue
70
}
71
if _, ok := testCase.(*codePyNoSig); ok {
72
continue
73
}
74
if err := templates.SignTemplate(tsigner, templatePath); err != nil {
75
log.Fatalf("Could not sign template %v got: %s\n", templatePath, err)
76
}
77
}
78
79
}
80
81
func getEnvValues() []string {
82
return []string{
83
signer.CertEnvVarName + "=" + testcertpath,
84
}
85
}
86
87
type codeSnippet struct{}
88
89
// Execute executes a test case and returns an error if occurred
90
func (h *codeSnippet) Execute(filePath string) error {
91
results, err := testutils.RunNucleiArgsWithEnvAndGetResults(debug, getEnvValues(), "-t", filePath, "-u", "input", "-code")
92
if err != nil {
93
return err
94
}
95
return expectResultsCount(results, 1)
96
}
97
98
type codePreCondition struct{}
99
100
// Execute executes a test case and returns an error if occurred
101
func (h *codePreCondition) Execute(filePath string) error {
102
results, err := testutils.RunNucleiArgsWithEnvAndGetResults(debug, getEnvValues(), "-t", filePath, "-u", "input", "-code", "-esc")
103
if err != nil {
104
return err
105
}
106
if osutils.IsLinux() {
107
return expectResultsCount(results, 1)
108
} else {
109
return expectResultsCount(results, 0)
110
111
}
112
}
113
114
type codeFile struct{}
115
116
// Execute executes a test case and returns an error if occurred
117
func (h *codeFile) Execute(filePath string) error {
118
results, err := testutils.RunNucleiArgsWithEnvAndGetResults(debug, getEnvValues(), "-t", filePath, "-u", "input", "-code")
119
if err != nil {
120
return err
121
}
122
return expectResultsCount(results, 1)
123
}
124
125
type codeEnvVar struct{}
126
127
// Execute executes a test case and returns an error if occurred
128
func (h *codeEnvVar) Execute(filePath string) error {
129
results, err := testutils.RunNucleiArgsWithEnvAndGetResults(debug, getEnvValues(), "-t", filePath, "-u", "input", "-V", "baz=baz", "-code")
130
if err != nil {
131
return err
132
}
133
return expectResultsCount(results, 1)
134
}
135
136
type unsignedCode struct{}
137
138
// Execute executes a test case and returns an error if occurred
139
func (h *unsignedCode) Execute(filePath string) error {
140
results, err := testutils.RunNucleiArgsWithEnvAndGetResults(debug, getEnvValues(), "-t", filePath, "-u", "input", "-code")
141
142
// should error out
143
if err != nil {
144
return nil
145
}
146
147
// this point should never be reached
148
return errors.Join(expectResultsCount(results, 1), errors.New("unsigned template was executed"))
149
}
150
151
type codePyNoSig struct{}
152
153
// Execute executes a test case and returns an error if occurred
154
func (h *codePyNoSig) Execute(filePath string) error {
155
results, err := testutils.RunNucleiArgsWithEnvAndGetResults(debug, getEnvValues(), "-t", filePath, "-u", "input", "-code")
156
157
// should error out
158
if err != nil {
159
return nil
160
}
161
162
// this point should never be reached
163
return errors.Join(expectResultsCount(results, 1), errors.New("unsigned template was executed"))
164
}
165
166