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 10:25:06 +00:00
committed by GitHub
co-authored by wxiaoguang
parent fac8bf2eca
commit 76a81b24f9
248 changed files with 1110 additions and 995 deletions
+8 -10
View File
@@ -41,17 +41,16 @@ func TwoFactor(ctx *context.Context) {
// TwoFactorPost validates a user's two-factor authentication token.
func TwoFactorPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.TwoFactorAuthForm)
form := web.GetForm[*forms.TwoFactorAuthForm](ctx)
ctx.Data["Title"] = ctx.Tr("twofa")
// Ensure user is in a 2FA session.
idSess := ctx.Session.Get("twofaUid")
if idSess == nil {
id, hasSession := ctx.Session.Get("twofaUid").(int64)
if !hasSession {
ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
return
}
id := idSess.(int64)
twofa, err := auth.GetTwoFactorByUID(ctx, id)
if err != nil {
ctx.ServerError("UserSignIn", err)
@@ -66,7 +65,7 @@ func TwoFactorPost(ctx *context.Context) {
}
if ok {
remember := ctx.Session.Get("twofaRemember").(bool)
remember := ctx.Session.Get("twofaRemember").(bool) //nolint:forcetypeassert // must exist
u, err := user_model.GetUserByID(ctx, id)
if err != nil {
ctx.ServerError("UserSignIn", err)
@@ -105,17 +104,16 @@ func TwoFactorScratch(ctx *context.Context) {
// TwoFactorScratchPost validates and invalidates a user's two-factor scratch token.
func TwoFactorScratchPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.TwoFactorScratchAuthForm)
form := web.GetForm[*forms.TwoFactorScratchAuthForm](ctx)
ctx.Data["Title"] = ctx.Tr("twofa_scratch")
// Ensure user is in a 2FA session.
idSess := ctx.Session.Get("twofaUid")
if idSess == nil {
id, hasSession := ctx.Session.Get("twofaUid").(int64)
if !hasSession {
ctx.ServerError("UserSignIn", errors.New("not in 2FA session"))
return
}
id := idSess.(int64)
twofa, err := auth.GetTwoFactorByUID(ctx, id)
if err != nil {
ctx.ServerError("UserSignIn", err)
@@ -135,7 +133,7 @@ func TwoFactorScratchPost(ctx *context.Context) {
return
}
remember := ctx.Session.Get("twofaRemember").(bool)
remember := ctx.Session.Get("twofaRemember").(bool) //nolint:forcetypeassert // must exist
u, err := user_model.GetUserByID(ctx, id)
if err != nil {
ctx.ServerError("UserSignIn", err)