-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* AllowedMethodsHandler to check allowed methods (between access control and scope) * allowed_methods attribute for api and endpoint blocks * wildcard for allowed_methods * CORS preflight: Access-Control-Allow-Methods only set with requested method if method is allowed (per allowed_methods or default) * Documentation for allowed_methods * changelog entry * little bit shorter code * moved config file * test cases for blocking request by not allowing any method * test cases for invalid strings in allowed_methods * upper-case methods only after regexp check * our own added operations (method/endpoint) must be allowed, no need to check method * removed now unused MustAddRoute() and allowedMethods * don't allow TRACE, added test cases for CONNECT * check methods at config load * moved methodAllowed from CORS to CORSOptions
- Loading branch information
Showing
20 changed files
with
462 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
var defaultAllowedMethods = []string{ | ||
http.MethodGet, | ||
http.MethodHead, | ||
http.MethodPost, | ||
http.MethodPut, | ||
http.MethodPatch, | ||
http.MethodDelete, | ||
http.MethodOptions, | ||
} | ||
|
||
var _ http.Handler = &AllowedMethodsHandler{} | ||
|
||
type AllowedMethodsHandler struct { | ||
allowedMethods map[string]struct{} | ||
allowedHandler http.Handler | ||
notAllowedHandler http.Handler | ||
} | ||
|
||
type methodAllowedFunc func(string) bool | ||
|
||
func NewAllowedMethodsHandler(allowedMethods []string, allowedHandler, notAllowedHandler http.Handler) (*AllowedMethodsHandler, error) { | ||
amh := &AllowedMethodsHandler{ | ||
allowedMethods: make(map[string]struct{}), | ||
allowedHandler: allowedHandler, | ||
notAllowedHandler: notAllowedHandler, | ||
} | ||
if allowedMethods == nil { | ||
allowedMethods = defaultAllowedMethods | ||
} | ||
for _, method := range allowedMethods { | ||
if method == "*" { | ||
for _, m := range defaultAllowedMethods { | ||
amh.allowedMethods[m] = struct{}{} | ||
} | ||
} else { | ||
method = strings.ToUpper(method) | ||
amh.allowedMethods[method] = struct{}{} | ||
} | ||
} | ||
|
||
return amh, nil | ||
} | ||
|
||
func (a *AllowedMethodsHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
if _, ok := a.allowedMethods[req.Method]; !ok { | ||
a.notAllowedHandler.ServeHTTP(rw, req) | ||
return | ||
} | ||
|
||
a.allowedHandler.ServeHTTP(rw, req) | ||
} | ||
|
||
func (a *AllowedMethodsHandler) MethodAllowed(method string) bool { | ||
method = strings.TrimSpace(strings.ToUpper(method)) | ||
_, ok := a.allowedMethods[method] | ||
return ok | ||
} | ||
|
||
func (a *AllowedMethodsHandler) Child() http.Handler { | ||
return a.allowedHandler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.