Changeset 0.28.4 (#190)
This commit is contained in:
33
server/middleware/validate.go
Normal file
33
server/middleware/validate.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const maxParamLen = 255
|
||||
|
||||
// ValidatePathParams rejects requests with null bytes or excessively long
|
||||
// path parameters. This prevents 500s from invalid UUIDs reaching the
|
||||
// database layer.
|
||||
func ValidatePathParams() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
for _, p := range c.Params {
|
||||
if len(p.Value) > maxParamLen {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"error": "path parameter too long",
|
||||
})
|
||||
return
|
||||
}
|
||||
if strings.ContainsRune(p.Value, 0) {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"error": "invalid character in path parameter",
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user