@@ -20,6 +20,7 @@ import (
20
20
"encoding/hex"
21
21
"encoding/json"
22
22
"encoding/pem"
23
+ "errors"
23
24
"fmt"
24
25
"hash"
25
26
"html"
@@ -46,7 +47,7 @@ import (
46
47
"github.com/projectdiscovery/gostruct"
47
48
"github.com/projectdiscovery/mapcidr"
48
49
jarm "github.com/projectdiscovery/utils/crypto/jarm"
49
- errors "github.com/projectdiscovery/utils/errors "
50
+ "github.com/projectdiscovery/utils/errkit "
50
51
maputils "github.com/projectdiscovery/utils/maps"
51
52
randint "github.com/projectdiscovery/utils/rand"
52
53
stringsutil "github.com/projectdiscovery/utils/strings"
67
68
68
69
// ErrParsingArg is error when parsing value of argument
69
70
// Use With Caution: Nuclei ignores this error in extractors(ref: https://github.com/projectdiscovery/nuclei/issues/3950)
70
- ErrParsingArg = errors .New ("error parsing argument value" )
71
+ ErrParsingArg = errkit .New ("error parsing argument value" )
71
72
72
73
DefaultMaxDecompressionSize = int64 (10 * 1024 * 1024 ) // 10MB
73
74
DefaultCacheSize = 6144
@@ -81,7 +82,7 @@ var functions []dslFunction
81
82
func AddFunction (function dslFunction ) error {
82
83
for _ , f := range functions {
83
84
if function .Name == f .Name {
84
- return errors .New ("duplicate helper function key defined" )
85
+ return errkit .New ("duplicate helper function key defined" )
85
86
}
86
87
}
87
88
functions = append (functions , function )
@@ -147,15 +148,16 @@ func init() {
147
148
true ,
148
149
func (args ... interface {}) (interface {}, error ) {
149
150
argCount := len (args )
150
- if argCount == 0 {
151
+ switch argCount {
152
+ case 0 :
151
153
return nil , ErrInvalidDslFunction
152
- } else if argCount == 1 {
154
+ case 1 :
153
155
runes := []rune (toString (args [0 ]))
154
156
sort .Slice (runes , func (i int , j int ) bool {
155
157
return runes [i ] < runes [j ]
156
158
})
157
159
return string (runes ), nil
158
- } else {
160
+ default :
159
161
tokens := make ([]string , 0 , argCount )
160
162
for _ , arg := range args {
161
163
tokens = append (tokens , toString (arg ))
@@ -208,9 +210,10 @@ func init() {
208
210
true ,
209
211
func (args ... interface {}) (interface {}, error ) {
210
212
argCount := len (args )
211
- if argCount == 0 {
213
+ switch argCount {
214
+ case 0 :
212
215
return nil , ErrInvalidDslFunction
213
- } else if argCount == 1 {
216
+ case 1 :
214
217
builder := & strings.Builder {}
215
218
visited := make (map [rune ]struct {})
216
219
for _ , i := range toString (args [0 ]) {
@@ -220,7 +223,7 @@ func init() {
220
223
}
221
224
}
222
225
return builder .String (), nil
223
- } else {
226
+ default :
224
227
result := make ([]string , 0 , argCount )
225
228
visited := make (map [string ]struct {})
226
229
for _ , i := range args [0 :] {
@@ -543,7 +546,7 @@ func init() {
543
546
}))
544
547
MustAddFunction (NewWithPositionalArgs ("mmh3" , 1 , true , func (args ... interface {}) (interface {}, error ) {
545
548
hasher := murmur3 .New32WithSeed (0 )
546
- hasher .Write ([]byte (fmt .Sprint (args [0 ])))
549
+ hasher .Write ([]byte (fmt .Sprint (args [0 ]))) //nolint
547
550
return fmt .Sprintf ("%d" , int32 (hasher .Sum32 ())), nil
548
551
}))
549
552
MustAddFunction (NewWithPositionalArgs ("contains" , 2 , true , func (args ... interface {}) (interface {}, error ) {
@@ -649,24 +652,25 @@ func init() {
649
652
true ,
650
653
func (arguments ... interface {}) (interface {}, error ) {
651
654
argumentsSize := len (arguments )
652
- if argumentsSize == 2 {
655
+ switch argumentsSize {
656
+ case 2 :
653
657
input := toString (arguments [0 ])
654
658
separatorOrCount := toString (arguments [1 ])
655
659
656
660
count , err := strconv .Atoi (separatorOrCount )
657
661
if err != nil {
658
- return strings .SplitN (input , separatorOrCount , - 1 ), nil
662
+ return strings .Split (input , separatorOrCount ), nil
659
663
}
660
664
return toChunks (input , count ), nil
661
- } else if argumentsSize == 3 {
665
+ case 3 :
662
666
input := toString (arguments [0 ])
663
667
separator := toString (arguments [1 ])
664
668
count , err := strconv .Atoi (toString (arguments [2 ]))
665
669
if err != nil {
666
670
return nil , ErrInvalidDslFunction
667
671
}
668
672
return strings .SplitN (input , separator , count ), nil
669
- } else {
673
+ default :
670
674
return nil , ErrInvalidDslFunction
671
675
}
672
676
}))
@@ -676,25 +680,26 @@ func init() {
676
680
true ,
677
681
func (arguments ... interface {}) (interface {}, error ) {
678
682
argumentsSize := len (arguments )
679
- if argumentsSize < 2 {
683
+ switch {
684
+ case argumentsSize < 2 :
680
685
return nil , ErrInvalidDslFunction
681
- } else if argumentsSize == 2 {
686
+ case argumentsSize == 2 :
682
687
separator := toString (arguments [0 ])
683
688
elements , ok := arguments [1 ].([]string )
684
689
685
690
if ! ok {
686
- return nil , errors .New ("cannot cast elements into string" )
691
+ return nil , errkit .New ("cannot cast elements into string" )
687
692
}
688
693
689
694
return strings .Join (elements , separator ), nil
690
- } else {
695
+ default :
691
696
separator := toString (arguments [0 ])
692
697
elements := arguments [1 :argumentsSize ]
693
698
694
699
stringElements := make ([]string , 0 , argumentsSize )
695
700
for _ , element := range elements {
696
701
if _ , ok := element .([]string ); ok {
697
- return nil , errors .New ("cannot use join on more than one slice element" )
702
+ return nil , errkit .New ("cannot use join on more than one slice element" )
698
703
}
699
704
700
705
stringElements = append (stringElements , toString (element ))
@@ -985,7 +990,7 @@ func init() {
985
990
986
991
firstParsed , parseErr := version .NewVersion (toString (args [0 ]))
987
992
if parseErr != nil {
988
- return nil , errors . NewWithErr (ErrParsingArg ). Wrap ( parseErr )
993
+ return nil , errkit . Combine (ErrParsingArg , parseErr )
989
994
}
990
995
991
996
var versionConstraints []string
@@ -1016,11 +1021,11 @@ func init() {
1016
1021
bLen = int (floatVal )
1017
1022
}
1018
1023
if bLen == 0 {
1019
- return nil , errors .New ("invalid padding length" )
1024
+ return nil , errkit .New ("invalid padding length" )
1020
1025
}
1021
1026
bByte := []byte (toString (args [1 ]))
1022
1027
if len (bByte ) == 0 {
1023
- return nil , errors .New ("invalid padding byte" )
1028
+ return nil , errkit .New ("invalid padding byte" )
1024
1029
}
1025
1030
bData := []byte (toString (args [0 ]))
1026
1031
dataLen := len (bData )
@@ -1030,7 +1035,7 @@ func init() {
1030
1035
1031
1036
padMode , ok := args [3 ].(string )
1032
1037
if ! ok || (padMode != "prefix" && padMode != "suffix" ) {
1033
- return nil , errors .New ("padding mode must be 'prefix' or 'suffix'" )
1038
+ return nil , errkit .New ("padding mode must be 'prefix' or 'suffix'" )
1034
1039
}
1035
1040
1036
1041
paddingLen := bLen - dataLen
@@ -1101,31 +1106,31 @@ func init() {
1101
1106
}
1102
1107
argStr := toString (args [0 ])
1103
1108
if len (argStr ) == 0 {
1104
- return nil , errors .New ("empty string" )
1109
+ return nil , errkit .New ("empty string" )
1105
1110
}
1106
1111
start , err := strconv .Atoi (toString (args [1 ]))
1107
1112
if err != nil {
1108
- return nil , errors . NewWithErr (err ). Msgf ( "invalid start position" )
1113
+ return nil , errkit . Wrap (err , "invalid start position" )
1109
1114
}
1110
1115
if start > len (argStr ) {
1111
- return nil , errors .New ("start position bigger than slice length" )
1116
+ return nil , errkit .New ("start position bigger than slice length" )
1112
1117
}
1113
1118
if len (args ) == 2 {
1114
1119
return argStr [start :], nil
1115
1120
}
1116
1121
1117
1122
end , err := strconv .Atoi (toString (args [2 ]))
1118
1123
if err != nil {
1119
- return nil , errors .New ("invalid end position" )
1124
+ return nil , errkit .New ("invalid end position" )
1120
1125
}
1121
1126
if end < 0 {
1122
- return nil , errors .New ("negative end position" )
1127
+ return nil , errkit .New ("negative end position" )
1123
1128
}
1124
1129
if end < start {
1125
- return nil , errors .New ("end position before start" )
1130
+ return nil , errkit .New ("end position before start" )
1126
1131
}
1127
1132
if end > len (argStr ) {
1128
- return nil , errors .New ("end position bigger than slice length start" )
1133
+ return nil , errkit .New ("end position bigger than slice length start" )
1129
1134
}
1130
1135
return argStr [start :end ], nil
1131
1136
}))
@@ -1448,8 +1453,8 @@ func init() {
1448
1453
}
1449
1454
1450
1455
var mtime int64
1451
- if ! reader .Header . ModTime .IsZero () {
1452
- mtime = reader .Header . ModTime .Unix ()
1456
+ if ! reader .ModTime .IsZero () {
1457
+ mtime = reader .ModTime .Unix ()
1453
1458
}
1454
1459
_ = reader .Close ()
1455
1460
0 commit comments