MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / ValidatePath

Function ValidatePath

internal/server/middleware_request_text.go:122–146  ·  view source on GitHub ↗

ValidatePath rejects a request whose percent-DECODED URL path is not a value the database can be asked about. Every handler that resolves a workspace, collection, item, comment or attachment from a path segment hands that segment to the store verbatim, and the store binds it into a text comparison.

(decorate func(http.Handler) http.Handler)

Source from the content-addressed store, hash-verified

120//
121// BUG-2782.
122func ValidatePath(decorate func(http.Handler) http.Handler) func(http.Handler) http.Handler {
123 var reject http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
124 // Set explicitly rather than relying on jsonContentType, which is
125 // mounted below this middleware and never runs for a rejection.
126 // Without it net/http sniffs the JSON body as text/plain.
127 w.Header().Set("Content-Type", "application/json")
128 // Not "is not valid UTF-8": a NUL is valid UTF-8 and is rejected
129 // here too, so that wording would be false for half the inputs
130 // this refuses.
131 writeError(w, http.StatusBadRequest, "invalid_path",
132 "Request path contains invalid UTF-8 or a NUL byte")
133 })
134 if decorate != nil {
135 reject = decorate(reject)
136 }
137 return func(next http.Handler) http.Handler {
138 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
139 if !bindableText(r.URL.Path) {
140 reject.ServeHTTP(w, r)
141 return
142 }
143 next.ServeHTTP(w, r)
144 })
145 }
146}
147
148// bindableText reports whether a decoded string can be bound into a text
149// comparison. It is the shared predicate of both middlewares in this file:

Callers 1

setupRouterMethod · 0.85

Calls 5

writeErrorFunction · 0.85
bindableTextFunction · 0.85
SetMethod · 0.45
HeaderMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected