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
+6 -6
View File
@@ -81,7 +81,7 @@ type rsaSingingKey struct {
}
func newRSASingingKey(signingMethod jwt.SigningMethod, key *rsa.PrivateKey) (rsaSingingKey, error) {
kid, err := util.CreatePublicKeyFingerprint(key.Public().(*rsa.PublicKey))
kid, err := util.CreatePublicKeyFingerprint(&key.PublicKey)
if err != nil {
return rsaSingingKey{}, err
}
@@ -110,7 +110,7 @@ func (key rsaSingingKey) VerifyKey() any {
}
func (key rsaSingingKey) ToJWK() (map[string]string, error) {
pubKey := key.key.Public().(*rsa.PublicKey)
pubKey := &key.key.PublicKey
return map[string]string{
"kty": "RSA",
@@ -132,7 +132,7 @@ type eddsaSigningKey struct {
}
func newEdDSASingingKey(signingMethod jwt.SigningMethod, key ed25519.PrivateKey) (eddsaSigningKey, error) {
kid, err := util.CreatePublicKeyFingerprint(key.Public().(ed25519.PublicKey))
kid, err := util.CreatePublicKeyFingerprint(key.Public().(ed25519.PublicKey)) //nolint:forcetypeassert // ed25519.PrivateKey.Public always returns ed25519.PublicKey
if err != nil {
return eddsaSigningKey{}, err
}
@@ -161,7 +161,7 @@ func (key eddsaSigningKey) VerifyKey() any {
}
func (key eddsaSigningKey) ToJWK() (map[string]string, error) {
pubKey := key.key.Public().(ed25519.PublicKey)
pubKey := key.key.Public().(ed25519.PublicKey) //nolint:forcetypeassert // ed25519.PrivateKey.Public always returns ed25519.PublicKey
return map[string]string{
"alg": key.SigningMethod().Alg(),
@@ -183,7 +183,7 @@ type ecdsaSingingKey struct {
}
func newECDSASingingKey(signingMethod jwt.SigningMethod, key *ecdsa.PrivateKey) (ecdsaSingingKey, error) {
kid, err := util.CreatePublicKeyFingerprint(key.Public().(*ecdsa.PublicKey))
kid, err := util.CreatePublicKeyFingerprint(&key.PublicKey)
if err != nil {
return ecdsaSingingKey{}, err
}
@@ -212,7 +212,7 @@ func (key ecdsaSingingKey) VerifyKey() any {
}
func (key ecdsaSingingKey) ToJWK() (map[string]string, error) {
pubKey := key.key.Public().(*ecdsa.PublicKey)
pubKey := &key.key.PublicKey
// PublicKey.Bytes returns the uncompressed SEC 1 format: 0x04 || X || Y
pubKeyBytes, err := pubKey.Bytes()
@@ -53,7 +53,8 @@ func TestECDSASigningKeyToJWK(t *testing.T) {
assert.Len(t, yBytes, tc.coordLen)
// Verify the decoded coordinates reconstruct the original public key point
pubKey := privKey.Public().(*ecdsa.PublicKey)
pubKey, ok := privKey.Public().(*ecdsa.PublicKey)
require.True(t, ok)
assert.Equal(t, 0, new(big.Int).SetBytes(xBytes).Cmp(pubKey.X))
assert.Equal(t, 0, new(big.Int).SetBytes(yBytes).Cmp(pubKey.Y))
})