Merge cd6e8b2e299405e620057caf007ee5846f1c30e5 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646

This commit is contained in:
Gautam Kumar 2026-11-11 10:03:27 +05:30 committed by GitHub
commit 333bf98cda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 95 additions and 13 deletions

25
auth.go
View File

@ -5,6 +5,7 @@
package gin
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"net/http"
@ -23,8 +24,9 @@ const AuthProxyUserKey = "proxy_user"
type Accounts map[string]string
type authPair struct {
value string
user string
value string
user string
digest [sha256.Size]byte
}
type authPairs []authPair
@ -33,12 +35,20 @@ func (a authPairs) searchCredential(authValue string) (string, bool) {
if authValue == "" {
return "", false
}
authDigest := sha256.Sum256(bytesconv.StringToBytes(authValue))
var matchedUser string
var found bool
for _, pair := range a {
if subtle.ConstantTimeCompare(bytesconv.StringToBytes(pair.value), bytesconv.StringToBytes(authValue)) == 1 {
return pair.user, true
// Compare fixed-size SHA-256 digests so subtle.ConstantTimeCompare
// does not return early on length mismatch and leak credential length.
if subtle.ConstantTimeCompare(pair.digest[:], authDigest[:]) == 1 &&
subtle.ConstantTimeCompare(bytesconv.StringToBytes(pair.value), bytesconv.StringToBytes(authValue)) == 1 &&
!found {
matchedUser = pair.user
found = true
}
}
return "", false
return matchedUser, found
}
// BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where
@ -81,8 +91,9 @@ func processAccounts(accounts Accounts) authPairs {
assert1(user != "", "User can not be empty")
value := authorizationHeader(user, password)
pairs = append(pairs, authPair{
value: value,
user: user,
value: value,
user: user,
digest: sha256.Sum256(bytesconv.StringToBytes(value)),
})
}
return pairs

View File

@ -5,6 +5,7 @@
package gin
import (
"crypto/sha256"
"encoding/base64"
"net/http"
"net/http/httptest"
@ -22,16 +23,19 @@ func TestBasicAuth(t *testing.T) {
assert.Len(t, pairs, 3)
assert.Contains(t, pairs, authPair{
user: "bar",
value: "Basic YmFyOmZvbw==",
user: "bar",
value: "Basic YmFyOmZvbw==",
digest: sha256.Sum256([]byte("Basic YmFyOmZvbw==")),
})
assert.Contains(t, pairs, authPair{
user: "foo",
value: "Basic Zm9vOmJhcg==",
user: "foo",
value: "Basic Zm9vOmJhcg==",
digest: sha256.Sum256([]byte("Basic Zm9vOmJhcg==")),
})
assert.Contains(t, pairs, authPair{
user: "admin",
value: "Basic YWRtaW46cGFzc3dvcmQ=",
user: "admin",
value: "Basic YWRtaW46cGFzc3dvcmQ=",
digest: sha256.Sum256([]byte("Basic YWRtaW46cGFzc3dvcmQ=")),
})
}
@ -77,6 +81,73 @@ func TestBasicAuthSearchCredential(t *testing.T) {
assert.False(t, found)
}
func TestBasicAuthSearchCredentialLengthAndCollisionGuard(t *testing.T) {
pairs := processAccounts(Accounts{
"a": "",
"admin": "password",
"longuser": "very-long-secret-password-value",
})
for _, tc := range []struct {
name string
header string
wantUser string
wantFound bool
}{
{
name: "empty password account matches",
header: authorizationHeader("a", ""),
wantUser: "a",
wantFound: true,
},
{
name: "long account matches",
header: authorizationHeader("longuser", "very-long-secret-password-value"),
wantUser: "longuser",
wantFound: true,
},
{
name: "shorter than any credential",
header: "Basic YQ==",
},
{
name: "prefix of valid credential",
header: authorizationHeader("admin", "password")[:10],
},
{
name: "longer than any credential",
header: authorizationHeader("longuser", "very-long-secret-password-value-extra"),
},
{
name: "same length wrong password",
header: authorizationHeader("admin", "passworx"),
},
{
name: "non-basic scheme",
header: "Bearer some-token",
},
} {
t.Run(tc.name, func(t *testing.T) {
user, found := pairs.searchCredential(tc.header)
assert.Equal(t, tc.wantUser, user)
assert.Equal(t, tc.wantFound, found)
})
}
// Verify exact value check prevents false positives even on matching digests.
candidate := authorizationHeader("admin", "wrong")
forged := authPairs{
{
user: "admin",
value: authorizationHeader("admin", "password"),
digest: sha256.Sum256([]byte(candidate)),
},
}
user, found := forged.searchCredential(candidate)
assert.Empty(t, user)
assert.False(t, found)
}
func TestBasicAuthAuthorizationHeader(t *testing.T) {
assert.Equal(t, "Basic YWRtaW46cGFzc3dvcmQ=", authorizationHeader("admin", "password"))
}