diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2024-08-08 11:43:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-08 09:43:04 +0000 |
commit | 3862b31abb0fcc094e9d042a7093b8bb3af9f885 (patch) | |
tree | cf40142db98ace7b69c4efea6c16cdf143239355 /services/packages/rpm/sign.go | |
parent | de2787a4939ffdc11184a2115289cfb097272ba2 (diff) | |
download | gitea-3862b31abb0fcc094e9d042a7093b8bb3af9f885.tar.gz gitea-3862b31abb0fcc094e9d042a7093b8bb3af9f885.zip |
Fix RPM resource leak (#31794)
Fixes a resource leak introduced by #27069.
- add defer
- move sign code out of `repository.go`
Diffstat (limited to 'services/packages/rpm/sign.go')
-rw-r--r-- | services/packages/rpm/sign.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/services/packages/rpm/sign.go b/services/packages/rpm/sign.go new file mode 100644 index 0000000000..820355a638 --- /dev/null +++ b/services/packages/rpm/sign.go @@ -0,0 +1,39 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package rpm + +import ( + "bytes" + "io" + "strings" + + packages_module "code.gitea.io/gitea/modules/packages" + + "github.com/ProtonMail/go-crypto/openpgp" + "github.com/sassoftware/go-rpmutils" +) + +func SignPackage(buf *packages_module.HashedBuffer, privateKey string) (*packages_module.HashedBuffer, error) { + keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(privateKey)) + if err != nil { + return nil, err + } + + h, err := rpmutils.SignRpmStream(buf, keyring[0].PrivateKey, nil) + if err != nil { + return nil, err + } + + signBlob, err := h.DumpSignatureHeader(false) + if err != nil { + return nil, err + } + + if _, err := buf.Seek(int64(h.OriginalSignatureHeaderSize()), io.SeekStart); err != nil { + return nil, err + } + + // create new buf with signature prefix + return packages_module.CreateHashedBufferFromReader(io.MultiReader(bytes.NewReader(signBlob), buf)) +} |