From 27e90f8692caa409fe1457ee7c88346b0b8f77b5 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 19 Nov 2026 19:33:03 +0000 Subject: [PATCH] db: accept a credential only when it starts with its prefix strings.Cut matched the prefix anywhere, so "x" and "Bearer " passed. --- CHANGELOG.md | 4 ++++ hscontrol/db/api_key.go | 2 +- hscontrol/db/api_key_test.go | 7 +++++++ hscontrol/db/oauth.go | 2 +- hscontrol/db/oauth_test.go | 6 ++++++ hscontrol/db/preauth_keys.go | 2 +- hscontrol/db/preauth_keys_test.go | 15 +++++++++++++++ 7 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1cc55d91..6942e2e6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/hscontrol/db/api_key.go b/hscontrol/db/api_key.go index 519b5a2f7..dc780fe86 100644 --- a/hscontrol/db/api_key.go +++ b/hscontrol/db/api_key.go @@ -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 diff --git a/hscontrol/db/api_key_test.go b/hscontrol/db/api_key_test.go index 8094a8122..d73fed7b6 100644 --- a/hscontrol/db/api_key_test.go +++ b/hscontrol/db/api_key_test.go @@ -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) { diff --git a/hscontrol/db/oauth.go b/hscontrol/db/oauth.go index 527760896..15eec1677 100644 --- a/hscontrol/db/oauth.go +++ b/hscontrol/db/oauth.go @@ -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 } diff --git a/hscontrol/db/oauth_test.go b/hscontrol/db/oauth_test.go index 89062ae58..7c0ec85f3 100644 --- a/hscontrol/db/oauth_test.go +++ b/hscontrol/db/oauth_test.go @@ -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) diff --git a/hscontrol/db/preauth_keys.go b/hscontrol/db/preauth_keys.go index 37647bb4c..c983112f4 100644 --- a/hscontrol/db/preauth_keys.go +++ b/hscontrol/db/preauth_keys.go @@ -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 diff --git a/hscontrol/db/preauth_keys_test.go b/hscontrol/db/preauth_keys_test.go index dd89444ba..3aa51f70f 100644 --- a/hscontrol/db/preauth_keys_test.go +++ b/hscontrol/db/preauth_keys_test.go @@ -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 {