@@ -39,6 +39,7 @@ import (
3939 "net/url"
4040 "os"
4141 "path/filepath"
42+ "regexp"
4243 "sort"
4344 "strconv"
4445 "strings"
@@ -80,6 +81,7 @@ const (
8081
8182var (
8283 ErrRequestMalformed = errors .New ("request malformed" )
84+ validPolicyIDRegex = regexp .MustCompile (`^[a-zA-Z0-9.\-_~]+$` )
8385)
8486
8587// apiModifyKeySuccess represents when a Key modification was successful
@@ -1110,6 +1112,11 @@ func (gw *Gateway) handleAddOrUpdatePolicy(polID string, r *http.Request) (inter
11101112 return apiError ("Request malformed" ), http .StatusBadRequest
11111113 }
11121114
1115+ if newPol .ID != "" && ! isValidPolicyID (newPol .ID ) {
1116+ log .WithField ("id" , newPol .ID ).Error ("Policy ID contains invalid characters" )
1117+ return apiError ("Invalid Policy ID in body. Allowed characters: a-z, A-Z, 0-9, ., _, -" ), http .StatusBadRequest
1118+ }
1119+
11131120 if polID != "" && newPol .ID != polID && r .Method == http .MethodPut {
11141121 log .Error ("PUT operation on different IDs" )
11151122 return apiError ("Request ID does not match that in policy! For Update operations these must match." ), http .StatusBadRequest
@@ -1555,6 +1562,12 @@ func (gw *Gateway) handleDeleteAPI(apiID string) (interface{}, int) {
15551562func (gw * Gateway ) polHandler (w http.ResponseWriter , r * http.Request ) {
15561563 polID := mux .Vars (r )["polID" ]
15571564
1565+ if polID != "" && ! isValidPolicyID (polID ) {
1566+ log .WithField ("id" , polID ).Error ("Policy ID contains invalid characters" )
1567+ doJSONWrite (w , http .StatusBadRequest , apiError ("Invalid Policy ID. Allowed characters: a-z, A-Z, 0-9, ., _, -" ))
1568+ return
1569+ }
1570+
15581571 var obj interface {}
15591572 var code int
15601573
@@ -1589,6 +1602,13 @@ func (gw *Gateway) polHandler(w http.ResponseWriter, r *http.Request) {
15891602 doJSONWrite (w , code , obj )
15901603}
15911604
1605+ func isValidPolicyID (id string ) bool {
1606+ if id == "" {
1607+ return true
1608+ }
1609+ return validPolicyIDRegex .MatchString (id )
1610+ }
1611+
15921612func (gw * Gateway ) apiHandler (w http.ResponseWriter , r * http.Request ) {
15931613 apiID := mux .Vars (r )["apiID" ]
15941614
0 commit comments