Requester/authn: check the interface for nil value to avoid false positives (#90210)

This commit is contained in:
Charandas 2024-07-08 16:30:03 -07:00 committed by GitHub
parent 5ae5fa3a7a
commit 3eef9b3397
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@ package identity
import (
"context"
"fmt"
"reflect"
)
type ctxUserKey struct{}
@ -16,8 +17,12 @@ func WithRequester(ctx context.Context, usr Requester) context.Context {
func GetRequester(ctx context.Context) (Requester, error) {
// Set by appcontext.WithUser
u, ok := ctx.Value(ctxUserKey{}).(Requester)
if ok && u != nil {
if ok && !checkNilRequester(u) {
return u, nil
}
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())
}