Skip to content

Commit 84812e4

Browse files
Fix SSH signing key path will be displayed in the pull request UI (#35381)
Closes #35361 --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 16e1207 commit 84812e4

File tree

8 files changed

+70
-18
lines changed

8 files changed

+70
-18
lines changed

models/asymkey/gpg_key_commit_verification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type CommitVerification struct {
2525
SigningUser *user_model.User // if Verified, then SigningUser is non-nil
2626
CommittingUser *user_model.User // if Verified, then CommittingUser is non-nil
2727
SigningEmail string
28-
SigningKey *GPGKey
28+
SigningKey *GPGKey // FIXME: need to refactor it to a new name like "SigningGPGKey", it is also used in some templates
2929
SigningSSHKey *PublicKey
3030
TrustStatus string
3131
}

models/asymkey/key_display.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package asymkey
5+
6+
import (
7+
"os"
8+
9+
"code.gitea.io/gitea/modules/git"
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/modules/setting"
12+
)
13+
14+
func GetDisplaySigningKey(key *git.SigningKey) string {
15+
if key == nil || key.Format == "" {
16+
return ""
17+
}
18+
19+
switch key.Format {
20+
case git.SigningKeyFormatOpenPGP:
21+
return key.KeyID
22+
case git.SigningKeyFormatSSH:
23+
content, err := os.ReadFile(key.KeyID)
24+
if err != nil {
25+
log.Error("Unable to read SSH key %s: %v", key.KeyID, err)
26+
return "(Unable to read SSH key)"
27+
}
28+
display, err := CalcFingerprint(string(content))
29+
if err != nil {
30+
log.Error("Unable to calculate fingerprint for SSH key %s: %v", key.KeyID, err)
31+
return "(Unable to calculate fingerprint for SSH key)"
32+
}
33+
return display
34+
}
35+
setting.PanicInDevOrTesting("Unknown signing key format: %s", key.Format)
36+
return "(Unknown key format)"
37+
}

modules/git/key.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33

44
package git
55

6+
import "code.gitea.io/gitea/modules/setting"
7+
68
// Based on https://git-scm.com/docs/git-config#Documentation/git-config.txt-gpgformat
79
const (
810
SigningKeyFormatOpenPGP = "openpgp" // for GPG keys, the expected default of git cli
911
SigningKeyFormatSSH = "ssh"
1012
)
1113

14+
// SigningKey represents an instance key info which will be used to sign git commits.
15+
// FIXME: need to refactor it to a new name, this name conflicts with the variable names for "asymkey.GPGKey" in many places.
1216
type SigningKey struct {
1317
KeyID string
1418
Format string
1519
}
20+
21+
func (s *SigningKey) String() string {
22+
// Do not expose KeyID
23+
// In case the key is a file path and the struct is rendered in a template, then the server path will be exposed.
24+
setting.PanicInDevOrTesting("don't call SigningKey.String() - it exposes the KeyID which might be a local file path")
25+
return "SigningKey:" + s.Format
26+
}

routers/web/repo/issue_view.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313

1414
activities_model "code.gitea.io/gitea/models/activities"
15+
asymkey_model "code.gitea.io/gitea/models/asymkey"
1516
"code.gitea.io/gitea/models/db"
1617
git_model "code.gitea.io/gitea/models/git"
1718
issues_model "code.gitea.io/gitea/models/issues"
@@ -494,7 +495,7 @@ func preparePullViewSigning(ctx *context.Context, issue *issues_model.Issue) {
494495
if ctx.Doer != nil {
495496
sign, key, _, err := asymkey_service.SignMerge(ctx, pull, ctx.Doer, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitHeadRefName())
496497
ctx.Data["WillSign"] = sign
497-
ctx.Data["SigningKey"] = key
498+
ctx.Data["SigningKeyMergeDisplay"] = asymkey_model.GetDisplaySigningKey(key)
498499
if err != nil {
499500
if asymkey_service.IsErrWontSign(err) {
500501
ctx.Data["WontSignReason"] = err.(*asymkey_service.ErrWontSign).Reason

services/context/repo.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path"
1515
"strings"
1616

17+
asymkey_model "code.gitea.io/gitea/models/asymkey"
1718
"code.gitea.io/gitea/models/db"
1819
git_model "code.gitea.io/gitea/models/git"
1920
issues_model "code.gitea.io/gitea/models/issues"
@@ -99,7 +100,7 @@ type CommitFormOptions struct {
99100
UserCanPush bool
100101
RequireSigned bool
101102
WillSign bool
102-
SigningKey *git.SigningKey
103+
SigningKeyFormDisplay string
103104
WontSignReason string
104105
CanCreatePullRequest bool
105106
CanCreateBasePullRequest bool
@@ -139,7 +140,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
139140
protectionRequireSigned = protectedBranch.RequireSignedCommits
140141
}
141142

142-
willSign, signKeyID, _, err := asymkey_service.SignCRUDAction(ctx, targetRepo.RepoPath(), doer, targetRepo.RepoPath(), refName.String())
143+
willSign, signKey, _, err := asymkey_service.SignCRUDAction(ctx, targetRepo.RepoPath(), doer, targetRepo.RepoPath(), refName.String())
143144
wontSignReason := ""
144145
if asymkey_service.IsErrWontSign(err) {
145146
wontSignReason = string(err.(*asymkey_service.ErrWontSign).Reason)
@@ -156,14 +157,14 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
156157
canCreatePullRequest := targetRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || canCreateBasePullRequest
157158

158159
opts := &CommitFormOptions{
159-
TargetRepo: targetRepo,
160-
WillSubmitToFork: submitToForkedRepo,
161-
CanCommitToBranch: canCommitToBranch,
162-
UserCanPush: canPushWithProtection,
163-
RequireSigned: protectionRequireSigned,
164-
WillSign: willSign,
165-
SigningKey: signKeyID,
166-
WontSignReason: wontSignReason,
160+
TargetRepo: targetRepo,
161+
WillSubmitToFork: submitToForkedRepo,
162+
CanCommitToBranch: canCommitToBranch,
163+
UserCanPush: canPushWithProtection,
164+
RequireSigned: protectionRequireSigned,
165+
WillSign: willSign,
166+
SigningKeyFormDisplay: asymkey_model.GetDisplaySigningKey(signKey),
167+
WontSignReason: wontSignReason,
167168

168169
CanCreatePullRequest: canCreatePullRequest,
169170
CanCreateBasePullRequest: canCreateBasePullRequest,

templates/repo/commit_sign_badge.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ so this template should be kept as small as possbile, DO NOT put large component
88
*/}}
99
{{- $commit := $.Commit -}}
1010
{{- $commitBaseLink := $.CommitBaseLink -}}
11-
{{- $verification := $.CommitSignVerification -}}
11+
{{- $verification := $.CommitSignVerification -}}{{- /* asymkey.CommitVerification */ -}}
1212

1313
{{- $extraClass := "" -}}
1414
{{- $verified := false -}}
@@ -50,7 +50,7 @@ so this template should be kept as small as possbile, DO NOT put large component
5050

5151
{{- if $verification.SigningSSHKey -}}
5252
{{- $msgSigningKey = print (ctx.Locale.Tr "repo.commits.ssh_key_fingerprint") ": " $verification.SigningSSHKey.Fingerprint -}}
53-
{{- else if $verification.SigningKey -}}
53+
{{- else if $verification.SigningKey -}}{{- /* asymkey.GPGKey */ -}}
5454
{{- $msgSigningKey = print (ctx.Locale.Tr "repo.commits.gpg_key_id") ": " $verification.SigningKey.PaddedKeyID -}}
5555
{{- end -}}
5656
{{- end -}}

templates/repo/editor/commit_form.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<div class="commit-form-wrapper">
22
{{ctx.AvatarUtils.Avatar .SignedUser 40 "commit-avatar"}}
33
<div class="commit-form avatar-content-left-arrow">
4-
<h3>{{- if .CommitFormOptions.WillSign}}
5-
<span title="{{ctx.Locale.Tr "repo.signing.will_sign" .CommitFormOptions.SigningKey}}">{{svg "octicon-lock" 24}}</span>
4+
<h3>
5+
{{- if .CommitFormOptions.WillSign}}
6+
<span data-tooltip-content="{{ctx.Locale.Tr "repo.signing.will_sign" .CommitFormOptions.SigningKeyFormDisplay}}">{{svg "octicon-lock" 24}}</span>
67
{{ctx.Locale.Tr "repo.editor.commit_signed_changes"}}
78
{{- else}}
89
<span title="{{ctx.Locale.Tr (printf "repo.signing.wont_sign.%s" .CommitFormOptions.WontSignReason)}}">{{svg "octicon-unlock" 24}}</span>
910
{{ctx.Locale.Tr "repo.editor.commit_changes"}}
10-
{{- end}}</h3>
11+
{{- end}}
12+
</h3>
1113
<div class="field">
1214
<input name="commit_summary" maxlength="100" placeholder="{{if .PageIsDelete}}{{ctx.Locale.Tr "repo.editor.delete" .TreePath}}{{else if .PageIsUpload}}{{ctx.Locale.Tr "repo.editor.upload_files_to_dir" .TreePath}}{{else if .IsNewFile}}{{ctx.Locale.Tr "repo.editor.add_tmpl"}}{{else if .PageIsPatch}}{{ctx.Locale.Tr "repo.editor.patch"}}{{else}}{{ctx.Locale.Tr "repo.editor.update" .TreePath}}{{end}}" value="{{.commit_summary}}" autofocus>
1315
</div>

templates/repo/issue/view_content/pull_merge_box.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
{{if .WillSign}}
189189
<div class="item">
190190
{{svg "octicon-lock" 16 "text green"}}
191-
{{ctx.Locale.Tr "repo.signing.will_sign" .SigningKey}}
191+
{{ctx.Locale.Tr "repo.signing.will_sign" .SigningKeyMergeDisplay}}
192192
</div>
193193
{{else if .IsSigned}}
194194
<div class="item">

0 commit comments

Comments
 (0)