Skip to content

Commit

Permalink
migrate to new utils/errkit
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunKoyalwar committed May 15, 2024
1 parent 8e81d7d commit 9232604
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 503 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ require (
github.com/projectdiscovery/tlsx v1.1.6
github.com/projectdiscovery/uncover v1.0.7
github.com/projectdiscovery/useragent v0.0.49
github.com/projectdiscovery/utils v0.0.92
github.com/projectdiscovery/utils v0.0.93-0.20240515205400-37bc84ef0c3f
github.com/projectdiscovery/wappalyzergo v0.0.122
github.com/redis/go-redis/v9 v9.1.0
github.com/seh-msft/burpxml v1.0.1
github.com/stretchr/testify v1.9.0
github.com/tarunKoyalwar/goleak v0.0.0-20240426214851-746d64600adc
github.com/tarunKoyalwar/goleak v0.0.0-20240429141123-0efa90dbdcf9
github.com/zmap/zgrab2 v0.1.8-0.20230806160807-97ba87c0e706
golang.org/x/term v0.19.0
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -350,3 +350,5 @@ require (

// https://go.dev/ref/mod#go-mod-file-retract
retract v3.2.0 // retract due to broken js protocol issue

replace github.com/projectdiscovery/fastdialer => /Users/tarun/Codebase/fastdialer
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,6 @@ github.com/projectdiscovery/clistats v0.0.20 h1:5jO5SLiRJ7f0nDV0ndBNmBeesbROouPo
github.com/projectdiscovery/clistats v0.0.20/go.mod h1:GJ2av0KnOvK0AISQnP8hyDclYIji1LVkx2l0pwnzAu4=
github.com/projectdiscovery/dsl v0.0.52 h1:jvIvF+qN8+MbI1MHtWJJKfWqAZQlCExL3ob7SddQbZE=
github.com/projectdiscovery/dsl v0.0.52/go.mod h1:xfcHwhy2HSaeGgh+1wqzOoCGm2XTdh5JzjBRBVHEMvI=
github.com/projectdiscovery/fastdialer v0.0.71 h1:96j6Y65hDPZ9AzlYpp95hvIH5Yx/0OE2UTx+frWfnm4=
github.com/projectdiscovery/fastdialer v0.0.71/go.mod h1:b/oPPVSoLLD2N4W2/HrXbhQbyJVXqRw8CK1lenCUk64=
github.com/projectdiscovery/fasttemplate v0.0.2 h1:h2cISk5xDhlJEinlBQS6RRx0vOlOirB2y3Yu4PJzpiA=
github.com/projectdiscovery/fasttemplate v0.0.2/go.mod h1:XYWWVMxnItd+r0GbjA1GCsUopMw1/XusuQxdyAIHMCw=
github.com/projectdiscovery/freeport v0.0.5 h1:jnd3Oqsl4S8n0KuFkE5Hm8WGDP24ITBvmyw5pFTHS8Q=
Expand Down Expand Up @@ -889,8 +887,8 @@ github.com/projectdiscovery/uncover v1.0.7 h1:ut+2lTuvmftmveqF5RTjMWAgyLj8ltPQC7
github.com/projectdiscovery/uncover v1.0.7/go.mod h1:HFXgm1sRPuoN0D4oATljPIdmbo/EEh1wVuxQqo/dwFE=
github.com/projectdiscovery/useragent v0.0.49 h1:wQc9i+Xy+mUMJ45Ralv1JsQImRWqEOEvpYUe6MchScg=
github.com/projectdiscovery/useragent v0.0.49/go.mod h1:jQz6X/usiXrPYE6B/1uVKuzIrBJXgw9hLC9eeNy38+0=
github.com/projectdiscovery/utils v0.0.92 h1:lGCmjUJhzoNX4FQZWpp80058pRlD0/dYxLJOSs07EqY=
github.com/projectdiscovery/utils v0.0.92/go.mod h1:d5uvD5qcRiK3qxZbBy9eatCqrCSuj9SObL04w/WgXSg=
github.com/projectdiscovery/utils v0.0.93-0.20240515205400-37bc84ef0c3f h1:6fk5y6FG4SfB80aXOEQfz+MJFJ/1F3sAF17nUcstXOc=
github.com/projectdiscovery/utils v0.0.93-0.20240515205400-37bc84ef0c3f/go.mod h1:2+mWzk5FeYdK9imo5eLk6oVeih0G0wsTff1pzBAh9tk=
github.com/projectdiscovery/wappalyzergo v0.0.122 h1:xfNJ7VNzU/OGlgYtsyB5ppuOHdfWzU2B8cYATwTz54c=
github.com/projectdiscovery/wappalyzergo v0.0.122/go.mod h1:qW0PP+UBMcdQBBnwk+X6YYFs6huKNvn2BOVs4vQPru0=
github.com/projectdiscovery/yamldoc-go v1.0.4 h1:eZoESapnMw6WAHiVgRwNqvbJEfNHEH148uthhFbG5jE=
Expand Down Expand Up @@ -1016,8 +1014,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/tarunKoyalwar/goleak v0.0.0-20240426214851-746d64600adc h1:/5P5I7oDqdLee8W9Moof0xSD8tT1qEVzhObSI9CqHkg=
github.com/tarunKoyalwar/goleak v0.0.0-20240426214851-746d64600adc/go.mod h1:uQdBQGrE1fZ2EyOs0pLcCDd1bBV4rSThieuIIGhXZ50=
github.com/tarunKoyalwar/goleak v0.0.0-20240429141123-0efa90dbdcf9 h1:GXIyLuIJ5Qk46lI8WJ83qHBZKUI3zhmMmuoY9HICUIQ=
github.com/tarunKoyalwar/goleak v0.0.0-20240429141123-0efa90dbdcf9/go.mod h1:uQdBQGrE1fZ2EyOs0pLcCDd1bBV4rSThieuIIGhXZ50=
github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI=
github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
Expand Down
4 changes: 2 additions & 2 deletions pkg/js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"github.com/dop251/goja"

"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators"
"github.com/projectdiscovery/nuclei/v3/pkg/types/errkit"
contextutil "github.com/projectdiscovery/utils/context"
"github.com/projectdiscovery/utils/errkit"
stringsutil "github.com/projectdiscovery/utils/strings"
)

var (
// ErrJSExecDeadline is the error returned when alloted time for script execution exceeds
ErrJSExecDeadline = errkit.New("js engine execution deadline exceeded").SetClass(errkit.ErrClassDeadline).Build()
ErrJSExecDeadline = errkit.New("js engine execution deadline exceeded").SetKind(errkit.ErrKindDeadline).Build()
)

// Compiler provides a runtime to execute goja runtime
Expand Down
25 changes: 18 additions & 7 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"regexp"
Expand All @@ -27,7 +28,9 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
protocolUtils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
"github.com/projectdiscovery/nuclei/v3/pkg/types/nucleierr"
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
"github.com/projectdiscovery/utils/errkit"
fileutil "github.com/projectdiscovery/utils/file"
osutils "github.com/projectdiscovery/utils/os"
)
Expand Down Expand Up @@ -299,10 +302,12 @@ func (w *StandardWriter) Write(event *ResultEvent) error {

// JSONLogRequest is a trace/error log request written to file
type JSONLogRequest struct {
Template string `json:"template"`
Input string `json:"input"`
Error string `json:"error"`
Type string `json:"type"`
Template string `json:"template"`
Type string `json:"type"`
Input string `json:"input"`
Error string `json:"error"`
Kind string `json:"kind"`
Attrs interface{} `json:"attrs,omitempty"`
}

// Request writes a log the requests trace log
Expand All @@ -314,11 +319,17 @@ func (w *StandardWriter) Request(templatePath, input, requestType string, reques
Template: templatePath,
Input: input,
Type: requestType,
Kind: errkit.ErrKindUnknown.String(),
}
if unwrappedErr := utils.UnwrapError(requestErr); unwrappedErr != nil {
request.Error = unwrappedErr.Error()
} else {
errX := errkit.FromError(requestErr)
if errX == nil {
request.Error = "none"
} else {
request.Error = errX.Cause().Error()
request.Kind = errkit.GetErrorKind(requestErr, nucleierr.ErrTemplateLogic).String()
if len(errX.Attrs()) > 0 {
request.Attrs = slog.GroupValue(errX.Attrs()...)
}
}

data, err := jsoniter.Marshal(request)
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocols/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
protocolutils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
"github.com/projectdiscovery/nuclei/v3/pkg/types/errkit"
contextutil "github.com/projectdiscovery/utils/context"
"github.com/projectdiscovery/utils/errkit"
errorutil "github.com/projectdiscovery/utils/errors"
)

Expand All @@ -45,7 +45,7 @@ var (
// pythonEnvRegexCompiled is the compiled regex for python environment variables
pythonEnvRegexCompiled = regexp.MustCompile(pythonEnvRegex)
// ErrCodeExecutionDeadline is the error returned when alloted time for script execution exceeds
ErrCodeExecutionDeadline = errkit.New("code execution deadline exceeded").SetClass(errkit.ErrClassDeadline).Build()
ErrCodeExecutionDeadline = errkit.New("code execution deadline exceeded").SetKind(errkit.ErrKindDeadline).Build()
)

// Request is a request for the SSL protocol
Expand Down
22 changes: 16 additions & 6 deletions pkg/protocols/common/hosterrorscache/hosterrorscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (

"github.com/bluele/gcache"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/types/errkit"
"github.com/projectdiscovery/nuclei/v3/pkg/types/nucleierr"
"github.com/projectdiscovery/utils/errkit"
)

// CacheInterface defines the signature of the hosterrorscache so that
Expand Down Expand Up @@ -135,15 +136,24 @@ func (c *Cache) checkError(err error) bool {
if err == nil {
return false
}
// parse error for furthur processing
errX := errkit.FromError(err)
switch errX.Class() {
case errkit.ErrClassTemplateLogic:
kind := errkit.GetErrorKind(err, nucleierr.ErrTemplateLogic)
switch kind {
case nucleierr.ErrTemplateLogic:
// these are errors that are not related to the target
// and are due to template logic
return false

case errkit.ErrKindNetworkTemporary:
// these should not be counted as host errors
return false
case errkit.ErrKindNetworkPermanent:
// these should be counted as host errors
return true
case errkit.ErrKindDeadline:
// these should not be counted as host errors
return false
default:
// parse error for furthur processing
errX := errkit.FromError(err)
tmp := errX.Cause()
cause := tmp.Error()
if strings.Contains(cause, "ReadStatusLine:") && strings.Contains(cause, "read: connection reset by peer") {
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocols/common/protocolstate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func Init(options *types.Options) error {
if err != nil {
return err
}
opts.ProxyDialer = &dialer
opts.ProxyDialer = dialer
}

if options.SystemResolvers {
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocols/headless/engine/page_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/utils/vardump"
protocolutils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
httputil "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils/http"
"github.com/projectdiscovery/nuclei/v3/pkg/types/errkit"
contextutil "github.com/projectdiscovery/utils/context"
"github.com/projectdiscovery/utils/errkit"
errorutil "github.com/projectdiscovery/utils/errors"
fileutil "github.com/projectdiscovery/utils/file"
folderutil "github.com/projectdiscovery/utils/folder"
Expand All @@ -37,7 +37,7 @@ var (
errinvalidArguments = errorutil.New("invalid arguments provided")
ErrLFAccessDenied = errorutil.New("Use -allow-local-file-access flag to enable local file access")
// ErrActionExecDealine is the error returned when alloted time for action execution exceeds
ErrActionExecDealine = errkit.New("headless action execution deadline exceeded").SetClass(errkit.ErrClassDeadline).Build()
ErrActionExecDealine = errkit.New("headless action execution deadline exceeded").SetKind(errkit.ErrKindDeadline).Build()
)

const (
Expand Down
7 changes: 4 additions & 3 deletions pkg/protocols/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ import (
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/signerpool"
templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
"github.com/projectdiscovery/nuclei/v3/pkg/types/errkit"
"github.com/projectdiscovery/nuclei/v3/pkg/types/nucleierr"
"github.com/projectdiscovery/rawhttp"
convUtil "github.com/projectdiscovery/utils/conversion"
"github.com/projectdiscovery/utils/errkit"
errorutil "github.com/projectdiscovery/utils/errors"
httpUtils "github.com/projectdiscovery/utils/http"
"github.com/projectdiscovery/utils/reader"
Expand All @@ -56,9 +57,9 @@ const (
var (
MaxBodyRead = int64(10 * 1024 * 1024) // 10MB
// ErrMissingVars is error occured when variables are missing
ErrMissingVars = errkit.New("stop execution due to unresolved variables").SetClass(errkit.ErrClassTemplateLogic).Build()
ErrMissingVars = errkit.New("stop execution due to unresolved variables").SetKind(nucleierr.ErrTemplateLogic).Build()
// ErrHttpEngineRequestDeadline is error occured when request deadline set by http request engine is exceeded
ErrHttpEngineRequestDeadline = errkit.New("http request engine deadline exceeded").SetClass(errkit.ErrClassDeadline).Build()
ErrHttpEngineRequestDeadline = errkit.New("http request engine deadline exceeded").SetKind(errkit.ErrKindDeadline).Build()
)

// Type returns the type of the protocol request
Expand Down
7 changes: 4 additions & 3 deletions pkg/protocols/http/request_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/http/httpclientpool"
"github.com/projectdiscovery/nuclei/v3/pkg/types/errkit"
"github.com/projectdiscovery/nuclei/v3/pkg/types/nucleierr"
"github.com/projectdiscovery/retryablehttp-go"
"github.com/projectdiscovery/utils/errkit"
iputil "github.com/projectdiscovery/utils/ip"
stringsutil "github.com/projectdiscovery/utils/strings"
)
Expand All @@ -33,10 +34,10 @@ var (
// which was alloted using @timeout annotation this usually means that vulnerability was not found
// in rare case it could also happen due to network congestion
// the assigned class is TemplateLogic since this in almost every case means that server is not vulnerable
ErrTimeoutAnnotationDeadline = errkit.New("timeout annotation deadline exceeded").SetClass(errkit.ErrClassTemplateLogic).Build()
ErrTimeoutAnnotationDeadline = errkit.New("timeout annotation deadline exceeded").SetKind(nucleierr.ErrTemplateLogic).Build()
// ErrRequestTimeoutDeadline is the error returned when a specific amount of time was exceeded for a request
// this happens when the request execution exceeds alloted time
ErrRequestTimeoutDeadline = errkit.New("request timeout deadline exceeded when notimeout is set").SetClass(errkit.ErrClassDeadline).Build()
ErrRequestTimeoutDeadline = errkit.New("request timeout deadline exceeded when notimeout is set").SetKind(errkit.ErrKindDeadline).Build()
)

type flowMark int
Expand Down
Loading

0 comments on commit 9232604

Please sign in to comment.