mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AnonymousAuth: Fix concurrent read-write crash (#68637)
clone http req before tagging
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user