Skip to content

Commit 8612686

Browse files
committed
Use constant time comparisons for client secrets
This is a precaution to avoid possible timing attacks on client secrets.
1 parent d9cfac1 commit 8612686

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

client.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package osin
22

3+
import "crypto/subtle"
4+
35
// Client information
46
type Client interface {
57
// Client id
@@ -49,7 +51,7 @@ func (d *DefaultClient) GetUserData() interface{} {
4951

5052
// Implement the ClientSecretMatcher interface
5153
func (d *DefaultClient) ClientSecretMatches(secret string) bool {
52-
return d.Secret == secret
54+
return subtle.ConstantTimeCompare([]byte(d.Secret), []byte(secret)) == 1
5355
}
5456

5557
func (d *DefaultClient) CopyFrom(client Client) {

util.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package osin
22

33
import (
4+
"crypto/subtle"
45
"encoding/base64"
56
"errors"
67
"net/http"
@@ -28,7 +29,7 @@ func CheckClientSecret(client Client, secret string) bool {
2829
return client.ClientSecretMatches(secret)
2930
default:
3031
// Fallback to the less secure method of extracting the plain text secret from the client for comparison
31-
return client.GetSecret() == secret
32+
return subtle.ConstantTimeCompare([]byte(client.GetSecret()), []byte(secret)) == 1
3233
}
3334
}
3435

0 commit comments

Comments
 (0)