Skip to content

Commit b0cda64

Browse files
committed
Merge branch 'main' into pr/258
2 parents 10a8566 + 57274b5 commit b0cda64

File tree

4 files changed

+56
-51
lines changed

4 files changed

+56
-51
lines changed

dsl.go

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/hex"
2121
"encoding/json"
2222
"encoding/pem"
23+
"errors"
2324
"fmt"
2425
"hash"
2526
"html"
@@ -46,7 +47,7 @@ import (
4647
"github.com/projectdiscovery/gostruct"
4748
"github.com/projectdiscovery/mapcidr"
4849
jarm "github.com/projectdiscovery/utils/crypto/jarm"
49-
errors "github.com/projectdiscovery/utils/errors"
50+
"github.com/projectdiscovery/utils/errkit"
5051
maputils "github.com/projectdiscovery/utils/maps"
5152
randint "github.com/projectdiscovery/utils/rand"
5253
stringsutil "github.com/projectdiscovery/utils/strings"
@@ -67,7 +68,7 @@ var (
6768

6869
// ErrParsingArg is error when parsing value of argument
6970
// 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")
7172

7273
DefaultMaxDecompressionSize = int64(10 * 1024 * 1024) // 10MB
7374
DefaultCacheSize = 6144
@@ -81,7 +82,7 @@ var functions []dslFunction
8182
func AddFunction(function dslFunction) error {
8283
for _, f := range functions {
8384
if function.Name == f.Name {
84-
return errors.New("duplicate helper function key defined")
85+
return errkit.New("duplicate helper function key defined")
8586
}
8687
}
8788
functions = append(functions, function)
@@ -147,15 +148,16 @@ func init() {
147148
true,
148149
func(args ...interface{}) (interface{}, error) {
149150
argCount := len(args)
150-
if argCount == 0 {
151+
switch argCount {
152+
case 0:
151153
return nil, ErrInvalidDslFunction
152-
} else if argCount == 1 {
154+
case 1:
153155
runes := []rune(toString(args[0]))
154156
sort.Slice(runes, func(i int, j int) bool {
155157
return runes[i] < runes[j]
156158
})
157159
return string(runes), nil
158-
} else {
160+
default:
159161
tokens := make([]string, 0, argCount)
160162
for _, arg := range args {
161163
tokens = append(tokens, toString(arg))
@@ -208,9 +210,10 @@ func init() {
208210
true,
209211
func(args ...interface{}) (interface{}, error) {
210212
argCount := len(args)
211-
if argCount == 0 {
213+
switch argCount {
214+
case 0:
212215
return nil, ErrInvalidDslFunction
213-
} else if argCount == 1 {
216+
case 1:
214217
builder := &strings.Builder{}
215218
visited := make(map[rune]struct{})
216219
for _, i := range toString(args[0]) {
@@ -220,7 +223,7 @@ func init() {
220223
}
221224
}
222225
return builder.String(), nil
223-
} else {
226+
default:
224227
result := make([]string, 0, argCount)
225228
visited := make(map[string]struct{})
226229
for _, i := range args[0:] {
@@ -543,7 +546,7 @@ func init() {
543546
}))
544547
MustAddFunction(NewWithPositionalArgs("mmh3", 1, true, func(args ...interface{}) (interface{}, error) {
545548
hasher := murmur3.New32WithSeed(0)
546-
hasher.Write([]byte(fmt.Sprint(args[0])))
549+
hasher.Write([]byte(fmt.Sprint(args[0]))) //nolint
547550
return fmt.Sprintf("%d", int32(hasher.Sum32())), nil
548551
}))
549552
MustAddFunction(NewWithPositionalArgs("contains", 2, true, func(args ...interface{}) (interface{}, error) {
@@ -649,24 +652,25 @@ func init() {
649652
true,
650653
func(arguments ...interface{}) (interface{}, error) {
651654
argumentsSize := len(arguments)
652-
if argumentsSize == 2 {
655+
switch argumentsSize {
656+
case 2:
653657
input := toString(arguments[0])
654658
separatorOrCount := toString(arguments[1])
655659

656660
count, err := strconv.Atoi(separatorOrCount)
657661
if err != nil {
658-
return strings.SplitN(input, separatorOrCount, -1), nil
662+
return strings.Split(input, separatorOrCount), nil
659663
}
660664
return toChunks(input, count), nil
661-
} else if argumentsSize == 3 {
665+
case 3:
662666
input := toString(arguments[0])
663667
separator := toString(arguments[1])
664668
count, err := strconv.Atoi(toString(arguments[2]))
665669
if err != nil {
666670
return nil, ErrInvalidDslFunction
667671
}
668672
return strings.SplitN(input, separator, count), nil
669-
} else {
673+
default:
670674
return nil, ErrInvalidDslFunction
671675
}
672676
}))
@@ -676,25 +680,26 @@ func init() {
676680
true,
677681
func(arguments ...interface{}) (interface{}, error) {
678682
argumentsSize := len(arguments)
679-
if argumentsSize < 2 {
683+
switch {
684+
case argumentsSize < 2:
680685
return nil, ErrInvalidDslFunction
681-
} else if argumentsSize == 2 {
686+
case argumentsSize == 2:
682687
separator := toString(arguments[0])
683688
elements, ok := arguments[1].([]string)
684689

685690
if !ok {
686-
return nil, errors.New("cannot cast elements into string")
691+
return nil, errkit.New("cannot cast elements into string")
687692
}
688693

689694
return strings.Join(elements, separator), nil
690-
} else {
695+
default:
691696
separator := toString(arguments[0])
692697
elements := arguments[1:argumentsSize]
693698

694699
stringElements := make([]string, 0, argumentsSize)
695700
for _, element := range elements {
696701
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")
698703
}
699704

700705
stringElements = append(stringElements, toString(element))
@@ -985,7 +990,7 @@ func init() {
985990

986991
firstParsed, parseErr := version.NewVersion(toString(args[0]))
987992
if parseErr != nil {
988-
return nil, errors.NewWithErr(ErrParsingArg).Wrap(parseErr)
993+
return nil, errkit.Combine(ErrParsingArg, parseErr)
989994
}
990995

991996
var versionConstraints []string
@@ -1016,11 +1021,11 @@ func init() {
10161021
bLen = int(floatVal)
10171022
}
10181023
if bLen == 0 {
1019-
return nil, errors.New("invalid padding length")
1024+
return nil, errkit.New("invalid padding length")
10201025
}
10211026
bByte := []byte(toString(args[1]))
10221027
if len(bByte) == 0 {
1023-
return nil, errors.New("invalid padding byte")
1028+
return nil, errkit.New("invalid padding byte")
10241029
}
10251030
bData := []byte(toString(args[0]))
10261031
dataLen := len(bData)
@@ -1030,7 +1035,7 @@ func init() {
10301035

10311036
padMode, ok := args[3].(string)
10321037
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'")
10341039
}
10351040

10361041
paddingLen := bLen - dataLen
@@ -1101,31 +1106,31 @@ func init() {
11011106
}
11021107
argStr := toString(args[0])
11031108
if len(argStr) == 0 {
1104-
return nil, errors.New("empty string")
1109+
return nil, errkit.New("empty string")
11051110
}
11061111
start, err := strconv.Atoi(toString(args[1]))
11071112
if err != nil {
1108-
return nil, errors.NewWithErr(err).Msgf("invalid start position")
1113+
return nil, errkit.Wrap(err, "invalid start position")
11091114
}
11101115
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")
11121117
}
11131118
if len(args) == 2 {
11141119
return argStr[start:], nil
11151120
}
11161121

11171122
end, err := strconv.Atoi(toString(args[2]))
11181123
if err != nil {
1119-
return nil, errors.New("invalid end position")
1124+
return nil, errkit.New("invalid end position")
11201125
}
11211126
if end < 0 {
1122-
return nil, errors.New("negative end position")
1127+
return nil, errkit.New("negative end position")
11231128
}
11241129
if end < start {
1125-
return nil, errors.New("end position before start")
1130+
return nil, errkit.New("end position before start")
11261131
}
11271132
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")
11291134
}
11301135
return argStr[start:end], nil
11311136
}))
@@ -1448,8 +1453,8 @@ func init() {
14481453
}
14491454

14501455
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()
14531458
}
14541459
_ = reader.Close()
14551460

func.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (d dslFunction) hash(args ...interface{}) string {
139139
binary.LittleEndian.PutUint64(b[:], math.Float64bits(v))
140140
_, _ = hasher.Write(b[:])
141141
default:
142-
_, _ = hasher.Write([]byte(fmt.Sprintf("%v", v)))
142+
_, _ = fmt.Fprintf(hasher, "%v", v)
143143
}
144144

145145
if i < len(args)-1 {

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ require (
1313
github.com/projectdiscovery/gologger v1.1.54
1414
github.com/projectdiscovery/gostruct v0.0.2
1515
github.com/projectdiscovery/mapcidr v1.1.34
16-
github.com/projectdiscovery/utils v0.4.22
16+
github.com/projectdiscovery/utils v0.5.0
1717
github.com/sashabaranov/go-openai v1.37.0
1818
github.com/spaolacci/murmur3 v1.1.0
1919
github.com/stretchr/testify v1.10.0
2020
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8
21-
golang.org/x/text v0.23.0
21+
golang.org/x/text v0.24.0
2222
)
2323

2424
require (
@@ -53,13 +53,13 @@ require (
5353
github.com/tidwall/gjson v1.18.0 // indirect
5454
github.com/tidwall/match v1.1.1 // indirect
5555
github.com/tidwall/pretty v1.2.1 // indirect
56-
github.com/ulikunitz/xz v0.5.12 // indirect
56+
github.com/ulikunitz/xz v0.5.14 // indirect
5757
go.uber.org/multierr v1.11.0 // indirect
5858
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
5959
golang.org/x/mod v0.22.0 // indirect
60-
golang.org/x/net v0.38.0 // indirect
61-
golang.org/x/sync v0.12.0 // indirect
62-
golang.org/x/sys v0.31.0 // indirect
60+
golang.org/x/net v0.39.0 // indirect
61+
golang.org/x/sync v0.13.0 // indirect
62+
golang.org/x/sys v0.32.0 // indirect
6363
golang.org/x/tools v0.29.0 // indirect
6464
gopkg.in/djherbis/times.v1 v1.3.0 // indirect
6565
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ github.com/projectdiscovery/gostruct v0.0.2 h1:s8gP8ApugGM4go1pA+sVlPDXaWqNP5BBD
147147
github.com/projectdiscovery/gostruct v0.0.2/go.mod h1:H86peL4HKwMXcQQtEa6lmC8FuD9XFt6gkNR0B/Mu5PE=
148148
github.com/projectdiscovery/mapcidr v1.1.34 h1:udr83vQ7oz3kEOwlsU6NC6o08leJzSDQtls1wmXN/kM=
149149
github.com/projectdiscovery/mapcidr v1.1.34/go.mod h1:1+1R6OkKSAKtWDXE9RvxXtXPoajXTYX0eiEdkqlhQqQ=
150-
github.com/projectdiscovery/utils v0.4.22 h1:OO3FU2uX967sQxu5JtpdBZNzOevvKHAhWqkoTGl+C0A=
151-
github.com/projectdiscovery/utils v0.4.22/go.mod h1:3l84gpCwL9KG1/ZmslOBABCrk84CcpGWJZfR8wZysR4=
150+
github.com/projectdiscovery/utils v0.5.0 h1:DN7mg2DpyObLByuObXzAFEkdNRDoPUnqE5N2szd3b3c=
151+
github.com/projectdiscovery/utils v0.5.0/go.mod h1:eCAWMmyaNxyPWbiKv1oeYJLIKpxceHE2+NWx3Jodhqk=
152152
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
153153
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
154154
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
@@ -182,8 +182,8 @@ github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
182182
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
183183
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
184184
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
185-
github.com/ulikunitz/xz v0.5.12 h1:37Nm15o69RwBkXM0J6A5OlE67RZTfzUxTj8fB3dfcsc=
186-
github.com/ulikunitz/xz v0.5.12/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
185+
github.com/ulikunitz/xz v0.5.14 h1:uv/0Bq533iFdnMHZdRBTOlaNMdb1+ZxXIlHDZHIHcvg=
186+
github.com/ulikunitz/xz v0.5.14/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
187187
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
188188
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
189189
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@@ -249,8 +249,8 @@ golang.org/x/net v0.0.0-20200528225125-3c3fba18258b/go.mod h1:qpuaurCH72eLCgpAm/
249249
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
250250
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
251251
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
252-
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
253-
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
252+
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
253+
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
254254
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
255255
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
256256
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -264,8 +264,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
264264
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
265265
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
266266
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
267-
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
268-
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
267+
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
268+
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
269269
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
270270
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
271271
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -288,8 +288,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
288288
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
289289
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
290290
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
291-
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
292-
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
291+
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
292+
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
293293
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
294294
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
295295
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
@@ -300,8 +300,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
300300
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
301301
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
302302
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
303-
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
304-
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
303+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
304+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
305305
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
306306
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
307307
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

0 commit comments

Comments
 (0)