chore: enable forcetypeassert linter, fix issues (#38804)

Enable [`forcetypeassert`](https://github.com/gostaticanalysis/forcetypeassert)
linter to prevent unchecked type assertions. ~650 issues fixed, most
fixes were clean, some use `setting.PanicInDevOrTesting`.

The only behaviour changes are where code would previously send a 500 error
or panic, a 4xx error is now emitted.

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
silverwind
2026-08-09 12:25:06 +02:00
committed by GitHub
parent fac8bf2eca
commit 76a81b24f9
248 changed files with 1110 additions and 995 deletions
+2 -2
View File
@@ -56,7 +56,7 @@ func AccountPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.ChangePasswordForm)
form := web.GetForm[*forms.ChangePasswordForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsAccount"] = true
ctx.Data["Email"] = ctx.Doer.Email
@@ -106,7 +106,7 @@ func EmailPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.AddEmailForm)
form := web.GetForm[*forms.AddEmailForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsAccount"] = true
ctx.Data["Email"] = ctx.Doer.Email
+1 -1
View File
@@ -34,7 +34,7 @@ func Applications(ctx *context.Context) {
// ApplicationsPost response for add user's access token
func ApplicationsPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewAccessTokenForm)
form := web.GetForm[*forms.NewAccessTokenForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsApplications"] = true
+13 -9
View File
@@ -43,7 +43,7 @@ func Keys(ctx *context.Context) {
// KeysPost response for change user's SSH/GPG keys
func KeysPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AddKeyForm)
form := web.GetForm[*forms.AddKeyForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsKeys"] = true
ctx.Data["DisableSSH"] = setting.SSH.Disabled
@@ -98,6 +98,8 @@ func KeysPost(ctx *context.Context) {
}
if err != nil {
ctx.Data["HasGPGError"] = true
var errInvalidTokenSignature asymkey_model.ErrGPGInvalidTokenSignature
var errNoEmailFound asymkey_model.ErrGPGNoEmailFound
switch {
case asymkey_model.IsErrGPGKeyParsing(err):
ctx.Flash.Error(ctx.Tr("form.invalid_gpg_key", err.Error()))
@@ -107,20 +109,20 @@ func KeysPost(ctx *context.Context) {
ctx.Data["Err_Content"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("settings.gpg_key_id_used"), tplSettingsKeys, &form)
case asymkey_model.IsErrGPGInvalidTokenSignature(err):
case errors.As(err, &errInvalidTokenSignature):
loadKeysData(ctx)
ctx.Data["Err_Content"] = true
ctx.Data["Err_Signature"] = true
keyID := err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
keyID := errInvalidTokenSignature.ID
ctx.Data["KeyID"] = keyID
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
ctx.RenderWithErrDeprecated(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form)
case asymkey_model.IsErrGPGNoEmailFound(err):
case errors.As(err, &errNoEmailFound):
loadKeysData(ctx)
ctx.Data["Err_Content"] = true
ctx.Data["Err_Signature"] = true
keyID := err.(asymkey_model.ErrGPGNoEmailFound).ID
keyID := errNoEmailFound.ID
ctx.Data["KeyID"] = keyID
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
ctx.RenderWithErrDeprecated(ctx.Tr("settings.gpg_no_key_email_found"), tplSettingsKeys, &form)
@@ -149,12 +151,13 @@ func KeysPost(ctx *context.Context) {
}
if err != nil {
ctx.Data["HasGPGVerifyError"] = true
var errInvalidTokenSignature asymkey_model.ErrGPGInvalidTokenSignature
switch {
case asymkey_model.IsErrGPGInvalidTokenSignature(err):
case errors.As(err, &errInvalidTokenSignature):
loadKeysData(ctx)
ctx.Data["VerifyingID"] = form.KeyID
ctx.Data["Err_Signature"] = true
keyID := err.(asymkey_model.ErrGPGInvalidTokenSignature).ID
keyID := errInvalidTokenSignature.ID
ctx.Data["KeyID"] = keyID
ctx.Data["PaddedKeyID"] = asymkey_model.PaddedKeyID(keyID)
ctx.RenderWithErrDeprecated(ctx.Tr("settings.gpg_invalid_token_signature"), tplSettingsKeys, &form)
@@ -224,11 +227,12 @@ func KeysPost(ctx *context.Context) {
}
if err != nil {
ctx.Data["HasSSHVerifyError"] = true
var errInvalidTokenSignature asymkey_model.ErrSSHInvalidTokenSignature
switch {
case asymkey_model.IsErrSSHInvalidTokenSignature(err):
case errors.As(err, &errInvalidTokenSignature):
loadKeysData(ctx)
ctx.Data["Err_Signature"] = true
ctx.Data["Fingerprint"] = err.(asymkey_model.ErrSSHInvalidTokenSignature).Fingerprint
ctx.Data["Fingerprint"] = errInvalidTokenSignature.Fingerprint
ctx.RenderWithErrDeprecated(ctx.Tr("settings.ssh_invalid_token_signature"), tplSettingsKeys, &form)
default:
ctx.ServerError("VerifySSH", err)
+8 -13
View File
@@ -23,8 +23,8 @@ type OAuth2CommonHandlers struct {
TplAppEdit templates.TplName // the template for the application edit page
}
func (oa *OAuth2CommonHandlers) renderEditPage(ctx *context.Context) {
app := ctx.Data["App"].(*auth.OAuth2Application)
func (oa *OAuth2CommonHandlers) renderEditPage(ctx *context.Context, app *auth.OAuth2Application) {
ctx.Data["App"] = app
ctx.Data["FormActionPath"] = fmt.Sprintf("%s/%d", oa.BasePathEditPrefix, app.ID)
if ctx.ContextUser != nil && ctx.ContextUser.IsOrganization() {
@@ -39,7 +39,7 @@ func (oa *OAuth2CommonHandlers) renderEditPage(ctx *context.Context) {
// AddApp adds an oauth2 application
func (oa *OAuth2CommonHandlers) AddApp(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
form := web.GetForm[*forms.EditOAuth2ApplicationForm](ctx)
if ctx.HasError() {
ctx.Flash.Error(ctx.GetErrMsg())
// go to the application list page
@@ -61,14 +61,13 @@ func (oa *OAuth2CommonHandlers) AddApp(ctx *context.Context) {
// render the edit page with secret
ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"), true)
ctx.Data["App"] = app
ctx.Data["ClientSecret"], err = app.GenerateClientSecret(ctx)
if err != nil {
ctx.ServerError("GenerateClientSecret", err)
return
}
oa.renderEditPage(ctx)
oa.renderEditPage(ctx, app)
}
// EditShow displays the given application
@@ -86,13 +85,12 @@ func (oa *OAuth2CommonHandlers) EditShow(ctx *context.Context) {
ctx.NotFound(nil)
return
}
ctx.Data["App"] = app
oa.renderEditPage(ctx)
oa.renderEditPage(ctx, app)
}
// EditSave saves the oauth2 application
func (oa *OAuth2CommonHandlers) EditSave(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
form := web.GetForm[*forms.EditOAuth2ApplicationForm](ctx)
if ctx.HasError() {
app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.PathParamInt64("id"))
@@ -108,9 +106,7 @@ func (oa *OAuth2CommonHandlers) EditSave(ctx *context.Context) {
ctx.NotFound(nil)
return
}
ctx.Data["App"] = app
oa.renderEditPage(ctx)
oa.renderEditPage(ctx, app)
return
}
@@ -145,14 +141,13 @@ func (oa *OAuth2CommonHandlers) RegenerateSecret(ctx *context.Context) {
ctx.NotFound(nil)
return
}
ctx.Data["App"] = app
ctx.Data["ClientSecret"], err = app.GenerateClientSecret(ctx)
if err != nil {
ctx.ServerError("GenerateClientSecret", err)
return
}
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"), true)
oa.renderEditPage(ctx)
oa.renderEditPage(ctx, app)
}
// DeleteApp deletes the given oauth2 application
+4 -4
View File
@@ -65,7 +65,7 @@ func ProfilePost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.UpdateProfileForm)
form := web.GetForm[*forms.UpdateProfileForm](ctx)
if form.Name != "" {
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureChangeUsername) {
@@ -175,7 +175,7 @@ func UpdateAvatarSetting(ctx *context.Context, form *forms.AvatarForm, ctxUser *
// AvatarPost response for change user's avatar request
func AvatarPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AvatarForm)
form := web.GetForm[*forms.AvatarForm](ctx)
if err := UpdateAvatarSetting(ctx, form, ctx.Doer); err != nil {
ctx.Flash.Error(err.Error())
} else {
@@ -354,7 +354,7 @@ func Appearance(ctx *context.Context) {
// UpdateUIThemePost is used to update users' specific theme
func UpdateUIThemePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.UpdateThemeForm)
form := web.GetForm[*forms.UpdateThemeForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsAppearance"] = true
@@ -384,7 +384,7 @@ func UpdateUIThemePost(ctx *context.Context) {
// UpdateUserLang update a user's language
func UpdateUserLang(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.UpdateLanguageForm)
form := web.GetForm[*forms.UpdateLanguageForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsAppearance"] = true
+5 -7
View File
@@ -100,9 +100,8 @@ func DisableTwoFactor(ctx *context.Context) {
func twofaGenerateSecretAndQr(ctx *context.Context) bool {
var otpKey *otp.Key
var err error
uri := ctx.Session.Get("twofaUri")
if uri != nil {
otpKey, err = otp.NewKeyFromURL(uri.(string))
if uri, ok := ctx.Session.Get("twofaUri").(string); ok {
otpKey, err = otp.NewKeyFromURL(uri)
if err != nil {
ctx.ServerError("SettingsTwoFactor: Failed NewKeyFromURL: ", err)
return false
@@ -193,7 +192,7 @@ func EnrollTwoFactorPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.TwoFactorAuthForm)
form := web.GetForm[*forms.TwoFactorAuthForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsSecurity"] = true
ctx.Data["ShowTwoFactorRequiredMessage"] = false
@@ -218,14 +217,13 @@ func EnrollTwoFactorPost(ctx *context.Context) {
return
}
secretRaw := ctx.Session.Get("twofaSecret")
if secretRaw == nil {
secret, ok := ctx.Session.Get("twofaSecret").(string)
if !ok {
ctx.Flash.Error(ctx.Tr("settings.twofa_failed_get_secret"))
ctx.Redirect(setting.AppSubURL + "/user/settings/security/two_factor/enroll")
return
}
secret := secretRaw.(string)
if !totp.Validate(form.Passcode, secret) {
if !twofaGenerateSecretAndQr(ctx) {
return
+1 -1
View File
@@ -24,7 +24,7 @@ func OpenIDPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.AddOpenIDForm)
form := web.GetForm[*forms.AddOpenIDForm](ctx)
ctx.Data["Title"] = ctx.Tr("settings_title")
ctx.Data["PageIsSettingsSecurity"] = true
@@ -30,7 +30,7 @@ func WebAuthnRegister(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.WebauthnRegistrationForm)
form := web.GetForm[*forms.WebauthnRegistrationForm](ctx)
if form.Name == "" {
// Set name to the hexadecimal of the current time
form.Name = strconv.FormatInt(time.Now().UnixNano(), 16)