Skip to content

Commit b6d425f

Browse files
balkizeripath
authored andcommitted
Fix panic when an invalid oauth2 name is passed (go-gitea#20820)
Backport go-gitea#20820 When trying to access an invalid oauth2 link, we get an internal server error and can see a panic stack-trace in logs Example: Try to go to this url for a gitea installation https://<gitea_url>/user/oauth2/DoesNotExist?redirect_to= It causes an internal server error Stack trace in log ``` 2022/08/17 01:26:50 routers/web/base.go:134:1() [E] [62fc43da] PANIC: runtime error: invalid memory address or nil pointer dereference /usr/local/go/src/runtime/panic.go:220 (0x453095) /usr/local/go/src/runtime/signal_unix.go:818 (0x453065) /source/routers/web/auth/oauth.go:1100 (0x20f6ef7) /source/routers/web/auth/oauth.go:785 (0x20f4684) /source/modules/web/wrap_convert.go:47 (0x1f45196) /source/modules/web/wrap.go:41 (0x1f433c9) /usr/local/go/src/net/http/server.go:2084 (0x93cace) <clipped> ``` Root cause: In this [line](https://github.com/go-gitea/gitea/blob/a4e91c4197483c94f13e623c962b6b011494e949/models/auth/oauth2.go#L516) here, err is nil. The caller assumes no error and tries to access a `nil *Source`
1 parent 9e8b1c6 commit b6d425f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

models/auth/oauth2.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,14 @@ func GetActiveOAuth2ProviderSources() ([]*Source, error) {
512512
func GetActiveOAuth2SourceByName(name string) (*Source, error) {
513513
authSource := new(Source)
514514
has, err := db.GetEngine(db.DefaultContext).Where("name = ? and type = ? and is_active = ?", name, OAuth2, true).Get(authSource)
515-
if !has || err != nil {
515+
if err != nil {
516516
return nil, err
517517
}
518518

519+
if !has {
520+
return nil, fmt.Errorf("oauth2 source not found, name: %q", name)
521+
}
522+
519523
return authSource, nil
520524
}
521525

0 commit comments

Comments
 (0)