Return wrapped errors for DSL compilation problems (#2492)

This allows the DSL help information to be printed when in debug mode.

Fixes #2481
dev
M. Ángel Jimeno 2022-08-29 10:11:32 +02:00 committed by GitHub
parent e329428684
commit 62a4e0aa52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 17 deletions

View File

@ -872,3 +872,16 @@ func appendSingleDigitZero(value string) string {
} }
return value return value
} }
type CompilationError struct {
DslSignature string
WrappedError error
}
func (e *CompilationError) Error() string {
return fmt.Sprintf("could not compile DSL expression %q: %v", e.DslSignature, e.WrappedError)
}
func (e *CompilationError) Unwrap() error {
return e.WrappedError
}

View File

@ -45,7 +45,7 @@ func (e *Extractor) CompileExtractors() error {
for _, dslExp := range e.DSL { for _, dslExp := range e.DSL {
compiled, err := govaluate.NewEvaluableExpressionWithFunctions(dslExp, dsl.HelperFunctions) compiled, err := govaluate.NewEvaluableExpressionWithFunctions(dslExp, dsl.HelperFunctions)
if err != nil { if err != nil {
return fmt.Errorf("could not compile dsl: %s", dslExp) return &dsl.CompilationError{DslSignature: dslExp, WrappedError: err}
} }
e.dslCompiled = append(e.dslCompiled, compiled) e.dslCompiled = append(e.dslCompiled, compiled)
} }

View File

@ -64,7 +64,7 @@ func (matcher *Matcher) CompileMatchers() error {
for _, dslExpression := range matcher.DSL { for _, dslExpression := range matcher.DSL {
compiledExpression, err := govaluate.NewEvaluableExpressionWithFunctions(dslExpression, dsl.HelperFunctions) compiledExpression, err := govaluate.NewEvaluableExpressionWithFunctions(dslExpression, dsl.HelperFunctions)
if err != nil { if err != nil {
return &DslCompilationError{DslSignature: dslExpression, WrappedError: err} return &dsl.CompilationError{DslSignature: dslExpression, WrappedError: err}
} }
matcher.dslCompiled = append(matcher.dslCompiled, compiledExpression) matcher.dslCompiled = append(matcher.dslCompiled, compiledExpression)
} }
@ -89,16 +89,3 @@ func (matcher *Matcher) CompileMatchers() error {
} }
return nil return nil
} }
type DslCompilationError struct {
DslSignature string
WrappedError error
}
func (e *DslCompilationError) Error() string {
return fmt.Sprintf("could not compile DSL expression: %s. %v", e.DslSignature, e.WrappedError)
}
func (e *DslCompilationError) Unwrap() error {
return e.WrappedError
}

View File

@ -8,7 +8,6 @@ import (
"github.com/projectdiscovery/gologger" "github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/operators/common/dsl" "github.com/projectdiscovery/nuclei/v2/pkg/operators/common/dsl"
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
"github.com/projectdiscovery/nuclei/v2/pkg/output" "github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols" "github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/writer" "github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/writer"
@ -33,7 +32,7 @@ func (e *Executer) Compile() error {
for _, request := range e.requests { for _, request := range e.requests {
if err := request.Compile(e.options); err != nil { if err := request.Compile(e.options); err != nil {
var dslCompilationError *matchers.DslCompilationError var dslCompilationError *dsl.CompilationError
if errors.As(err, &dslCompilationError) { if errors.As(err, &dslCompilationError) {
if cliOptions.Verbose { if cliOptions.Verbose {
rawErrorMessage := dslCompilationError.Error() rawErrorMessage := dslCompilationError.Error()