mirror of https://github.com/grafana/grafana
Security: fixes CVE-2022-29170 (#49240)
* Request interceptor: block redirects * handle location missing * Update pkg/infra/httpclient/httpclientprovider/host_redirect_validation_middleware.go Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> * Update pkg/infra/httpclient/httpclientprovider/host_redirect_validation_middleware.go Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> * linter * fixes tests Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>pull/49258/head
parent
33359aee6c
commit
aad2983350
@ -0,0 +1,37 @@ |
||||
package httpclientprovider |
||||
|
||||
import ( |
||||
"errors" |
||||
"net/http" |
||||
|
||||
"github.com/grafana/grafana/pkg/models" |
||||
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" |
||||
) |
||||
|
||||
const HostRedirectValidationMiddlewareName = "host-redirect-validation" |
||||
|
||||
func RedirectLimitMiddleware(reqValidator models.PluginRequestValidator) sdkhttpclient.Middleware { |
||||
return sdkhttpclient.NamedMiddlewareFunc(HostRedirectValidationMiddlewareName, func(opts sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper { |
||||
return sdkhttpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) { |
||||
res, err := next.RoundTrip(req) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if res.StatusCode >= 300 && res.StatusCode < 400 { |
||||
location, locationErr := res.Location() |
||||
if errors.Is(locationErr, http.ErrNoLocation) { |
||||
return res, nil |
||||
} |
||||
if locationErr != nil { |
||||
return nil, locationErr |
||||
} |
||||
|
||||
if validationErr := reqValidator.Validate(location.String(), nil); validationErr != nil { |
||||
return nil, validationErr |
||||
} |
||||
} |
||||
return res, nil |
||||
}) |
||||
}) |
||||
} |
Loading…
Reference in new issue