mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-27 02:33:43 +00:00
fix(asymkey): do not verify OpenPGP signatures with an SSH instance key, require git 2.18 (#39073)
With SIGNING_FORMAT = ssh the OpenPGP verification path builds its GPGSettings from the instance signing key but leaves the format empty, so it runs `gpg -a --export` on an SSH public key path. Depending on the local gpg setup that either exports nothing, so an OpenPGP signed commit reports gpg.error.generate_hash instead of a missing key, or it fails outright and logs an export error for every such commit. Both guards are needed. The first covers SIGNING_KEY set to a path with SIGNING_FORMAT=ssh; the second covers the shipped default SIGNING_KEY=default, where the format comes from git's own gpg.format and never gets reconciled with the hardcoded "openpgp". Drop either one and a working config goes back to broken. Also raise minimum git version to 2.18 which was already required before this change. Fixes: https://github.com/go-gitea/gitea/issues/37452 Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
+45
-69
@@ -9,94 +9,70 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"gitea.dev/modules/git/gitcmd"
|
||||
"gitea.dev/modules/process"
|
||||
"gitea.dev/modules/util"
|
||||
)
|
||||
|
||||
// GPGSettings represents the default GPG settings for this repository
|
||||
type GPGSettings struct {
|
||||
Sign bool
|
||||
KeyID string
|
||||
Email string
|
||||
Name string
|
||||
PublicKeyContent string
|
||||
Format string
|
||||
type CommitSignSettings struct {
|
||||
Sign bool
|
||||
Email string
|
||||
Name string
|
||||
|
||||
Format string // default to GPG
|
||||
KeyID string // GPG key id or SSH key file
|
||||
|
||||
cachedPublicKeyContent atomic.Pointer[string]
|
||||
}
|
||||
|
||||
// LoadPublicKeyContent will load the key from gpg
|
||||
func (gpgSettings *GPGSettings) LoadPublicKeyContent() error {
|
||||
if gpgSettings.PublicKeyContent != "" {
|
||||
return nil
|
||||
func (css *CommitSignSettings) PublicKeyContent() (string, error) {
|
||||
cached := css.cachedPublicKeyContent.Load()
|
||||
if cached != nil {
|
||||
return *cached, nil
|
||||
}
|
||||
|
||||
if gpgSettings.Format == SigningKeyFormatSSH {
|
||||
content, err := os.ReadFile(gpgSettings.KeyID)
|
||||
if css.Format == SigningKeyFormatSSH {
|
||||
content, err := os.ReadFile(css.KeyID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to read SSH public key file: %s, %w", gpgSettings.KeyID, err)
|
||||
return "", fmt.Errorf("unable to read SSH public key file: %s, %w", css.KeyID, err)
|
||||
}
|
||||
gpgSettings.PublicKeyContent = string(content)
|
||||
return nil
|
||||
s := string(content)
|
||||
css.cachedPublicKeyContent.Store(&s)
|
||||
return s, nil
|
||||
}
|
||||
content, stderr, err := process.GetManager().Exec(
|
||||
"gpg -a --export",
|
||||
"gpg", "-a", "--export", gpgSettings.KeyID)
|
||||
|
||||
content, stderr, err := process.GetManager().Exec("gpg -a --export", "gpg", "-a", "--export", css.KeyID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get default signing key: %s, %s, %w", gpgSettings.KeyID, stderr, err)
|
||||
return "", fmt.Errorf("unable to get default signing key: %s, %s, %w", css.KeyID, stderr, err)
|
||||
}
|
||||
gpgSettings.PublicKeyContent = content
|
||||
return nil
|
||||
css.cachedPublicKeyContent.Store(&content)
|
||||
return content, nil
|
||||
}
|
||||
|
||||
var (
|
||||
loadPublicGPGKeyMutex sync.RWMutex
|
||||
globalGPGSettings *GPGSettings
|
||||
)
|
||||
var GlobalCommitSignSettings = util.OnceValue[*CommitSignSettings]{
|
||||
Func: func() *CommitSignSettings {
|
||||
ctx := context.Background()
|
||||
css := &CommitSignSettings{}
|
||||
|
||||
// GetDefaultPublicGPGKey will return and cache the default public GPG settings
|
||||
func GetDefaultPublicGPGKey(ctx context.Context, forceUpdate bool) (*GPGSettings, error) {
|
||||
if !forceUpdate {
|
||||
loadPublicGPGKeyMutex.RLock()
|
||||
if globalGPGSettings != nil {
|
||||
defer loadPublicGPGKeyMutex.RUnlock()
|
||||
return globalGPGSettings, nil
|
||||
}
|
||||
loadPublicGPGKeyMutex.RUnlock()
|
||||
}
|
||||
// all errors are ignored because the keys might not exist
|
||||
// "--type=bool" resolves a valueless "commit.gpgsign" to true
|
||||
value, _, _ := gitcmd.NewCommand("config", "--global", "--default", "false", "--type=bool", "--get", "commit.gpgsign").RunStdString(ctx)
|
||||
css.Sign = strings.TrimSpace(value) == "true"
|
||||
|
||||
loadPublicGPGKeyMutex.Lock()
|
||||
defer loadPublicGPGKeyMutex.Unlock()
|
||||
signingKey, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.signingkey").RunStdString(ctx)
|
||||
css.KeyID = strings.TrimSpace(signingKey)
|
||||
css.Sign = css.Sign && css.KeyID != ""
|
||||
|
||||
if globalGPGSettings != nil && !forceUpdate {
|
||||
return globalGPGSettings, nil
|
||||
}
|
||||
format, _, _ := gitcmd.NewCommand("config", "--global", "--default", SigningKeyFormatOpenPGP, "--get", "gpg.format").RunStdString(ctx)
|
||||
css.Format = strings.TrimSpace(format)
|
||||
|
||||
globalGPGSettings = &GPGSettings{
|
||||
Sign: true,
|
||||
}
|
||||
defaultEmail, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.email").RunStdString(ctx)
|
||||
css.Email = strings.TrimSpace(defaultEmail)
|
||||
|
||||
value, _, _ := gitcmd.NewCommand("config", "--global", "--get", "commit.gpgsign").RunStdString(ctx)
|
||||
sign, valid := ParseBool(strings.TrimSpace(value))
|
||||
if !sign || !valid {
|
||||
globalGPGSettings.Sign = false
|
||||
return globalGPGSettings, nil
|
||||
}
|
||||
|
||||
signingKey, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.signingkey").RunStdString(ctx)
|
||||
globalGPGSettings.KeyID = strings.TrimSpace(signingKey)
|
||||
|
||||
format, _, _ := gitcmd.NewCommand("config", "--global", "--default", SigningKeyFormatOpenPGP, "--get", "gpg.format").RunStdString(ctx)
|
||||
globalGPGSettings.Format = strings.TrimSpace(format)
|
||||
|
||||
defaultEmail, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.email").RunStdString(ctx)
|
||||
globalGPGSettings.Email = strings.TrimSpace(defaultEmail)
|
||||
|
||||
defaultName, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.name").RunStdString(ctx)
|
||||
globalGPGSettings.Name = strings.TrimSpace(defaultName)
|
||||
|
||||
if err := globalGPGSettings.LoadPublicKeyContent(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return globalGPGSettings, nil
|
||||
defaultName, _, _ := gitcmd.NewCommand("config", "--global", "--get", "user.name").RunStdString(ctx)
|
||||
css.Name = strings.TrimSpace(defaultName)
|
||||
return css
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user