Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/catalog/config/template_test.go
4538 views
1
package config
2
3
import (
4
"os"
5
"path/filepath"
6
"testing"
7
8
osutils "github.com/projectdiscovery/utils/os"
9
"github.com/stretchr/testify/require"
10
)
11
12
func toAbs(p string) string {
13
if osutils.IsWindows() {
14
// Infer the drive letter from the Windows system path
15
// SystemRoot or WINDIR typically points to something like "C:\Windows"
16
systemRoot := os.Getenv("SystemRoot")
17
if systemRoot == "" {
18
systemRoot = os.Getenv("WINDIR")
19
}
20
// Extract volume name (e.g., "C:") from the system path
21
volumeName := filepath.VolumeName(systemRoot)
22
if volumeName == "" {
23
// Fallback to C: if we can't determine the volume
24
volumeName = "C:"
25
}
26
return filepath.FromSlash(volumeName + p)
27
}
28
return filepath.FromSlash(p)
29
}
30
31
func TestIsTemplate(t *testing.T) {
32
tests := []struct {
33
name string
34
fpath string
35
rootDir string
36
want bool
37
}{
38
{
39
name: "valid template",
40
fpath: "dns/cname.yaml",
41
rootDir: "",
42
want: true,
43
},
44
{
45
name: "excluded directory relative",
46
fpath: "helpers/data.txt",
47
rootDir: "",
48
want: false,
49
},
50
{
51
name: "excluded directory .git",
52
fpath: ".git/config",
53
rootDir: "",
54
want: false,
55
},
56
{
57
name: "absolute path with excluded parent dir (bug fix)",
58
fpath: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates/dns/cname.yaml"),
59
rootDir: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates"),
60
want: true,
61
},
62
{
63
name: "absolute path with excluded dir inside root",
64
fpath: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates/helpers/data.txt"),
65
rootDir: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates"),
66
want: false,
67
},
68
{
69
name: "absolute path without root (skip check)",
70
fpath: toAbs("/opt/helpers/foo.yaml"),
71
rootDir: "",
72
want: true,
73
},
74
{
75
name: "valid template with different extension",
76
fpath: "dns/cname.json",
77
rootDir: "",
78
want: true,
79
},
80
{
81
name: "invalid extension",
82
fpath: "dns/cname.txt",
83
rootDir: "",
84
want: false,
85
},
86
{
87
name: "excluded config file",
88
fpath: "cves.json",
89
rootDir: "",
90
want: false,
91
},
92
}
93
94
for _, tt := range tests {
95
t.Run(tt.name, func(t *testing.T) {
96
got := IsTemplateWithRoot(tt.fpath, tt.rootDir)
97
require.Equal(t, tt.want, got, "IsTemplateWithRoot(%q, %q)", tt.fpath, tt.rootDir)
98
})
99
}
100
}
101
102