db: accept a credential only when it starts with its prefix

strings.Cut matched the prefix anywhere, so "x<key>" and "Bearer  <key>" passed.
This commit is contained in:
Kristoffer Dalby committed 2026-11-20 18:56:59 +01:00
1 parent c7e9041cec
commit 27e90f8692
7 files changed
+35 -3

No files matched your search

+4
View File
@@ -43,6 +43,10 @@ secrets are 256 bits of server-generated randomness, never user-chosen, so they
are stored as a SHA-256 digest: recovering one means searching the whole secret
space, and password stretching would only add latency to every request.
A credential is accepted only when it starts with its prefix (`hskey-api-`,
`hskey-auth-`, `hskey-oauthtok-`). Text before the prefix, such as a second
space after `Bearer`, is now refused.
Keys created by older releases keep working on 0.30. Support for them is
removed on this schedule:
+1 -1
View File
@@ -202,7 +202,7 @@ func validateAPIKey(db *gorm.DB, keyStr string) (*types.APIKey, error) {
}
// Check for new format: hskey-api-{prefix}-{secret}
_, prefixAndSecret, found := strings.Cut(keyStr, apiKeyPrefix)
prefixAndSecret, found := strings.CutPrefix(keyStr, apiKeyPrefix)
if !found {
// Legacy format: prefix.secret
+7
View File
@@ -54,6 +54,13 @@ func TestValidateAPIKeyOk(t *testing.T) {
valid, err := db.ValidateAPIKey(apiKeyStr)
require.NoError(t, err)
assert.True(t, valid)
// The prefix must start the key, not merely appear in it.
for _, s := range []string{"x" + apiKeyStr, " " + apiKeyStr} {
valid, err := db.ValidateAPIKey(s)
require.Error(t, err, "%q", s)
assert.False(t, valid, "%q", s)
}
}
func TestValidateAPIKeyNotOk(t *testing.T) {
+1 -1
View File
@@ -263,7 +263,7 @@ func (hsdb *HSDatabase) AuthenticateAccessToken(tokenStr string) (*types.OAuthAc
return nil, ErrAccessTokenFailedToParse
}
_, rest, found := strings.Cut(tokenStr, types.AccessTokenPrefix)
rest, found := strings.CutPrefix(tokenStr, types.AccessTokenPrefix)
if !found {
return nil, ErrAccessTokenFailedToParse
}
+6
View File
@@ -341,6 +341,12 @@ func TestOAuthAccessTokenMintAuthenticateExpire(t *testing.T) {
assert.Equal(t, []string{"auth_keys"}, got.Scopes)
assert.Equal(t, []string{"tag:ci"}, got.Tags)
// The prefix must start the token, not merely appear in it.
for _, s := range []string{"x" + tokenStr, " " + tokenStr} {
_, err = db.AuthenticateAccessToken(s)
require.ErrorIs(t, err, ErrAccessTokenFailedToParse, "%q", s)
}
// An expired token is rejected even though the row still exists.
past := time.Now().Add(-time.Hour)
expiredStr, _, err := db.MintAccessToken(client.ClientID, nil, nil, &past)
+1 -1
View File
@@ -232,7 +232,7 @@ func findAuthKey(tx *gorm.DB, keyStr string) (*types.PreAuthKey, error) {
// TODO(kradalby): remove in 0.32 with legacy key formats (announced).
identifier, secret := legacyAuthKeyIdentifier(keyStr), keyStr
_, prefixAndSecret, found := strings.Cut(keyStr, authKeyPrefix)
prefixAndSecret, found := strings.CutPrefix(keyStr, authKeyPrefix)
if found {
var err error
+15
View File
@@ -236,6 +236,21 @@ func TestPreAuthKeyAuthentication(t *testing.T) {
findErrIs: ErrPreAuthKeyNotFound,
wantValidateErr: false,
},
{
name: "text_before_prefix_rejected",
setupKey: func() string {
keyStr, err := db.CreatePreAuthKey(
user.TypedID(),
true, false, nil, []string{"tag:test"},
)
require.NoError(t, err)
// The prefix must start the key, not merely appear in it.
return "x" + keyStr.Key
},
wantFindErr: true,
findErrIs: ErrPreAuthKeyNotFound,
},
{
name: "new_key_sha256",
setupKey: func() string {