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