fix(middlewares): fix data race in WithEndpoint() BE-11949 (#803)

release/2.32.0-rc1
andres-portainer 2025-06-16 12:56:51 -03:00 committed by GitHub
parent f07a3b1875
commit 036b87b649
1 changed files with 4 additions and 5 deletions

View File

@ -25,12 +25,12 @@ type key int
const contextEndpoint key = 0 const contextEndpoint key = 0
func WithEndpoint(endpointService dataservices.EndpointService, endpointIDParam string) mux.MiddlewareFunc { func WithEndpoint(endpointService dataservices.EndpointService, endpointIDParam string) mux.MiddlewareFunc {
if endpointIDParam == "" {
endpointIDParam = "id"
}
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) { return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) {
if endpointIDParam == "" {
endpointIDParam = "id"
}
endpointID, err := requesthelpers.RetrieveNumericRouteVariableValue(request, endpointIDParam) endpointID, err := requesthelpers.RetrieveNumericRouteVariableValue(request, endpointIDParam)
if err != nil { if err != nil {
httperror.WriteError(rw, http.StatusBadRequest, "Invalid environment identifier route variable", err) httperror.WriteError(rw, http.StatusBadRequest, "Invalid environment identifier route variable", err)
@ -51,7 +51,6 @@ func WithEndpoint(endpointService dataservices.EndpointService, endpointIDParam
ctx := context.WithValue(request.Context(), contextEndpoint, endpoint) ctx := context.WithValue(request.Context(), contextEndpoint, endpoint)
next.ServeHTTP(rw, request.WithContext(ctx)) next.ServeHTTP(rw, request.WithContext(ctx))
}) })
} }
} }