2020-12-21 09:01:32 +00:00
|
|
|
package protocols
|
|
|
|
|
2020-12-23 15:16:42 +00:00
|
|
|
import (
|
2023-11-10 23:12:27 +00:00
|
|
|
"encoding/base64"
|
2023-08-31 12:33:01 +00:00
|
|
|
"sync/atomic"
|
|
|
|
|
2022-10-13 03:04:37 +00:00
|
|
|
"github.com/projectdiscovery/ratelimit"
|
2023-08-31 12:33:01 +00:00
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
2023-06-09 14:22:56 +00:00
|
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
2021-09-07 14:31:46 +00:00
|
|
|
|
2021-10-28 11:50:07 +00:00
|
|
|
"github.com/logrusorgru/aurora"
|
2021-11-25 15:09:20 +00:00
|
|
|
|
2024-03-13 21:38:53 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/authprovider"
|
2023-10-17 12:14:13 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/input"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/js/compiler"
|
2024-03-13 20:46:30 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/loader/parser"
|
2023-10-17 12:14:13 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/model"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/operators/extractors"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/operators/matchers"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/output"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/progress"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/projectfile"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/contextargs"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/hosterrorscache"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/interactsh"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/utils/excludematchers"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/variables"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/headless/engine"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/reporting"
|
2023-11-27 18:54:45 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/scan"
|
2023-10-17 12:14:13 +00:00
|
|
|
templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2020-12-23 15:16:42 +00:00
|
|
|
)
|
2020-12-23 10:46:16 +00:00
|
|
|
|
2024-02-01 20:35:30 +00:00
|
|
|
// Optional Callback to update Thread count in payloads across all requests
|
|
|
|
type PayloadThreadSetterCallback func(opts *ExecutorOptions, totalRequests, currentThreads int) int
|
|
|
|
|
|
|
|
var (
|
|
|
|
MaxTemplateFileSizeForEncoding = 1024 * 1024
|
|
|
|
)
|
2023-11-10 23:12:27 +00:00
|
|
|
|
2020-12-25 15:03:52 +00:00
|
|
|
// Executer is an interface implemented any protocol based request executer.
|
2020-12-23 15:16:42 +00:00
|
|
|
type Executer interface {
|
2020-12-25 15:03:52 +00:00
|
|
|
// Compile compiles the execution generators preparing any requests possible.
|
2020-12-25 15:09:09 +00:00
|
|
|
Compile() error
|
2020-12-21 22:24:55 +00:00
|
|
|
// Requests returns the total number of requests the rule will perform
|
2020-12-29 10:08:14 +00:00
|
|
|
Requests() int
|
2020-12-25 15:03:52 +00:00
|
|
|
// Execute executes the protocol group and returns true or false if results were found.
|
2023-11-27 18:54:45 +00:00
|
|
|
Execute(ctx *scan.ScanContext) (bool, error)
|
2020-12-23 15:16:42 +00:00
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2023-11-27 18:54:45 +00:00
|
|
|
ExecuteWithResults(ctx *scan.ScanContext) ([]*output.ResultEvent, error)
|
2020-12-21 09:01:32 +00:00
|
|
|
}
|
2020-12-23 10:46:16 +00:00
|
|
|
|
2023-05-31 20:58:10 +00:00
|
|
|
// ExecutorOptions contains the configuration options for executer clients
|
|
|
|
type ExecutorOptions struct {
|
2020-12-24 20:54:55 +00:00
|
|
|
// TemplateID is the ID of the template for the request
|
|
|
|
TemplateID string
|
2020-12-29 06:38:46 +00:00
|
|
|
// TemplatePath is the path of the template for the request
|
|
|
|
TemplatePath string
|
2020-12-24 20:54:55 +00:00
|
|
|
// TemplateInfo contains information block of the template request
|
2021-07-12 14:20:01 +00:00
|
|
|
TemplateInfo model.Info
|
2023-11-10 23:12:27 +00:00
|
|
|
// RawTemplate is the raw template for the request
|
|
|
|
RawTemplate []byte
|
2020-12-23 15:16:42 +00:00
|
|
|
// Output is a writer interface for writing output events from executer.
|
|
|
|
Output output.Writer
|
2020-12-23 20:12:04 +00:00
|
|
|
// Options contains configuration options for the executer.
|
2020-12-23 15:16:42 +00:00
|
|
|
Options *types.Options
|
2021-02-02 06:40:47 +00:00
|
|
|
// IssuesClient is a client for nuclei issue tracker reporting
|
2023-02-07 08:45:49 +00:00
|
|
|
IssuesClient reporting.Client
|
2020-12-25 15:03:52 +00:00
|
|
|
// Progress is a progress client for scan reporting
|
2021-03-09 11:49:03 +00:00
|
|
|
Progress progress.Progress
|
2020-12-23 20:12:04 +00:00
|
|
|
// RateLimiter is a rate-limiter for limiting sent number of requests.
|
2022-09-19 11:39:28 +00:00
|
|
|
RateLimiter *ratelimit.Limiter
|
2021-02-26 07:43:11 +00:00
|
|
|
// Catalog is a template catalog implementation for nuclei
|
2022-08-10 18:05:58 +00:00
|
|
|
Catalog catalog.Catalog
|
2020-12-28 20:00:07 +00:00
|
|
|
// ProjectFile is the project file for nuclei
|
|
|
|
ProjectFile *projectfile.ProjectFile
|
2021-02-21 11:01:34 +00:00
|
|
|
// Browser is a browser engine for running headless templates
|
|
|
|
Browser *engine.Browser
|
2021-04-16 11:26:41 +00:00
|
|
|
// Interactsh is a client for interactsh oob polling server
|
|
|
|
Interactsh *interactsh.Client
|
2021-08-16 15:54:37 +00:00
|
|
|
// HostErrorsCache is an optional cache for handling host errors
|
2022-07-18 20:35:53 +00:00
|
|
|
HostErrorsCache hosterrorscache.CacheInterface
|
2022-12-19 14:22:17 +00:00
|
|
|
// Stop execution once first match is found (Assigned while parsing templates)
|
|
|
|
// Note: this is different from Options.StopAtFirstMatch (Assigned from CLI option)
|
2021-11-29 10:31:06 +00:00
|
|
|
StopAtFirstMatch bool
|
2022-03-30 19:24:35 +00:00
|
|
|
// Variables is a list of variables from template
|
|
|
|
Variables variables.Variable
|
2023-05-25 16:32:35 +00:00
|
|
|
// Constants is a list of constants from template
|
|
|
|
Constants map[string]interface{}
|
2022-06-24 17:39:27 +00:00
|
|
|
// ExcludeMatchers is the list of matchers to exclude
|
|
|
|
ExcludeMatchers *excludematchers.ExcludeMatchers
|
2022-10-20 11:53:00 +00:00
|
|
|
// InputHelper is a helper for input normalization
|
|
|
|
InputHelper *input.Helper
|
2021-02-05 19:06:43 +00:00
|
|
|
|
|
|
|
Operators []*operators.Operators // only used by offlinehttp module
|
2021-07-15 10:41:41 +00:00
|
|
|
|
2023-02-13 12:30:25 +00:00
|
|
|
// DoNotCache bool disables optional caching of the templates structure
|
|
|
|
DoNotCache bool
|
|
|
|
|
2021-10-28 11:50:07 +00:00
|
|
|
Colorizer aurora.Aurora
|
2021-07-15 10:41:41 +00:00
|
|
|
WorkflowLoader model.WorkflowLoader
|
2021-11-29 13:38:45 +00:00
|
|
|
ResumeCfg *types.ResumeCfg
|
2023-06-09 14:22:56 +00:00
|
|
|
// ProtocolType is the type of the template
|
|
|
|
ProtocolType templateTypes.ProtocolType
|
2023-08-31 12:33:01 +00:00
|
|
|
// Flow is execution flow for the template (written in javascript)
|
|
|
|
Flow string
|
|
|
|
// IsMultiProtocol is true if template has more than one protocol
|
|
|
|
IsMultiProtocol bool
|
|
|
|
// templateStore is a map which contains template context for each scan (i.e input * template-id pair)
|
|
|
|
templateCtxStore *mapsutil.SyncLockMap[string, *contextargs.Context]
|
2023-09-16 10:32:17 +00:00
|
|
|
// JsCompiler is abstracted javascript compiler which adds node modules and provides execution
|
|
|
|
// environment for javascript templates
|
|
|
|
JsCompiler *compiler.Compiler
|
2024-02-01 20:35:30 +00:00
|
|
|
// Optional Callback function to update Thread count in payloads across all protocols
|
|
|
|
// based on given logic. by default nuclei reverts to using value of `-c` when threads count
|
|
|
|
// is not specified or is 0 in template
|
|
|
|
OverrideThreadsCount PayloadThreadSetterCallback
|
2024-03-13 21:38:53 +00:00
|
|
|
// AuthProvider is a provider for auth strategies
|
|
|
|
AuthProvider authprovider.AuthProvider
|
2024-03-07 12:57:38 +00:00
|
|
|
//TemporaryDirectory is the directory to store temporary files
|
|
|
|
TemporaryDirectory string
|
2024-03-13 20:46:30 +00:00
|
|
|
Parser parser.Parser
|
2024-03-30 18:20:31 +00:00
|
|
|
// ExportReqURLPattern exports the request URL pattern
|
|
|
|
// in ResultEvent it contains the exact url pattern (ex: {{BaseURL}}/{{randstr}}/xyz) used in the request
|
|
|
|
ExportReqURLPattern bool
|
2024-02-01 20:35:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 17:40:09 +00:00
|
|
|
// todo: centralizing components is not feasible with current clogged architecture
|
|
|
|
// a possible approach could be an internal event bus with pub-subs? This would be less invasive than
|
|
|
|
// reworking dep injection from scratch
|
|
|
|
func (eo *ExecutorOptions) RateLimitTake() {
|
|
|
|
if eo.RateLimiter.GetLimit() != uint(eo.Options.RateLimit) {
|
|
|
|
eo.RateLimiter.SetLimit(uint(eo.Options.RateLimit))
|
|
|
|
eo.RateLimiter.SetDuration(eo.Options.RateLimitDuration)
|
|
|
|
}
|
|
|
|
eo.RateLimiter.Take()
|
|
|
|
}
|
|
|
|
|
2024-02-01 20:35:30 +00:00
|
|
|
// GetThreadsForPayloadRequests returns the number of threads to use as default for
|
|
|
|
// given max-request of payloads
|
|
|
|
func (e *ExecutorOptions) GetThreadsForNPayloadRequests(totalRequests int, currentThreads int) int {
|
|
|
|
if e.OverrideThreadsCount != nil {
|
|
|
|
return e.OverrideThreadsCount(e, totalRequests, currentThreads)
|
|
|
|
}
|
2024-02-12 19:50:19 +00:00
|
|
|
if currentThreads > 0 {
|
2024-02-01 20:35:30 +00:00
|
|
|
return currentThreads
|
2024-02-12 19:50:19 +00:00
|
|
|
} else {
|
2024-03-13 15:05:19 +00:00
|
|
|
return e.Options.PayloadConcurrency
|
2024-02-01 20:35:30 +00:00
|
|
|
}
|
2023-08-31 12:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTemplateCtxStore creates template context store (which contains templateCtx for every scan)
|
|
|
|
func (e *ExecutorOptions) CreateTemplateCtxStore() {
|
|
|
|
e.templateCtxStore = &mapsutil.SyncLockMap[string, *contextargs.Context]{
|
|
|
|
Map: make(map[string]*contextargs.Context),
|
|
|
|
ReadOnly: atomic.Bool{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTemplateCtx removes template context of given scan from store
|
|
|
|
func (e *ExecutorOptions) RemoveTemplateCtx(input *contextargs.MetaInput) {
|
|
|
|
scanId := input.GetScanHash(e.TemplateID)
|
|
|
|
if e.templateCtxStore != nil {
|
|
|
|
e.templateCtxStore.Delete(scanId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 00:23:42 +00:00
|
|
|
// HasTemplateCtx returns true if template context exists for given input
|
|
|
|
func (e *ExecutorOptions) HasTemplateCtx(input *contextargs.MetaInput) bool {
|
|
|
|
scanId := input.GetScanHash(e.TemplateID)
|
|
|
|
if e.templateCtxStore != nil {
|
|
|
|
return e.templateCtxStore.Has(scanId)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-08-31 12:33:01 +00:00
|
|
|
// GetTemplateCtx returns template context for given input
|
|
|
|
func (e *ExecutorOptions) GetTemplateCtx(input *contextargs.MetaInput) *contextargs.Context {
|
|
|
|
scanId := input.GetScanHash(e.TemplateID)
|
|
|
|
templateCtx, ok := e.templateCtxStore.Get(scanId)
|
|
|
|
if !ok {
|
|
|
|
// if template context does not exist create new and add it to store and return it
|
|
|
|
templateCtx = contextargs.New()
|
2024-01-18 00:23:42 +00:00
|
|
|
templateCtx.MetaInput = input
|
2023-08-31 12:33:01 +00:00
|
|
|
_ = e.templateCtxStore.Set(scanId, templateCtx)
|
|
|
|
}
|
|
|
|
return templateCtx
|
2023-06-09 14:22:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddTemplateVars adds vars to template context with given template type as prefix
|
|
|
|
// this method is no-op if template is not multi protocol
|
2023-08-31 12:33:01 +00:00
|
|
|
func (e *ExecutorOptions) AddTemplateVars(input *contextargs.MetaInput, reqType templateTypes.ProtocolType, reqID string, vars map[string]interface{}) {
|
|
|
|
// if we wan't to disable adding response variables and other variables to template context
|
|
|
|
// this is the statement that does it . template context is currently only enabled for
|
|
|
|
// multiprotocol and flow templates
|
|
|
|
if !e.IsMultiProtocol && e.Flow == "" {
|
|
|
|
// no-op if not multi protocol template or flow template
|
2023-06-09 14:22:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-31 12:33:01 +00:00
|
|
|
templateCtx := e.GetTemplateCtx(input)
|
2023-06-09 14:22:56 +00:00
|
|
|
for k, v := range vars {
|
|
|
|
if !stringsutil.EqualFoldAny(k, "template-id", "template-info", "template-path") {
|
2023-08-31 12:33:01 +00:00
|
|
|
if reqID != "" {
|
|
|
|
k = reqID + "_" + k
|
|
|
|
} else if reqType < templateTypes.InvalidProtocol {
|
|
|
|
k = reqType.String() + "_" + k
|
2023-06-09 14:22:56 +00:00
|
|
|
}
|
2023-08-31 12:33:01 +00:00
|
|
|
templateCtx.Set(k, v)
|
2023-06-09 14:22:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTemplateVar adds given var to template context with given template type as prefix
|
|
|
|
// this method is no-op if template is not multi protocol
|
2023-08-31 12:33:01 +00:00
|
|
|
func (e *ExecutorOptions) AddTemplateVar(input *contextargs.MetaInput, templateType templateTypes.ProtocolType, reqID string, key string, value interface{}) {
|
|
|
|
if !e.IsMultiProtocol && e.Flow == "" {
|
|
|
|
// no-op if not multi protocol template or flow template
|
2023-06-09 14:22:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-08-31 12:33:01 +00:00
|
|
|
templateCtx := e.GetTemplateCtx(input)
|
|
|
|
if reqID != "" {
|
|
|
|
key = reqID + "_" + key
|
|
|
|
} else if templateType < templateTypes.InvalidProtocol {
|
|
|
|
key = templateType.String() + "_" + key
|
2023-06-09 14:22:56 +00:00
|
|
|
}
|
2023-08-31 12:33:01 +00:00
|
|
|
templateCtx.Set(key, value)
|
2020-12-23 10:46:16 +00:00
|
|
|
}
|
2020-12-25 15:03:52 +00:00
|
|
|
|
2021-10-27 11:20:36 +00:00
|
|
|
// Copy returns a copy of the executeroptions structure
|
2023-05-31 20:58:10 +00:00
|
|
|
func (e ExecutorOptions) Copy() ExecutorOptions {
|
2021-10-27 11:20:36 +00:00
|
|
|
copy := e
|
2023-08-31 12:33:01 +00:00
|
|
|
copy.CreateTemplateCtxStore()
|
2021-10-27 11:20:36 +00:00
|
|
|
return copy
|
|
|
|
}
|
|
|
|
|
2020-12-25 15:03:52 +00:00
|
|
|
// Request is an interface implemented any protocol based request generator.
|
|
|
|
type Request interface {
|
|
|
|
// Compile compiles the request generators preparing any requests possible.
|
2023-05-31 20:58:10 +00:00
|
|
|
Compile(options *ExecutorOptions) error
|
2020-12-25 15:03:52 +00:00
|
|
|
// Requests returns the total number of requests the rule will perform
|
2020-12-29 10:08:14 +00:00
|
|
|
Requests() int
|
2021-01-16 08:40:24 +00:00
|
|
|
// GetID returns the ID for the request if any. IDs are used for multi-request
|
|
|
|
// condition matching. So, two requests can be sent and their match can
|
|
|
|
// be evaluated from the third request by using the IDs for both requests.
|
|
|
|
GetID() string
|
2021-09-29 16:43:46 +00:00
|
|
|
// Match performs matching operation for a matcher on model and returns:
|
|
|
|
// true and a list of matched snippets if the matcher type is supports it
|
|
|
|
// otherwise false and an empty string slice
|
|
|
|
Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string)
|
2021-09-07 14:31:46 +00:00
|
|
|
// Extract performs extracting operation for an extractor on model and returns true or false.
|
2020-12-25 15:03:52 +00:00
|
|
|
Extract(data map[string]interface{}, matcher *extractors.Extractor) map[string]struct{}
|
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2022-10-03 10:12:20 +00:00
|
|
|
ExecuteWithResults(input *contextargs.Context, dynamicValues, previous output.InternalEvent, callback OutputEventCallback) error
|
2021-10-06 18:53:03 +00:00
|
|
|
// MakeResultEventItem creates a result event from internal wrapped event. Intended to be used by MakeResultEventItem internally
|
|
|
|
MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent
|
|
|
|
// MakeResultEvent creates a flat list of result events from an internal wrapped event, based on successful matchers and extracted data
|
2021-10-01 13:52:38 +00:00
|
|
|
MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent
|
|
|
|
// GetCompiledOperators returns a list of the compiled operators
|
|
|
|
GetCompiledOperators() []*operators.Operators
|
2021-11-04 21:31:41 +00:00
|
|
|
// Type returns the type of the protocol request
|
|
|
|
Type() templateTypes.ProtocolType
|
2020-12-25 15:03:52 +00:00
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
|
|
|
|
// OutputEventCallback is a callback event for any results found during scanning.
|
|
|
|
type OutputEventCallback func(result *output.InternalWrappedEvent)
|
2021-10-06 18:53:03 +00:00
|
|
|
|
|
|
|
func MakeDefaultResultEvent(request Request, wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
|
2024-01-10 11:04:40 +00:00
|
|
|
// Note: operator result is generated if something was succesfull match/extract/dynamic-extract
|
|
|
|
// but results should not be generated if
|
|
|
|
// 1. no match was found and some dynamic values were extracted
|
|
|
|
// 2. if something was extracted (matchers exist but no match was found)
|
2021-10-06 18:53:03 +00:00
|
|
|
if len(wrapped.OperatorsResult.DynamicValues) > 0 && !wrapped.OperatorsResult.Matched {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-10 11:04:40 +00:00
|
|
|
// check if something was extracted (except dynamic values)
|
|
|
|
extracted := len(wrapped.OperatorsResult.Extracts) > 0 || len(wrapped.OperatorsResult.OutputExtracts) > 0
|
|
|
|
if extracted && len(wrapped.OperatorsResult.Operators.Matchers) > 0 && !wrapped.OperatorsResult.Matched {
|
|
|
|
// if extracted and matchers exist but no match was found then don't generate result
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-06 18:53:03 +00:00
|
|
|
|
|
|
|
results := make([]*output.ResultEvent, 0, len(wrapped.OperatorsResult.Matches)+1)
|
|
|
|
|
|
|
|
// If we have multiple matchers with names, write each of them separately.
|
|
|
|
if len(wrapped.OperatorsResult.Matches) > 0 {
|
|
|
|
for matcherNames := range wrapped.OperatorsResult.Matches {
|
|
|
|
data := request.MakeResultEventItem(wrapped)
|
|
|
|
data.MatcherName = matcherNames
|
|
|
|
results = append(results, data)
|
|
|
|
}
|
|
|
|
} else if len(wrapped.OperatorsResult.Extracts) > 0 {
|
|
|
|
for k, v := range wrapped.OperatorsResult.Extracts {
|
|
|
|
data := request.MakeResultEventItem(wrapped)
|
|
|
|
data.ExtractorName = k
|
|
|
|
data.ExtractedResults = v
|
|
|
|
results = append(results, data)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data := request.MakeResultEventItem(wrapped)
|
|
|
|
results = append(results, data)
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
2021-10-29 12:56:06 +00:00
|
|
|
|
|
|
|
// MakeDefaultExtractFunc performs extracting operation for an extractor on model and returns true or false.
|
|
|
|
func MakeDefaultExtractFunc(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
|
2021-11-08 10:31:45 +00:00
|
|
|
part := extractor.Part
|
|
|
|
if part == "" {
|
|
|
|
part = "response"
|
|
|
|
}
|
|
|
|
|
|
|
|
item, ok := data[part]
|
2022-04-20 09:32:13 +00:00
|
|
|
if !ok && !extractors.SupportsMap(extractor) {
|
2021-10-29 12:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
itemStr := types.ToString(item)
|
|
|
|
|
|
|
|
switch extractor.GetType() {
|
|
|
|
case extractors.RegexExtractor:
|
|
|
|
return extractor.ExtractRegex(itemStr)
|
|
|
|
case extractors.KValExtractor:
|
|
|
|
return extractor.ExtractKval(data)
|
|
|
|
case extractors.JSONExtractor:
|
|
|
|
return extractor.ExtractJSON(itemStr)
|
|
|
|
case extractors.XPathExtractor:
|
2022-08-22 09:57:32 +00:00
|
|
|
return extractor.ExtractXPath(itemStr)
|
2022-04-20 09:32:13 +00:00
|
|
|
case extractors.DSLExtractor:
|
|
|
|
return extractor.ExtractDSL(data)
|
2021-10-29 12:56:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeDefaultMatchFunc performs matching operation for a matcher on model and returns true or false.
|
|
|
|
func MakeDefaultMatchFunc(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
|
2021-11-08 10:31:45 +00:00
|
|
|
part := matcher.Part
|
|
|
|
if part == "" {
|
|
|
|
part = "response"
|
|
|
|
}
|
|
|
|
|
2021-11-08 10:40:04 +00:00
|
|
|
partItem, ok := data[part]
|
2022-01-13 07:52:43 +00:00
|
|
|
if !ok && matcher.Type.MatcherType != matchers.DSLMatcher {
|
2021-10-29 12:56:06 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
item := types.ToString(partItem)
|
|
|
|
|
|
|
|
switch matcher.GetType() {
|
|
|
|
case matchers.SizeMatcher:
|
|
|
|
result := matcher.Result(matcher.MatchSize(len(item)))
|
|
|
|
return result, nil
|
|
|
|
case matchers.WordsMatcher:
|
2021-11-03 13:28:00 +00:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchWords(item, nil))
|
2021-10-29 12:56:06 +00:00
|
|
|
case matchers.RegexMatcher:
|
2021-11-03 13:28:00 +00:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchRegex(item))
|
2021-10-29 12:56:06 +00:00
|
|
|
case matchers.BinaryMatcher:
|
2021-11-03 13:28:00 +00:00
|
|
|
return matcher.ResultWithMatchedSnippet(matcher.MatchBinary(item))
|
2021-10-29 12:56:06 +00:00
|
|
|
case matchers.DSLMatcher:
|
|
|
|
return matcher.Result(matcher.MatchDSL(data)), nil
|
2023-08-25 17:11:51 +00:00
|
|
|
case matchers.XPathMatcher:
|
|
|
|
return matcher.Result(matcher.MatchXPath(item)), []string{}
|
2021-10-29 12:56:06 +00:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2023-11-10 23:12:27 +00:00
|
|
|
|
|
|
|
func (e *ExecutorOptions) EncodeTemplate() string {
|
|
|
|
if !e.Options.OmitTemplate && len(e.RawTemplate) <= MaxTemplateFileSizeForEncoding {
|
|
|
|
return base64.StdEncoding.EncodeToString(e.RawTemplate)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|