Skip to content

Commit 2ebd0ec

Browse files
authored
fix: resolve nil pointer dereference on missing oauth config (#8352)
1 parent 9f5bc7c commit 2ebd0ec

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

coderd/httpmw/apikey.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ func ExtractAPIKey(rw http.ResponseWriter, r *http.Request, cfg ExtractAPIKeyCon
237237
}
238238
// Check if the OAuth token is expired
239239
if link.OAuthExpiry.Before(now) && !link.OAuthExpiry.IsZero() && link.OAuthRefreshToken != "" {
240+
if cfg.OAuth2Configs == nil {
241+
return write(http.StatusInternalServerError, codersdk.Response{
242+
Message: internalErrorMessage,
243+
Detail: fmt.Sprintf("Unable to refresh OAuth token for login type %q. "+
244+
"No OAuth2Configs provided. Contact an administrator to configure this login type.", key.LoginType),
245+
})
246+
}
240247
var oauthConfig OAuth2Config
241248
switch key.LoginType {
242249
case database.LoginTypeGithub:

coderd/httpmw/apikey_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/sha256"
66
"fmt"
7+
"io"
78
"net"
89
"net/http"
910
"net/http/httptest"
@@ -595,4 +596,39 @@ func TestAPIKey(t *testing.T) {
595596
require.Equal(t, sentAPIKey.ExpiresAt, gotAPIKey.ExpiresAt)
596597
require.Equal(t, sentAPIKey.LoginType, gotAPIKey.LoginType)
597598
})
599+
600+
t.Run("MissongConfig", func(t *testing.T) {
601+
t.Parallel()
602+
var (
603+
db = dbfake.New()
604+
user = dbgen.User(t, db, database.User{})
605+
_, token = dbgen.APIKey(t, db, database.APIKey{
606+
UserID: user.ID,
607+
LastUsed: database.Now(),
608+
ExpiresAt: database.Now().AddDate(0, 0, 1),
609+
LoginType: database.LoginTypeOIDC,
610+
})
611+
_ = dbgen.UserLink(t, db, database.UserLink{
612+
UserID: user.ID,
613+
LoginType: database.LoginTypeOIDC,
614+
OAuthRefreshToken: "random",
615+
// expired
616+
OAuthExpiry: time.Now().Add(time.Hour * -1),
617+
})
618+
619+
r = httptest.NewRequest("GET", "/", nil)
620+
rw = httptest.NewRecorder()
621+
)
622+
r.Header.Set(codersdk.SessionTokenHeader, token)
623+
624+
httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
625+
DB: db,
626+
RedirectToLogin: false,
627+
})(successHandler).ServeHTTP(rw, r)
628+
res := rw.Result()
629+
defer res.Body.Close()
630+
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
631+
out, _ := io.ReadAll(res.Body)
632+
require.Contains(t, string(out), "Unable to refresh")
633+
})
598634
}

0 commit comments

Comments
 (0)