Skip to content

Commit

Permalink
Use constant time comparisons for client secrets
Browse files Browse the repository at this point in the history
This is a precaution to avoid possible timing attacks on
client secrets.
  • Loading branch information
stlaz committed Jan 13, 2021
1 parent d9cfac1 commit 8612686
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package osin

import "crypto/subtle"

// Client information
type Client interface {
// Client id
Expand Down Expand Up @@ -49,7 +51,7 @@ func (d *DefaultClient) GetUserData() interface{} {

// Implement the ClientSecretMatcher interface
func (d *DefaultClient) ClientSecretMatches(secret string) bool {
return d.Secret == secret
return subtle.ConstantTimeCompare([]byte(d.Secret), []byte(secret)) == 1
}

func (d *DefaultClient) CopyFrom(client Client) {
Expand Down
3 changes: 2 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package osin

import (
"crypto/subtle"
"encoding/base64"
"errors"
"net/http"
Expand Down Expand Up @@ -28,7 +29,7 @@ func CheckClientSecret(client Client, secret string) bool {
return client.ClientSecretMatches(secret)
default:
// Fallback to the less secure method of extracting the plain text secret from the client for comparison
return client.GetSecret() == secret
return subtle.ConstantTimeCompare([]byte(client.GetSecret()), []byte(secret)) == 1
}
}

Expand Down

0 comments on commit 8612686

Please sign in to comment.