mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-28 00:17:00 +00:00
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
This commit is contained in:
parent
bcbc9f33d7
commit
b10109ffe8
@ -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 {
|
||||
|
@ -56,6 +56,22 @@ func Test_SSHParsePublicKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_CheckPublicKeyString(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
content string
|
||||
}{
|
||||
{"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},
|
||||
{"ssh-rsa AAAAB3NzaC1yc2EA\r\nAAADAQABAAAAgQDAu7tvI\nvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+\r\nBZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvW\nqIwC4prx/WVk2wLTJjzBAhyNx\r\nfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\r\n\r\n"},
|
||||
{"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICV0MGX/W9IvLA4FXpIuUcdDcbj5KX4syHgsTy7soVgf"},
|
||||
{"\r\nssh-ed25519 \r\nAAAAC3NzaC1lZDI1NTE5AAAAICV0MGX/W9IvLA4FXpIuUcdDcbj5KX4syHgsTy7soVgf\r\n\r\n"},
|
||||
} {
|
||||
_, err := CheckPublicKeyString(test.content)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_calcFingerprint(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
Loading…
Reference in New Issue
Block a user