Skip to content

Commit 2830fdf

Browse files
wxiaoguanglunny
authored andcommitted
Fix user's sign email check (#35045)
Fix #21692
1 parent 4e0269e commit 2830fdf

File tree

4 files changed

+109
-28
lines changed

4 files changed

+109
-28
lines changed

models/asymkey/ssh_key_fingerprint.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"xorm.io/builder"
1414
)
1515

16-
// The database is used in checkKeyFingerprint however most of these functions probably belong in a module
16+
// The database is used in checkKeyFingerprint. However, most of these functions probably belong in a module
1717

18-
// checkKeyFingerprint only checks if key fingerprint has been used as public key,
18+
// checkKeyFingerprint only checks if key fingerprint has been used as a public key,
1919
// it is OK to use same key as deploy key for multiple repositories/users.
2020
func checkKeyFingerprint(ctx context.Context, fingerprint string) error {
2121
has, err := db.Exist[PublicKey](ctx, builder.Eq{"fingerprint": fingerprint})

models/fixtures/public_key.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
created_unix: 1559593109
1010
updated_unix: 1565224552
1111
login_source_id: 0
12+
verified: false

services/asymkey/commit.go

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func ParseCommitWithSignature(ctx context.Context, c *git.Commit) *asymkey_model
4949
return ParseCommitWithSignatureCommitter(ctx, c, committer)
5050
}
5151

52+
// ParseCommitWithSignatureCommitter parses a commit's GPG or SSH signature.
53+
// The caller guarantees that the committer user is related to the commit by checking its activated email addresses or no-reply address.
54+
// If the commit is singed by an instance key, then committer can be nil.
55+
// If the signature exists, even if committer is nil, the returned CommittingUser will be a non-nil fake user (e.g.: instance key)
5256
func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, committer *user_model.User) *asymkey_model.CommitVerification {
5357
// If no signature just report the committer
5458
if c.Signature == nil {
@@ -117,20 +121,11 @@ func ParseCommitWithSignatureCommitter(ctx context.Context, c *git.Commit, commi
117121
}
118122
}
119123

120-
committerEmailAddresses, _ := cache.GetWithContextCache(ctx, cachegroup.UserEmailAddresses, committer.ID, user_model.GetEmailAddresses)
121-
activated := false
122-
for _, e := range committerEmailAddresses {
123-
if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
124-
activated = true
125-
break
126-
}
127-
}
128-
129124
for _, k := range keys {
130125
// Pre-check (& optimization) that emails attached to key can be attached to the committer email and can validate
131126
canValidate := false
132127
email := ""
133-
if k.Verified && activated {
128+
if k.Verified {
134129
canValidate = true
135130
email = c.Committer.Email
136131
}
@@ -220,8 +215,8 @@ func checkKeyEmails(ctx context.Context, email string, keys ...*asymkey_model.GP
220215
return true, e.Email
221216
}
222217
}
223-
if user.KeepEmailPrivate && strings.EqualFold(email, user.GetEmail()) {
224-
return true, user.GetEmail()
218+
if user != nil && strings.EqualFold(email, user.GetPlaceholderEmail()) {
219+
return true, user.GetPlaceholderEmail()
225220
}
226221
}
227222
}
@@ -376,21 +371,8 @@ func ParseCommitWithSSHSignature(ctx context.Context, c *git.Commit, committer *
376371
}
377372
}
378373

379-
committerEmailAddresses, err := cache.GetWithContextCache(ctx, cachegroup.UserEmailAddresses, committer.ID, user_model.GetEmailAddresses)
380-
if err != nil {
381-
log.Error("GetEmailAddresses: %v", err)
382-
}
383-
384-
activated := false
385-
for _, e := range committerEmailAddresses {
386-
if e.IsActivated && strings.EqualFold(e.Email, c.Committer.Email) {
387-
activated = true
388-
break
389-
}
390-
}
391-
392374
for _, k := range keys {
393-
if k.Verified && activated {
375+
if k.Verified {
394376
commitVerification := verifySSHCommitVerification(c.Signature.Signature, c.Signature.Payload, k, committer, committer, c.Committer.Email)
395377
if commitVerification != nil {
396378
return commitVerification

services/asymkey/commit_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package asymkey
5+
6+
import (
7+
"strings"
8+
"testing"
9+
10+
asymkey_model "code.gitea.io/gitea/models/asymkey"
11+
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/models/unittest"
13+
user_model "code.gitea.io/gitea/models/user"
14+
"code.gitea.io/gitea/modules/git"
15+
"code.gitea.io/gitea/modules/setting"
16+
"code.gitea.io/gitea/modules/test"
17+
18+
"github.com/stretchr/testify/assert"
19+
"github.com/stretchr/testify/require"
20+
)
21+
22+
func TestParseCommitWithSSHSignature(t *testing.T) {
23+
assert.NoError(t, unittest.PrepareTestDatabase())
24+
25+
// Here we only need to do some tests that "tests/integration/gpg_ssh_git_test.go" doesn't cover
26+
27+
// -----BEGIN OPENSSH PRIVATE KEY-----
28+
// b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
29+
// QyNTUxOQAAACC6T6zF0oPak8dOIzzT1kXB7LrcsVo04SKc3GjuvMllZwAAAJgy08upMtPL
30+
// qQAAAAtzc2gtZWQyNTUxOQAAACC6T6zF0oPak8dOIzzT1kXB7LrcsVo04SKc3GjuvMllZw
31+
// AAAEDWqPHTH51xb4hy1y1f1VeWL/2A9Q0b6atOyv5fx8x5prpPrMXSg9qTx04jPNPWRcHs
32+
// utyxWjThIpzcaO68yWVnAAAAEXVzZXIyQGV4YW1wbGUuY29tAQIDBA==
33+
// -----END OPENSSH PRIVATE KEY-----
34+
sshPubKey, err := asymkey_model.AddPublicKey(t.Context(), 999, "user-ssh-key-any-name", "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILpPrMXSg9qTx04jPNPWRcHsutyxWjThIpzcaO68yWVn", 0)
35+
require.NoError(t, err)
36+
_, err = db.GetEngine(t.Context()).ID(sshPubKey.ID).Cols("verified").Update(&asymkey_model.PublicKey{Verified: true})
37+
require.NoError(t, err)
38+
39+
t.Run("UserSSHKey", func(t *testing.T) {
40+
commit, err := git.CommitFromReader(nil, git.Sha1ObjectFormat.EmptyObjectID(), strings.NewReader(`tree a3b1fad553e0f9a2b4a58327bebde36c7da75aa2
41+
author user2 <[email protected]> 1752194028 -0700
42+
committer user2 <[email protected]> 1752194028 -0700
43+
gpgsig -----BEGIN SSH SIGNATURE-----
44+
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAguk+sxdKD2pPHTiM809ZFwey63L
45+
FaNOEinNxo7rzJZWcAAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
46+
AAAAQBfX+6mcKZBnXckwHcBFqRuXMD3vTKi1yv5wgrqIxTyr2LWB97xxmO92cvjsr0POQ2
47+
2YA7mQS510Cg2s1uU1XAk=
48+
-----END SSH SIGNATURE-----
49+
50+
init project
51+
`))
52+
require.NoError(t, err)
53+
54+
// the committingUser is guaranteed by the caller, parseCommitWithSSHSignature doesn't do any more checks
55+
committingUser := &user_model.User{ID: 999, Name: "user-x"}
56+
ret := ParseCommitWithSSHSignature(t.Context(), commit, committingUser)
57+
require.NotNil(t, ret)
58+
assert.True(t, ret.Verified)
59+
assert.Equal(t, committingUser.Name+" / "+sshPubKey.Fingerprint, ret.Reason)
60+
assert.False(t, ret.Warning)
61+
assert.Equal(t, committingUser, ret.SigningUser)
62+
assert.Equal(t, committingUser, ret.CommittingUser)
63+
assert.Equal(t, sshPubKey.ID, ret.SigningSSHKey.ID)
64+
})
65+
66+
t.Run("TrustedSSHKey", func(t *testing.T) {
67+
defer test.MockVariableValue(&setting.Repository.Signing.SigningName, "gitea")()
68+
defer test.MockVariableValue(&setting.Repository.Signing.SigningEmail, "[email protected]")()
69+
70+
commit, err := git.CommitFromReader(nil, git.Sha1ObjectFormat.EmptyObjectID(), strings.NewReader(`tree 9a93ffa76e8b72bdb6431910b3a506fa2b39f42e
71+
author User Two <[email protected]> 1749230009 +0200
72+
committer User Two <[email protected]> 1749230009 +0200
73+
gpgsig -----BEGIN SSH SIGNATURE-----
74+
U1NIU0lHAAAAAQAAADMAAAALc3NoLWVkMjU1MTkAAAAgfpjiJ1VpbcT5svDW6qgB8kPujl
75+
KK74epLnUT2hAs8T0AAAADZ2l0AAAAAAAAAAZzaGE1MTIAAABTAAAAC3NzaC1lZDI1NTE5
76+
AAAAQDX2t2iHuuLxEWHLJetYXKsgayv3c43r0pJNfAzdLN55Q65pC5M7rG6++gT2bxcpOu
77+
Y6EXbpLqia9sunEF3+LQY=
78+
-----END SSH SIGNATURE-----
79+
80+
Initial commit with signed file
81+
`))
82+
require.NoError(t, err)
83+
committingUser := &user_model.User{
84+
ID: 2,
85+
Name: "User Two",
86+
87+
}
88+
ret := ParseCommitWithSSHSignature(t.Context(), commit, committingUser)
89+
require.NotNil(t, ret)
90+
assert.True(t, ret.Verified)
91+
assert.False(t, ret.Warning)
92+
assert.Equal(t, committingUser, ret.CommittingUser)
93+
if assert.NotNil(t, ret.SigningUser) {
94+
assert.Equal(t, "gitea", ret.SigningUser.Name)
95+
assert.Equal(t, "[email protected]", ret.SigningUser.Email)
96+
}
97+
})
98+
}

0 commit comments

Comments
 (0)