Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/fuzz/component/cookie.go
2070 views
1
package component
2
3
import (
4
"context"
5
"fmt"
6
"net/http"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/dataformat"
9
"github.com/projectdiscovery/retryablehttp-go"
10
mapsutil "github.com/projectdiscovery/utils/maps"
11
)
12
13
// Cookie is a component for a request cookie
14
type Cookie struct {
15
value *Value
16
17
req *retryablehttp.Request
18
}
19
20
var _ Component = &Cookie{}
21
22
// NewCookie creates a new cookie component
23
func NewCookie() *Cookie {
24
return &Cookie{}
25
}
26
27
// Name returns the name of the component
28
func (c *Cookie) Name() string {
29
return RequestCookieComponent
30
}
31
32
// Parse parses the component and returns the
33
// parsed component
34
func (c *Cookie) Parse(req *retryablehttp.Request) (bool, error) {
35
if len(req.Cookies()) == 0 {
36
return false, nil
37
}
38
c.req = req
39
c.value = NewValue("")
40
41
parsedCookies := mapsutil.NewOrderedMap[string, any]()
42
for _, cookie := range req.Cookies() {
43
parsedCookies.Set(cookie.Name, cookie.Value)
44
}
45
if parsedCookies.Len() == 0 {
46
return false, nil
47
}
48
c.value.SetParsed(dataformat.KVOrderedMap(&parsedCookies), "")
49
return true, nil
50
}
51
52
// Iterate iterates through the component
53
func (c *Cookie) Iterate(callback func(key string, value interface{}) error) (err error) {
54
c.value.parsed.Iterate(func(key string, value any) bool {
55
if errx := callback(key, value); errx != nil {
56
err = errx
57
return false
58
}
59
return true
60
})
61
return
62
}
63
64
// SetValue sets a value in the component
65
// for a key
66
func (c *Cookie) SetValue(key string, value string) error {
67
if !c.value.SetParsedValue(key, value) {
68
return ErrSetValue
69
}
70
return nil
71
}
72
73
// Delete deletes a key from the component
74
func (c *Cookie) Delete(key string) error {
75
if !c.value.Delete(key) {
76
return ErrKeyNotFound
77
}
78
return nil
79
}
80
81
// Rebuild returns a new request with the
82
// component rebuilt
83
func (c *Cookie) Rebuild() (*retryablehttp.Request, error) {
84
// TODO: Fix cookie duplication with auth-file
85
cloned := c.req.Clone(context.Background())
86
87
cloned.Header.Del("Cookie")
88
c.value.parsed.Iterate(func(key string, value any) bool {
89
cookie := &http.Cookie{
90
Name: key,
91
Value: fmt.Sprint(value), // Assume the value is always a string for cookies
92
}
93
cloned.AddCookie(cookie)
94
return true
95
})
96
return cloned, nil
97
}
98
99
// Clone clones current state of this component
100
func (c *Cookie) Clone() Component {
101
return &Cookie{
102
value: c.value.Clone(),
103
req: c.req.Clone(context.Background()),
104
}
105
}
106
107