2024-03-14 03:08:53 +05:30
|
|
|
package component
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-06-11 04:43:46 +05:30
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2024-03-14 03:08:53 +05:30
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz/dataformat"
|
|
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2024-08-28 14:11:02 +07:00
|
|
|
"github.com/projectdiscovery/utils/maps"
|
2024-06-11 04:43:46 +05:30
|
|
|
urlutil "github.com/projectdiscovery/utils/url"
|
2024-03-14 03:08:53 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Path is a component for a request Path
|
|
|
|
|
type Path struct {
|
|
|
|
|
value *Value
|
|
|
|
|
|
|
|
|
|
req *retryablehttp.Request
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ Component = &Path{}
|
|
|
|
|
|
|
|
|
|
// NewPath creates a new URL component
|
|
|
|
|
func NewPath() *Path {
|
|
|
|
|
return &Path{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the name of the component
|
|
|
|
|
func (q *Path) Name() string {
|
|
|
|
|
return RequestPathComponent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse parses the component and returns the
|
|
|
|
|
// parsed component
|
|
|
|
|
func (q *Path) Parse(req *retryablehttp.Request) (bool, error) {
|
|
|
|
|
q.req = req
|
2024-06-11 04:43:46 +05:30
|
|
|
q.value = NewValue("")
|
2024-03-14 03:08:53 +05:30
|
|
|
|
2024-06-11 04:43:46 +05:30
|
|
|
splitted := strings.Split(req.URL.Path, "/")
|
|
|
|
|
values := make(map[string]interface{})
|
|
|
|
|
for i := range splitted {
|
|
|
|
|
pathTillNow := strings.Join(splitted[:i+1], "/")
|
|
|
|
|
if pathTillNow == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
values[strconv.Itoa(i)] = pathTillNow
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-06-11 04:43:46 +05:30
|
|
|
q.value.SetParsed(dataformat.KVMap(values), "")
|
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 (q *Path) Iterate(callback func(key string, value interface{}) error) (err error) {
|
|
|
|
|
q.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 (q *Path) SetValue(key string, value string) error {
|
2024-06-11 04:43:46 +05:30
|
|
|
escaped := urlutil.ParamEncode(value)
|
|
|
|
|
if !q.value.SetParsedValue(key, escaped) {
|
2024-03-14 03:08:53 +05:30
|
|
|
return ErrSetValue
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete deletes a key from the component
|
|
|
|
|
func (q *Path) Delete(key string) error {
|
|
|
|
|
if !q.value.Delete(key) {
|
|
|
|
|
return ErrKeyNotFound
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rebuild returns a new request with the
|
|
|
|
|
// component rebuilt
|
|
|
|
|
func (q *Path) Rebuild() (*retryablehttp.Request, error) {
|
2024-08-28 14:11:02 +07:00
|
|
|
originalValues := mapsutil.Map[string, any]{}
|
2024-06-11 04:43:46 +05:30
|
|
|
splitted := strings.Split(q.req.URL.Path, "/")
|
|
|
|
|
for i := range splitted {
|
|
|
|
|
pathTillNow := strings.Join(splitted[:i+1], "/")
|
|
|
|
|
if pathTillNow == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
originalValues[strconv.Itoa(i)] = pathTillNow
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
originalPath := q.req.URL.Path
|
|
|
|
|
lengthSplitted := len(q.value.parsed.Map)
|
|
|
|
|
for i := lengthSplitted; i > 0; i-- {
|
|
|
|
|
key := strconv.Itoa(i)
|
2024-08-28 14:11:02 +07:00
|
|
|
|
|
|
|
|
original, ok := originalValues.GetOrDefault(key, "").(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new, ok := q.value.parsed.Map.GetOrDefault(key, "").(string)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if new == original {
|
|
|
|
|
// no need to replace
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 04:43:46 +05:30
|
|
|
originalPath = strings.Replace(originalPath, original, new, 1)
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2024-06-11 04:43:46 +05:30
|
|
|
|
|
|
|
|
rebuiltPath := originalPath
|
|
|
|
|
|
|
|
|
|
// Clone the request and update the path
|
2024-03-14 03:08:53 +05:30
|
|
|
cloned := q.req.Clone(context.Background())
|
2024-06-11 04:43:46 +05:30
|
|
|
if err := cloned.UpdateRelPath(rebuiltPath, true); err != nil {
|
|
|
|
|
cloned.URL.RawPath = rebuiltPath
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
|
|
|
|
return cloned, nil
|
|
|
|
|
}
|
2024-03-29 13:31:30 +05:30
|
|
|
|
|
|
|
|
// Clones current state to a new component
|
|
|
|
|
func (q *Path) Clone() Component {
|
|
|
|
|
return &Path{
|
|
|
|
|
value: q.value.Clone(),
|
|
|
|
|
req: q.req.Clone(context.Background()),
|
|
|
|
|
}
|
|
|
|
|
}
|