Tarun Koyalwar e1d3f474a4
support for dynamic variables in template context (multi protocol execution) (#3672)
* multi proto request genesis

* adds template context dynamic vars

* feat: proto level resp variables

* remove proto prefix hacky logic

* implement template ctx args

* remove old var name logic

* improve AddTemplateVars func

* add multi proto comments+docs

* vardump with sorted keys

* fix race condition in ctx args

* default initialize ctx args

* use generic map

* index variables with multiple values

* fix nil cookies

* use synclock map

* fix build failure

* fix lint error

* resolve merge conflicts

* multi proto: add unit+ integration tests

* fix unit tests

* Issue 3339 headless fuzz (#3790)

* Basic headless fuzzing

* Remove debug statements

* Add integration tests

* Update template

* Fix recognize payload value in matcher

* Update tempalte

* use req.SetURL()

---------

Co-authored-by: Tarun Koyalwar <tarun@projectdiscovery.io>

* Auto Generate Syntax Docs + JSONSchema [Fri Jun  9 00:23:32 UTC 2023] 🤖

* Add headless header and status matchers (#3794)

* add headless header and status matchers

* rename headers as header

* add integration test for header+status

* fix typo

---------

Co-authored-by: Shubham Rasal <shubham@projectdiscovery.io>
Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: Dogan Can Bakir <65292895+dogancanbakir@users.noreply.github.com>
2023-06-09 19:52:56 +05:30

83 lines
2.1 KiB
Go

package contextargs
import (
"net/http/cookiejar"
"github.com/projectdiscovery/gologger"
maputils "github.com/projectdiscovery/utils/maps"
)
// Context implements a shared context struct to share information across multiple templates within a workflow
type Context struct {
// Meta is the target for the executor
MetaInput *MetaInput
// CookieJar shared within workflow's http templates
CookieJar *cookiejar.Jar
// Args is a workflow shared key-value store
args maputils.SyncLockMap[string, interface{}]
}
// Create a new contextargs instance
func New() *Context {
return NewWithInput("")
}
// Create a new contextargs instance with input string
func NewWithInput(input string) *Context {
jar, err := cookiejar.New(nil)
if err != nil {
gologger.Error().Msgf("Could not create cookie jar: %s\n", err)
}
return &Context{MetaInput: &MetaInput{Input: input}, CookieJar: jar, args: maputils.SyncLockMap[string, interface{}]{
Map: make(map[string]interface{}),
}}
}
// Set the specific key-value pair
func (ctx *Context) Set(key string, value interface{}) {
if err := ctx.args.Set(key, value); err != nil {
gologger.Error().Msgf("contextargs: could not set key: %s\n", err)
}
}
// Get the value with specific key if exists
func (ctx *Context) Get(key string) (interface{}, bool) {
return ctx.args.Get(key)
}
func (ctx *Context) GetAll() maputils.Map[string, interface{}] {
return ctx.args.GetAll()
}
func (ctx *Context) ForEach(f func(string, interface{}) error) {
if err := ctx.args.Iterate(f); err != nil {
gologger.Error().Msgf("contextargs: could not iterate: %s\n", err)
}
}
// Merge merges the map into the contextargs
func (ctx *Context) Merge(m map[string]interface{}) {
if err := ctx.args.Merge(m); err != nil {
gologger.Error().Msgf("contextargs: could not merge: %s\n", err)
}
}
// Has check if the key exists
func (ctx *Context) Has(key string) bool {
return ctx.args.Has(key)
}
func (ctx *Context) HasArgs() bool {
return !ctx.args.IsEmpty()
}
func (ctx *Context) Clone() *Context {
newCtx := &Context{
MetaInput: ctx.MetaInput.Clone(),
args: *ctx.args.Clone(),
CookieJar: ctx.CookieJar,
}
return newCtx
}