[v10.0.x] AnonymousAuth: Fix concurrent read-write crash (#68803)
AnonymousAuth: Fix concurrent read-write crash (#68637)
clone http req before tagging
(cherry picked from commit 05e71d4c6b)
Co-authored-by: Jo <joao.guerreiro@grafana.com>
Co-authored-by: jguer <me@jguer.space>
This commit is contained in:
co-authored by
Jo
jguer
parent
e21236b55e
commit
5a6e6624e0
@@ -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)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -259,13 +259,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)
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user