Skip to content

Commit deaabf6

Browse files
GiteaBotKN4CK3R
andauthored
Fix missing 0 prefix of GPG key id (#30245) (#30248)
Backport #30245 by @KN4CK3R Fixes #30235 If the key id "front" byte has a single digit, `%X` is missing the 0 prefix. ` 38D1A3EADDBEA9C` instead of `038D1A3EADDBEA9C` When using the `IssuerFingerprint` slice `%X` is enough but I changed it to `%016X` too to be consistent. Co-authored-by: KN4CK3R <[email protected]>
1 parent 39322d9 commit deaabf6

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

models/asymkey/gpg_key_commit_verification.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,7 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *CommitVerific
139139
}
140140
}
141141

142-
keyID := ""
143-
if sig.IssuerKeyId != nil && (*sig.IssuerKeyId) != 0 {
144-
keyID = fmt.Sprintf("%X", *sig.IssuerKeyId)
145-
}
146-
if keyID == "" && sig.IssuerFingerprint != nil && len(sig.IssuerFingerprint) > 0 {
147-
keyID = fmt.Sprintf("%X", sig.IssuerFingerprint[12:20])
148-
}
142+
keyID := tryGetKeyIDFromSignature(sig)
149143
defaultReason := NoKeyFound
150144

151145
// First check if the sig has a keyID and if so just look at that

models/asymkey/gpg_key_common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,13 @@ func extractSignature(s string) (*packet.Signature, error) {
134134
}
135135
return sig, nil
136136
}
137+
138+
func tryGetKeyIDFromSignature(sig *packet.Signature) string {
139+
if sig.IssuerKeyId != nil && (*sig.IssuerKeyId) != 0 {
140+
return fmt.Sprintf("%016X", *sig.IssuerKeyId)
141+
}
142+
if sig.IssuerFingerprint != nil && len(sig.IssuerFingerprint) > 0 {
143+
return fmt.Sprintf("%016X", sig.IssuerFingerprint[12:20])
144+
}
145+
return ""
146+
}

models/asymkey/gpg_key_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"code.gitea.io/gitea/models/unittest"
1212
user_model "code.gitea.io/gitea/models/user"
1313
"code.gitea.io/gitea/modules/timeutil"
14+
"code.gitea.io/gitea/modules/util"
1415

16+
"github.com/keybase/go-crypto/openpgp/packet"
1517
"github.com/stretchr/testify/assert"
1618
)
1719

@@ -391,3 +393,13 @@ epiDVQ==
391393
assert.Equal(t, time.Unix(1586105389, 0), expire)
392394
}
393395
}
396+
397+
func TestTryGetKeyIDFromSignature(t *testing.T) {
398+
assert.Empty(t, tryGetKeyIDFromSignature(&packet.Signature{}))
399+
assert.Equal(t, "038D1A3EADDBEA9C", tryGetKeyIDFromSignature(&packet.Signature{
400+
IssuerKeyId: util.ToPointer(uint64(0x38D1A3EADDBEA9C)),
401+
}))
402+
assert.Equal(t, "038D1A3EADDBEA9C", tryGetKeyIDFromSignature(&packet.Signature{
403+
IssuerFingerprint: []uint8{0xb, 0x23, 0x24, 0xc7, 0xe6, 0xfe, 0x4f, 0x3a, 0x6, 0x26, 0xc1, 0x21, 0x3, 0x8d, 0x1a, 0x3e, 0xad, 0xdb, 0xea, 0x9c},
404+
}))
405+
}

0 commit comments

Comments
 (0)