Skip to content

Commit

Permalink
Retry SSH key verification with additional CRLF if it failed (go-gite…
Browse files Browse the repository at this point in the history
…a#28392)

Windows-based shells will add a CRLF when piping the token into
ssh-keygen command resulting in
verification error. This resolves go-gitea#21527.

---------

Co-authored-by: Heiko Besemann <[email protected]>
Co-authored-by: wxiaoguang <[email protected]>
  • Loading branch information
3 people authored and silverwind committed Feb 20, 2024
1 parent 6cdf96a commit d81cc15
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions models/asymkey/ssh_key_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ func VerifySSHKey(ctx context.Context, ownerID int64, fingerprint, token, signat
return "", ErrKeyNotExist{}
}

if err := sshsig.Verify(bytes.NewBuffer([]byte(token)), []byte(signature), []byte(key.Content), "gitea"); err != nil {
log.Error("Unable to validate token signature. Error: %v", err)
return "", ErrSSHInvalidTokenSignature{
Fingerprint: key.Fingerprint,
err = sshsig.Verify(bytes.NewBuffer([]byte(token)), []byte(signature), []byte(key.Content), "gitea")
if err != nil {
// edge case for Windows based shells that will add CR LF if piped to ssh-keygen command
// see https://github.com/PowerShell/PowerShell/issues/5974
if sshsig.Verify(bytes.NewBuffer([]byte(token+"\r\n")), []byte(signature), []byte(key.Content), "gitea") != nil {
log.Error("Unable to validate token signature. Error: %v", err)
return "", ErrSSHInvalidTokenSignature{
Fingerprint: key.Fingerprint,
}
}
}

Expand Down

0 comments on commit d81cc15

Please sign in to comment.