Skip to content

Commit

Permalink
dpq parsing: max num iterations (minor)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Jul 15, 2024
1 parent a2a0445 commit 1fe096c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions ais/dpq.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package ais

import (
"errors"
"fmt"
"net/url"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -66,18 +68,26 @@ func dpqFree(dpq *dpq) {
// Parse URL query for a selected few parameters used in the datapath.
// (This is a faster alternative to the conventional and RFC-compliant URL.Query()
// to be used narrowly to handle those few (keys) and nothing else.)

const maxNumQparams = 100

func (dpq *dpq) parse(rawQuery string) (err error) {
query := rawQuery // r.URL.RawQuery
for query != "" {
var (
iters int
query = rawQuery // r.URL.RawQuery
)
for query != "" && iters < maxNumQparams {
key, value := query, ""
if i := strings.IndexByte(key, '&'); i >= 0 {
key, query = key[:i], key[i+1:]
iters++
} else {
query = ""
query = "" // last iter
}
if k, v, ok := _dpqKeqV(key); ok {
key, value = k, v
}

// supported URL query parameters explicitly named below; attempt to parse anything
// outside this list will fail
switch key {
Expand Down Expand Up @@ -146,7 +156,10 @@ func (dpq *dpq) parse(rawQuery string) (err error) {
})
}
}
return
if err == nil && iters >= maxNumQparams {
err = errors.New("dpq: exceeded max number of iterations: " + strconv.Itoa(iters))
}
return err
}

func _dpqKeqV(s string) (string, string, bool) {
Expand Down

0 comments on commit 1fe096c

Please sign in to comment.