AnonymousAuth: Fix concurrent read-write crash (#68637)

clone http req before tagging
This commit is contained in:
Jo
2023-05-22 13:27:28 +02:00
committed by GitHub
parent e8ca3c0409
commit 05e71d4c6b
2 changed files with 17 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package clients
import (
"context"
"net/http"
"strings"
"github.com/grafana/grafana/pkg/infra/log"
@@ -40,13 +41,20 @@ func (a *Anonymous) Authenticate(ctx context.Context, r *authn.Request) (*authn.
return nil, err
}
httpReqCopy := &http.Request{}
if r.HTTPRequest != nil && r.HTTPRequest.Header != nil {
// avoid r.HTTPRequest.Clone(context.Background()) as we do not require a full clone
httpReqCopy.Header = r.HTTPRequest.Header.Clone()
httpReqCopy.RemoteAddr = r.HTTPRequest.RemoteAddr
}
go func() {
defer func() {
if err := recover(); err != nil {
a.log.Warn("tag anon session panic", "err", err)
}
}()
if err := a.anonSessionService.TagSession(context.Background(), r.HTTPRequest); err != nil {
if err := a.anonSessionService.TagSession(context.Background(), httpReqCopy); err != nil {
a.log.Warn("failed to tag anonymous session", "error", err)
}
}()

View File

@@ -237,13 +237,20 @@ func (h *ContextHandler) initContextWithAnonymousUser(reqContext *contextmodel.R
return false
}
httpReqCopy := &http.Request{}
if reqContext.Req != nil && reqContext.Req.Header != nil {
// avoid r.HTTPRequest.Clone(context.Background()) as we do not require a full clone
httpReqCopy.Header = reqContext.Req.Header.Clone()
httpReqCopy.RemoteAddr = reqContext.Req.RemoteAddr
}
go func() {
defer func() {
if err := recover(); err != nil {
reqContext.Logger.Warn("tag anon session panic", "err", err)
}
}()
if err := h.anonSessionService.TagSession(context.Background(), reqContext.Req); err != nil {
if err := h.anonSessionService.TagSession(context.Background(), httpReqCopy); err != nil {
reqContext.Logger.Warn("Failed to tag anonymous session", "error", err)
}
}()