summaryrefslogtreecommitdiffstats
path: root/models/ssh_key.go
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2019-07-23 15:25:06 +0200
committerLauris BH <lauris@nix.lv>2019-07-23 16:25:06 +0300
commitb10109ffe895d05c169b6a2143ec9e7967e68f75 (patch)
tree26c1fb1274415729b06a4b29957af5a3e241a13f /models/ssh_key.go
parentbcbc9f33d73393c47b27793ac91b8f9faf98d349 (diff)
downloadgitea-b10109ffe895d05c169b6a2143ec9e7967e68f75.tar.gz
gitea-b10109ffe895d05c169b6a2143ec9e7967e68f75.zip
Improve SSH key parser to handle newlines in keys (#7522)
* Strip newlines from SSH keys before adding them Fixes: https://github.com/go-gitea/gitea/issues/7500 * add test for CheckPublicKeyString * add one more test * simplify test * further simplify * make fmt
Diffstat (limited to 'models/ssh_key.go')
-rw-r--r--models/ssh_key.go18
1 files changed, 12 insertions, 6 deletions
diff --git a/models/ssh_key.go b/models/ssh_key.go
index ceb4d97560..cbd68a307e 100644
--- a/models/ssh_key.go
+++ b/models/ssh_key.go
@@ -96,17 +96,18 @@ func extractTypeFromBase64Key(key string) (string, error) {
// parseKeyString parses any key string in OpenSSH or SSH2 format to clean OpenSSH string (RFC4253).
func parseKeyString(content string) (string, error) {
- // Transform all legal line endings to a single "\n".
- content = strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(content)
- // remove trailing newline (and beginning spaces too)
+ // remove whitespace at start and end
content = strings.TrimSpace(content)
- lines := strings.Split(content, "\n")
var keyType, keyContent, keyComment string
- if len(lines) == 1 {
+ if !strings.Contains(content, "-----BEGIN") {
// Parse OpenSSH format.
- parts := strings.SplitN(lines[0], " ", 3)
+
+ // Remove all newlines
+ content = strings.NewReplacer("\r\n", "", "\n", "").Replace(content)
+
+ parts := strings.SplitN(content, " ", 3)
switch len(parts) {
case 0:
return "", errors.New("empty key")
@@ -133,6 +134,11 @@ func parseKeyString(content string) (string, error) {
}
} else {
// Parse SSH2 file format.
+
+ // Transform all legal line endings to a single "\n".
+ content = strings.NewReplacer("\r\n", "\n", "\r", "\n").Replace(content)
+
+ lines := strings.Split(content, "\n")
continuationLine := false
for _, line := range lines {