diff --git a/models/ssh_key.go b/models/ssh_key.go index d1132bf0c61a..edc6d45cd867 100644 --- a/models/ssh_key.go +++ b/models/ssh_key.go @@ -107,7 +107,7 @@ func parseKeyString(content string) (string, error) { var keyType, keyContent, keyComment string - if content[:len(ssh2keyStart)] == ssh2keyStart { + if strings.HasPrefix(content, ssh2keyStart) { // Parse SSH2 file format. // Transform all legal line endings to a single "\n". diff --git a/models/ssh_key_test.go b/models/ssh_key_test.go index 4bb612a67176..95cd4eeb1a2f 100644 --- a/models/ssh_key_test.go +++ b/models/ssh_key_test.go @@ -131,6 +131,19 @@ AAAAC3NzaC1lZDI1NTE5AAAAICV0MGX/W9IvLA4FXpIuUcdDcbj5KX4syHgsTy7soVgf _, err := CheckPublicKeyString(test.content) assert.NoError(t, err) } + + for _, invalidKeys := range []struct { + content string + }{ + {"test"}, + {"---- NOT A REAL KEY ----"}, + {"bad\nkey"}, + {"\t\t:)\t\r\n"}, + {"\r\ntest \r\ngitea\r\n\r\n"}, + } { + _, err := CheckPublicKeyString(invalidKeys.content) + assert.Error(t, err) + } } func Test_calcFingerprint(t *testing.T) {