Skip to content

Commit c5e5063

Browse files
6543lunny
authored andcommitted
Fix SSH2 conditonal in key parsing code (#8806) (#8810)
Avoid out of bounds error by using strings.HasPrefix to check for starting SSH2 text rather than assuming user input has at least 31 characters. Add tests for bad input as well. Fixes #8800
1 parent b040a87 commit c5e5063

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

models/ssh_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func parseKeyString(content string) (string, error) {
107107

108108
var keyType, keyContent, keyComment string
109109

110-
if content[:len(ssh2keyStart)] == ssh2keyStart {
110+
if strings.HasPrefix(content, ssh2keyStart) {
111111
// Parse SSH2 file format.
112112

113113
// Transform all legal line endings to a single "\n".

models/ssh_key_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ AAAAC3NzaC1lZDI1NTE5AAAAICV0MGX/W9IvLA4FXpIuUcdDcbj5KX4syHgsTy7soVgf
131131
_, err := CheckPublicKeyString(test.content)
132132
assert.NoError(t, err)
133133
}
134+
135+
for _, invalidKeys := range []struct {
136+
content string
137+
}{
138+
{"test"},
139+
{"---- NOT A REAL KEY ----"},
140+
{"bad\nkey"},
141+
{"\t\t:)\t\r\n"},
142+
{"\r\ntest \r\ngitea\r\n\r\n"},
143+
} {
144+
_, err := CheckPublicKeyString(invalidKeys.content)
145+
assert.Error(t, err)
146+
}
134147
}
135148

136149
func Test_calcFingerprint(t *testing.T) {

0 commit comments

Comments
 (0)