summaryrefslogtreecommitdiffstats
path: root/models/asymkey
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2023-02-02 18:25:54 +0000
committerGitHub <noreply@github.com>2023-02-02 18:25:54 +0000
commit2914c5299b37c3f98997fc923b0b715c9b3f750a (patch)
tree021346c382be0a3f515009f3a54c9cf57a743007 /models/asymkey
parentccb38512818dd3ee86f7960ed6cdf34754e4d09f (diff)
downloadgitea-2914c5299b37c3f98997fc923b0b715c9b3f750a.tar.gz
gitea-2914c5299b37c3f98997fc923b0b715c9b3f750a.zip
Improve error report when user passes a private key (#22726)
The error reported when a user passes a private ssh key as their ssh public key is not very nice. This PR improves this slightly. Ref #22693 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'models/asymkey')
-rw-r--r--models/asymkey/error.go3
-rw-r--r--models/asymkey/ssh_key_parse.go3
2 files changed, 6 insertions, 0 deletions
diff --git a/models/asymkey/error.go b/models/asymkey/error.go
index 1d486082f4..03bc82302f 100644
--- a/models/asymkey/error.go
+++ b/models/asymkey/error.go
@@ -24,6 +24,9 @@ func (err ErrKeyUnableVerify) Error() string {
return fmt.Sprintf("Unable to verify key content [result: %s]", err.Result)
}
+// ErrKeyIsPrivate is returned when the provided key is a private key not a public key
+var ErrKeyIsPrivate = util.NewSilentWrapErrorf(util.ErrInvalidArgument, "the provided key is a private key")
+
// ErrKeyNotExist represents a "KeyNotExist" kind of error.
type ErrKeyNotExist struct {
ID int64
diff --git a/models/asymkey/ssh_key_parse.go b/models/asymkey/ssh_key_parse.go
index 1df6db6fa7..8693c87e76 100644
--- a/models/asymkey/ssh_key_parse.go
+++ b/models/asymkey/ssh_key_parse.go
@@ -96,6 +96,9 @@ func parseKeyString(content string) (string, error) {
if block == nil {
return "", fmt.Errorf("failed to parse PEM block containing the public key")
}
+ if strings.Contains(block.Type, "PRIVATE") {
+ return "", ErrKeyIsPrivate
+ }
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {