forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_test.go
120 lines (109 loc) · 4.29 KB
/
load_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package generators
import (
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"
osutils "github.com/projectdiscovery/utils/os"
"github.com/stretchr/testify/require"
)
func TestLoadPayloads(t *testing.T) {
// since we are changing value of global variable i.e templates directory
// run this test as subprocess
if os.Getenv("LOAD_PAYLOAD_NO_ACCESS") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=TestLoadPayloadsWithAccess")
cmd.Env = append(os.Environ(), "LOAD_PAYLOAD_NO_ACCESS=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
if err != nil {
t.Fatalf("process ran with err %v, want exit status 1", err)
}
}
templateDir := getTemplatesDir(t)
config.DefaultConfig.SetTemplatesDir(templateDir)
generator := &PayloadGenerator{catalog: disk.NewCatalog(templateDir), options: getOptions(false)}
fullpath := filepath.Join(templateDir, "payloads.txt")
// Test sandbox
t.Run("templates-directory", func(t *testing.T) {
// testcase when loading file from template directory and template file is in root
// expected to succeed
values, err := generator.loadPayloads(map[string]interface{}{
"new": fullpath,
}, "/test")
require.NoError(t, err, "could not load payloads")
require.Equal(t, map[string][]string{"new": {"test", "another"}}, values, "could not get values")
})
t.Run("templates-path-relative", func(t *testing.T) {
// testcase when loading file from template directory and template file is current working directory
// expected to fail since this is LFI
_, err := generator.loadPayloads(map[string]interface{}{
"new": "../../../../../../../../../etc/passwd",
}, ".")
require.Error(t, err, "could load payloads")
})
t.Run("template-directory", func(t *testing.T) {
// testcase when loading file from template directory and template file is inside template directory
// expected to succeed
values, err := generator.loadPayloads(map[string]interface{}{
"new": fullpath,
}, filepath.Join(templateDir, "test.yaml"))
require.NoError(t, err, "could not load payloads")
require.Equal(t, map[string][]string{"new": {"test", "another"}}, values, "could not get values")
})
t.Run("invalid", func(t *testing.T) {
// testcase when loading file from /etc/passwd and template file is at root i.e /
// expected to fail since this is LFI
values, err := generator.loadPayloads(map[string]interface{}{
"new": "/etc/passwd",
}, "/random")
require.Error(t, err, "could load payloads got %v", values)
require.Equal(t, 0, len(values), "could get values")
// testcase when loading file from template directory and template file is at root i.e /
// expected to succeed
values, err = generator.loadPayloads(map[string]interface{}{
"new": fullpath,
}, "/random")
require.NoError(t, err, "could load payloads %v", values)
require.Equal(t, 1, len(values), "could get values")
require.Equal(t, []string{"test", "another"}, values["new"], "could get values")
})
}
func TestLoadPayloadsWithAccess(t *testing.T) {
// since we are changing value of global variable i.e templates directory
// run this test as subprocess
if os.Getenv("LOAD_PAYLOAD_WITH_ACCESS") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=TestLoadPayloadsWithAccess")
cmd.Env = append(os.Environ(), "LOAD_PAYLOAD_WITH_ACCESS=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
return
}
if err != nil {
t.Fatalf("process ran with err %v, want exit status 1", err)
}
}
templateDir := getTemplatesDir(t)
config.DefaultConfig.SetTemplatesDir(templateDir)
generator := &PayloadGenerator{catalog: disk.NewCatalog(templateDir), options: getOptions(true)}
t.Run("no-sandbox-unix", func(t *testing.T) {
if osutils.IsWindows() {
return
}
_, err := generator.loadPayloads(map[string]interface{}{
"new": "/etc/passwd",
}, "/random")
require.NoError(t, err, "could load payloads")
})
}
func getTemplatesDir(t *testing.T) string {
tempdir, err := os.MkdirTemp("", "templates-*")
require.NoError(t, err, "could not create temp dir")
fullpath := filepath.Join(tempdir, "payloads.txt")
err = os.WriteFile(fullpath, []byte("test\nanother"), 0777)
require.NoError(t, err, "could not write payload")
return tempdir
}