Skip to content

Commit 81c6c6c

Browse files
author
Ali
committed
config: guard against nil oauth2 credential in RoundTrip
toSecret returns (nil, nil) when no source is configured. Most callers guarded the returned SecretReader with an `!= nil` check before invoking Fetch, but oauth2RoundTripper.RoundTrip reached directly into rt.oauthCredential.Immutable() and would nil-deref panic when someone supplied an oauth2 block with no client-secret source (likely the proximate cause of prometheus/prometheus#16622). Return a clear error instead of panicking, and document toSecret's nil-return contract so future callers explicitly acknowledge it. Fixes #790 Signed-off-by: Ali <alliasgher123@gmail.com>
1 parent 9a26ab2 commit 81c6c6c

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

config/http_config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,10 @@ func (*refSecret) Immutable() bool {
828828
}
829829

830830
// toSecret returns a SecretReader from one of the given sources, assuming exactly
831-
// one or none of the sources are provided.
831+
// one or none of the sources are provided. When no source is provided it
832+
// returns (nil, nil); callers MUST guard the returned reader with a nil check
833+
// before invoking any method on it — see the nil-deref issue tracked at
834+
// https://github.com/prometheus/common/issues/790.
832835
func toSecret(secretManager SecretManager, text Secret, file, ref string) (SecretReader, error) {
833836
if text != "" {
834837
return NewInlineSecret(string(text)), nil
@@ -1053,6 +1056,15 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
10531056
needsInit bool
10541057
)
10551058

1059+
// oauthCredential can be nil when the caller constructed an oauth2
1060+
// config without any client-secret source (Secret/File/Ref all empty).
1061+
// That is an invalid config — oauth2 requires a client secret — but
1062+
// rather than panicking on the Immutable()/Fetch() calls below, surface
1063+
// it as a request-time error so the caller can see it.
1064+
if rt.oauthCredential == nil {
1065+
return nil, errors.New("oauth2 client secret is required")
1066+
}
1067+
10561068
rt.mtx.RLock()
10571069
secret = rt.lastSecret
10581070
needsInit = rt.lastRT.Source == nil

0 commit comments

Comments
 (0)