2024-03-14 03:08:53 +05:30
|
|
|
package component
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-03-25 10:08:26 +05:30
|
|
|
"fmt"
|
2024-03-14 03:08:53 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
2024-03-25 10:08:26 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/dataformat"
|
2024-03-14 03:08:53 +05:30
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2024-03-25 10:08:26 +05:30
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2024-03-14 03:08:53 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Cookie is a component for a request cookie
|
|
|
|
|
type Cookie struct {
|
|
|
|
|
value *Value
|
|
|
|
|
|
|
|
|
|
req *retryablehttp.Request
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ Component = &Cookie{}
|
|
|
|
|
|
|
|
|
|
// NewCookie creates a new cookie component
|
|
|
|
|
func NewCookie() *Cookie {
|
|
|
|
|
return &Cookie{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the name of the component
|
|
|
|
|
func (c *Cookie) Name() string {
|
|
|
|
|
return RequestCookieComponent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse parses the component and returns the
|
|
|
|
|
// parsed component
|
|
|
|
|
func (c *Cookie) Parse(req *retryablehttp.Request) (bool, error) {
|
|
|
|
|
if len(req.Cookies()) == 0 {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
c.req = req
|
|
|
|
|
c.value = NewValue("")
|
|
|
|
|
|
2024-03-25 10:08:26 +05:30
|
|
|
parsedCookies := mapsutil.NewOrderedMap[string, any]()
|
2024-03-14 03:08:53 +05:30
|
|
|
for _, cookie := range req.Cookies() {
|
2024-03-25 10:08:26 +05:30
|
|
|
parsedCookies.Set(cookie.Name, cookie.Value)
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-03-25 10:08:26 +05:30
|
|
|
if parsedCookies.Len() == 0 {
|
2024-03-14 03:08:53 +05:30
|
|
|
return false, nil
|
|
|
|
|
}
|
2024-03-25 10:08:26 +05:30
|
|
|
c.value.SetParsed(dataformat.KVOrderedMap(&parsedCookies), "")
|
2024-03-14 03:08:53 +05:30
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Iterate iterates through the component
|
2024-03-25 10:08:26 +05:30
|
|
|
func (c *Cookie) Iterate(callback func(key string, value interface{}) error) (err error) {
|
|
|
|
|
c.value.parsed.Iterate(func(key string, value any) bool {
|
|
|
|
|
if errx := callback(key, value); errx != nil {
|
|
|
|
|
err = errx
|
|
|
|
|
return false
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-03-25 10:08:26 +05:30
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
return
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetValue sets a value in the component
|
|
|
|
|
// for a key
|
|
|
|
|
func (c *Cookie) SetValue(key string, value string) error {
|
|
|
|
|
if !c.value.SetParsedValue(key, value) {
|
|
|
|
|
return ErrSetValue
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete deletes a key from the component
|
|
|
|
|
func (c *Cookie) Delete(key string) error {
|
|
|
|
|
if !c.value.Delete(key) {
|
|
|
|
|
return ErrKeyNotFound
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rebuild returns a new request with the
|
|
|
|
|
// component rebuilt
|
|
|
|
|
func (c *Cookie) Rebuild() (*retryablehttp.Request, error) {
|
2025-02-13 18:46:28 +05:30
|
|
|
// TODO: Fix cookie duplication with auth-file
|
2024-03-14 03:08:53 +05:30
|
|
|
cloned := c.req.Clone(context.Background())
|
|
|
|
|
|
|
|
|
|
cloned.Header.Del("Cookie")
|
2024-03-25 10:08:26 +05:30
|
|
|
c.value.parsed.Iterate(func(key string, value any) bool {
|
2024-03-14 03:08:53 +05:30
|
|
|
cookie := &http.Cookie{
|
|
|
|
|
Name: key,
|
2024-03-25 10:08:26 +05:30
|
|
|
Value: fmt.Sprint(value), // Assume the value is always a string for cookies
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
cloned.AddCookie(cookie)
|
2024-03-25 10:08:26 +05:30
|
|
|
return true
|
|
|
|
|
})
|
2024-03-14 03:08:53 +05:30
|
|
|
return cloned, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-29 13:31:30 +05:30
|
|
|
// Clone clones current state of this component
|
|
|
|
|
func (c *Cookie) Clone() Component {
|
|
|
|
|
return &Cookie{
|
|
|
|
|
value: c.value.Clone(),
|
|
|
|
|
req: c.req.Clone(context.Background()),
|
|
|
|
|
}
|
|
|
|
|
}
|