Repository navigation
Expand file tree
/
Copy pathclone_test.go
More file actions
134 lines (110 loc) · 4.25 KB
/
Copy pathclone_test.go
File metadata and controls
134 lines (110 loc) · 4.25 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package templates
import (
"os"
"path/filepath"
"testing"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/disk"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
"github.com/stretchr/testify/require"
)
func TestCloneTemplatePreservesCleanTemplateIsolation(t *testing.T) {
templatePath := filepath.Join(t.TempDir(), "multi-protocol.yaml")
require.NoError(t, os.WriteFile(templatePath, []byte(`id: clone-isolation
info:
name: Clone isolation
author: pdteam
severity: info
variables:
shared: original
dns:
- name: "{{FQDN}}"
type: A
http:
- method: GET
path:
- "{{BaseURL}}"
matchers:
- type: word
words:
- original
`), 0o600))
parsed, err := NewParser().ParseTemplate(templatePath, disk.NewCatalog(""))
require.NoError(t, err)
original := parsed.(*Template)
cloned := cloneTemplate(original)
require.NotSame(t, original, cloned)
require.NotSame(t, original.RequestsDNS[0], cloned.RequestsDNS[0])
require.NotSame(t, original.RequestsHTTP[0], cloned.RequestsHTTP[0])
require.Same(t, cloned.RequestsDNS[0], cloned.RequestsQueue[0])
require.Same(t, cloned.RequestsHTTP[0], cloned.RequestsQueue[1])
cloned.RequestsHTTP[0].Matchers[0].Words[0] = "changed"
cloned.Variables.Set("shared", "changed")
require.Equal(t, "original", original.RequestsHTTP[0].Matchers[0].Words[0])
require.Equal(t, map[string]interface{}{"shared": "original"}, original.Variables.GetAll())
}
func TestCloneTemplatePreservesMutableAliases(t *testing.T) {
sharedMap := map[string]interface{}{"value": "original"}
sharedSlice := []interface{}{"original"}
original := &Template{
Constants: map[string]interface{}{
"map": sharedMap,
"first-slice": sharedSlice,
"next-slice": sharedSlice,
},
}
original.Variables.InsertionOrderedStringMap = *utils.NewEmptyInsertionOrderedStringMap(1)
original.Variables.Set("map", sharedMap)
cloned := cloneTemplate(original)
clonedMap := cloned.Constants["map"].(map[string]interface{})
clonedVariableMap := cloned.Variables.GetAll()["map"].(map[string]interface{})
clonedFirstSlice := cloned.Constants["first-slice"].([]interface{})
clonedNextSlice := cloned.Constants["next-slice"].([]interface{})
clonedMap["value"] = "changed"
clonedFirstSlice[0] = "changed"
require.Equal(t, "changed", clonedVariableMap["value"])
require.Equal(t, "changed", clonedNextSlice[0])
require.Equal(t, "original", sharedMap["value"])
require.Equal(t, "original", sharedSlice[0])
}
func TestCloneTemplateKeepsExecutorOptionsByReference(t *testing.T) {
parser := NewParser()
original := &Template{}
original.Options = &protocols.ExecutorOptions{Parser: parser}
cloned := cloneTemplate(original)
require.Same(t, original.Options, cloned.Options)
require.Same(t, parser, cloned.Options.Parser)
}
func TestCacheSafeExecutorOptionsPreservesMutableAliases(t *testing.T) {
sharedMap := map[string]interface{}{"value": "original"}
options := &protocols.ExecutorOptions{
Constants: map[string]interface{}{"map": sharedMap},
}
options.Variables.InsertionOrderedStringMap = *utils.NewEmptyInsertionOrderedStringMap(1)
options.Variables.Set("map", sharedMap)
safeOptions := cacheSafeExecutorOptions(options)
safeConstantMap := safeOptions.Constants["map"].(map[string]interface{})
safeVariableMap := safeOptions.Variables.GetAll()["map"].(map[string]interface{})
safeConstantMap["value"] = "changed"
require.Equal(t, "changed", safeVariableMap["value"])
require.Equal(t, "original", sharedMap["value"])
}
func TestCloneTemplateHandlesMutableCycles(t *testing.T) {
cyclicMap := make(map[string]interface{})
cyclicMap["self"] = cyclicMap
cyclicSlice := make([]interface{}, 1)
cyclicSlice[0] = cyclicSlice
original := &Template{Constants: map[string]interface{}{
"map": cyclicMap,
"slice": cyclicSlice,
}}
cloned := cloneTemplate(original)
clonedMap := cloned.Constants["map"].(map[string]interface{})
clonedMap["self"].(map[string]interface{})["changed"] = true
clonedSlice := cloned.Constants["slice"].([]interface{})
clonedSlice[0].([]interface{})[0] = "changed"
require.True(t, clonedMap["changed"].(bool))
require.Equal(t, "changed", clonedSlice[0])
require.NotContains(t, cyclicMap, "changed")
require.IsType(t, []interface{}{}, cyclicSlice[0])
}