Skip to content

Commit

Permalink
add search char validation
Browse files Browse the repository at this point in the history
  • Loading branch information
morphy2k committed May 16, 2020
1 parent 16388c0 commit 1f3219e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controller/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"
"net/http"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -44,7 +45,10 @@ func getSort(def string, r *http.Request) map[string]int64 {
return sort
}

var regexNonAlnumBlankPunct = regexp.MustCompile(`[^[:alnum:][:blank:][:punct:]]`)

func isAlnumBlankPunct(s string) bool {
return !regexNonAlnumBlankPunct.MatchString(s)
}

func handleError(err error, w http.ResponseWriter) {
Expand Down
12 changes: 12 additions & 0 deletions controller/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func ItemIndexGET(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return
}

if !isAlnumBlankPunct(txt) {
s := &Status{}
s.BadRequest("Query string contains invalid characters").Render(w)
return
}

opts := &item.Options{}
opts.Limit, opts.Offset = getLimitOffset(r)

Expand Down Expand Up @@ -136,6 +142,12 @@ Loop:
return
}

if !isAlnumBlankPunct(txt) {
s := &Status{}
s.BadRequest("Query string contains invalid characters").Render(w)
return
}

result, err = item.GetByText(txt, opts, kind)
if err != nil {
handleError(err, w)
Expand Down

0 comments on commit 1f3219e

Please sign in to comment.