Skip to content

Commit 2914c52

Browse files
zeripathdelvh
andauthored
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 <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent ccb3851 commit 2914c52

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

models/asymkey/error.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ func (err ErrKeyUnableVerify) Error() string {
2424
return fmt.Sprintf("Unable to verify key content [result: %s]", err.Result)
2525
}
2626

27+
// ErrKeyIsPrivate is returned when the provided key is a private key not a public key
28+
var ErrKeyIsPrivate = util.NewSilentWrapErrorf(util.ErrInvalidArgument, "the provided key is a private key")
29+
2730
// ErrKeyNotExist represents a "KeyNotExist" kind of error.
2831
type ErrKeyNotExist struct {
2932
ID int64

models/asymkey/ssh_key_parse.go

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func parseKeyString(content string) (string, error) {
9696
if block == nil {
9797
return "", fmt.Errorf("failed to parse PEM block containing the public key")
9898
}
99+
if strings.Contains(block.Type, "PRIVATE") {
100+
return "", ErrKeyIsPrivate
101+
}
99102

100103
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
101104
if err != nil {

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ organization_leave_success = You have successfully left the organization %s.
518518
invalid_ssh_key = Cannot verify your SSH key: %s
519519
invalid_gpg_key = Cannot verify your GPG key: %s
520520
invalid_ssh_principal = Invalid principal: %s
521+
must_use_public_key = The key you provided is a private key. Please do not upload your private key anywhere. Use your public key instead.
521522
unable_verify_ssh_key = "Cannot verify the SSH key; double-check it for mistakes."
522523
auth_failed = Authentication failed: %v
523524

routers/web/repo/setting.go

+4
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,10 @@ func DeployKeysPost(ctx *context.Context) {
11581158
ctx.Flash.Info(ctx.Tr("settings.ssh_disabled"))
11591159
} else if asymkey_model.IsErrKeyUnableVerify(err) {
11601160
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
1161+
} else if err == asymkey_model.ErrKeyIsPrivate {
1162+
ctx.Data["HasError"] = true
1163+
ctx.Data["Err_Content"] = true
1164+
ctx.Flash.Error(ctx.Tr("form.must_use_public_key"))
11611165
} else {
11621166
ctx.Data["HasError"] = true
11631167
ctx.Data["Err_Content"] = true

routers/web/user/setting/keys.go

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ func KeysPost(ctx *context.Context) {
159159
ctx.Flash.Info(ctx.Tr("settings.ssh_disabled"))
160160
} else if asymkey_model.IsErrKeyUnableVerify(err) {
161161
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
162+
} else if err == asymkey_model.ErrKeyIsPrivate {
163+
ctx.Flash.Error(ctx.Tr("form.must_use_public_key"))
162164
} else {
163165
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
164166
}

0 commit comments

Comments
 (0)