mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Requester/authn: check the interface for nil value to avoid false positives (#90210)
This commit is contained in:
parent
5ae5fa3a7a
commit
3eef9b3397
@ -3,6 +3,7 @@ package identity
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ctxUserKey struct{}
|
type ctxUserKey struct{}
|
||||||
@ -16,8 +17,12 @@ func WithRequester(ctx context.Context, usr Requester) context.Context {
|
|||||||
func GetRequester(ctx context.Context) (Requester, error) {
|
func GetRequester(ctx context.Context) (Requester, error) {
|
||||||
// Set by appcontext.WithUser
|
// Set by appcontext.WithUser
|
||||||
u, ok := ctx.Value(ctxUserKey{}).(Requester)
|
u, ok := ctx.Value(ctxUserKey{}).(Requester)
|
||||||
if ok && u != nil {
|
if ok && !checkNilRequester(u) {
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("a Requester was not found in the context")
|
return nil, fmt.Errorf("a Requester was not found in the context")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkNilRequester(r Requester) bool {
|
||||||
|
return r == nil || (reflect.ValueOf(r).Kind() == reflect.Ptr && reflect.ValueOf(r).IsNil())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user