Skip to content

Commit da81736

Browse files
committed
Strip newlines from SSH keys before adding them
Fixes: #7500
1 parent 26b493b commit da81736

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

models/ssh_key.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,18 @@ func extractTypeFromBase64Key(key string) (string, error) {
9696

9797
// parseKeyString parses any key string in OpenSSH or SSH2 format to clean OpenSSH string (RFC4253).
9898
func parseKeyString(content string) (string, error) {
99-
// Transform all legal line endings to a single "\n".
100-
content = strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(content)
101-
// remove trailing newline (and beginning spaces too)
99+
// remove whitespace at start and end
102100
content = strings.TrimSpace(content)
103-
lines := strings.Split(content, "\n")
104101

105102
var keyType, keyContent, keyComment string
106103

107-
if len(lines) == 1 {
104+
if !strings.Contains(content, "-----BEGIN") {
108105
// Parse OpenSSH format.
109-
parts := strings.SplitN(lines[0], " ", 3)
106+
107+
// Remove all newlines
108+
content = strings.NewReplacer("\r\n", "", "\n", "").Replace(content)
109+
110+
parts := strings.SplitN(content, " ", 3)
110111
switch len(parts) {
111112
case 0:
112113
return "", errors.New("empty key")
@@ -133,6 +134,11 @@ func parseKeyString(content string) (string, error) {
133134
}
134135
} else {
135136
// Parse SSH2 file format.
137+
138+
// Transform all legal line endings to a single "\n".
139+
content = strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(content)
140+
141+
lines := strings.Split(content, "\n")
136142
continuationLine := false
137143

138144
for _, line := range lines {

0 commit comments

Comments
 (0)