mirror of https://github.com/lus/pasty.git
validate request content type
This commit is contained in:
parent
695b900f28
commit
5b0ba721b8
|
@ -0,0 +1,14 @@
|
|||
package web
|
||||
|
||||
import "net/http"
|
||||
|
||||
func accept(writer http.ResponseWriter, request *http.Request, contentTypes ...string) bool {
|
||||
contentType := request.Header.Get("Content-Type")
|
||||
for _, accepted := range contentTypes {
|
||||
if contentType == accepted {
|
||||
return true
|
||||
}
|
||||
}
|
||||
writeString(writer, http.StatusUnsupportedMediaType, "unsupported media type")
|
||||
return false
|
||||
}
|
|
@ -16,6 +16,9 @@ type v2EndpointCreatePastePayload struct {
|
|||
|
||||
func (server *Server) v2EndpointCreatePaste(writer http.ResponseWriter, request *http.Request) {
|
||||
// Read, parse and validate the request payload
|
||||
if !accept(writer, request, "application/json") {
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
writeErr(request, writer, err)
|
||||
|
|
|
@ -20,6 +20,9 @@ func (server *Server) v2EndpointModifyPaste(writer http.ResponseWriter, request
|
|||
}
|
||||
|
||||
// Read, parse and validate the request payload
|
||||
if !accept(writer, request, "application/json") {
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
writeErr(request, writer, err)
|
||||
|
|
|
@ -20,6 +20,9 @@ func (server *Server) v2EndpointReportPaste(writer http.ResponseWriter, request
|
|||
}
|
||||
|
||||
// Read, parse and validate the request payload
|
||||
if !accept(writer, request, "application/json") {
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
writeErr(request, writer, err)
|
||||
|
|
Loading…
Reference in New Issue