Skip to content

Commit

Permalink
knownhosts: fix hashed hostname component count in error message
Browse files Browse the repository at this point in the history
Correct the component splitting in the nextWord function to omit the
initial empty element when decoding the pipe-separated hostname hash.

Previously, the error message incorrectly counted this empty element,
leading to misleading errors like:

  knownhosts: got 3 components, want 3

This change makes the component split start from index 1.

The existing tests cover the changed code.

Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Apr 12, 2024
1 parent d042a39 commit 59f185c
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions ssh/knownhosts/knownhosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,17 +481,17 @@ func decodeHash(encoded string) (hashType string, salt, hash []byte, err error)
err = errors.New("knownhosts: hashed host must start with '|'")
return
}
components := strings.Split(encoded, "|")
if len(components) != 4 {
components := strings.Split(encoded[1:], "|")
if len(components) != 3 {
err = fmt.Errorf("knownhosts: got %d components, want 3", len(components))
return
}

hashType = components[1]
if salt, err = base64.StdEncoding.DecodeString(components[2]); err != nil {
hashType = components[0]
if salt, err = base64.StdEncoding.DecodeString(components[1]); err != nil {
return
}
if hash, err = base64.StdEncoding.DecodeString(components[3]); err != nil {
if hash, err = base64.StdEncoding.DecodeString(components[2]); err != nil {
return
}
return
Expand Down

0 comments on commit 59f185c

Please sign in to comment.