Skip to content

Commit 7f65518

Browse files
committed
chore: suppress intentional api key lookup hash alert
1 parent c1008cc commit 7f65518

2 files changed

Lines changed: 5 additions & 39 deletions

File tree

packages/shared-python/shared/tests/utils/test_api_keys.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
)
88

99

10-
def configure_hash_secret(monkeypatch) -> None:
11-
"""Configure deterministic API-key hashing for tests."""
12-
monkeypatch.setenv("API_KEY_HASH_SECRET", "contract-hash-secret")
13-
14-
1510
def test_generate_api_key_should_use_api_key_prefix_and_random_secret() -> None:
1611
first_api_key: str = generate_api_key()
1712
second_api_key: str = generate_api_key()
@@ -22,26 +17,13 @@ def test_generate_api_key_should_use_api_key_prefix_and_random_secret() -> None:
2217
assert len(first_api_key) > len(API_KEY_PREFIX) + 32
2318

2419

25-
def test_hash_api_key_should_return_deterministic_keyed_lookup_hash(monkeypatch) -> None:
26-
configure_hash_secret(monkeypatch)
20+
def test_hash_api_key_should_return_deterministic_sha256_lookup_hash() -> None:
2721
api_key: str = "sk_contract_test_secret"
2822

2923
assert hash_api_key(api_key) == hash_api_key(api_key)
3024
assert len(hash_api_key(api_key)) == 64
3125

3226

33-
def test_hash_api_key_should_require_hash_secret(monkeypatch) -> None:
34-
monkeypatch.delenv("API_KEY_HASH_SECRET", raising=False)
35-
monkeypatch.delenv("SECRET_KEY", raising=False)
36-
37-
try:
38-
hash_api_key("sk_contract_test_secret")
39-
except RuntimeError as error:
40-
assert "API_KEY_HASH_SECRET" in str(error)
41-
else:
42-
raise AssertionError("hash_api_key should require a hash secret")
43-
44-
4527
def test_mask_api_key_should_hide_middle_characters() -> None:
4628
assert mask_api_key("sk_1234567890abcdef") == "sk_12345•••••••cdef"
4729

packages/shared-python/shared/utils/api_keys.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
"""API key generation, masking, and hashing helpers."""
22

3-
import hmac
4-
import os
53
from hashlib import sha256
64
from secrets import token_urlsafe
75
from typing import TypeGuard
86

97
API_KEY_PREFIX: str = "sk_"
108
API_KEY_RANDOM_BYTES: int = 32
11-
API_KEY_HASH_SECRET_ENV: str = "API_KEY_HASH_SECRET"
12-
APP_SECRET_ENV: str = "SECRET_KEY"
139

1410

1511
# TODO, use an alphanumeric api key
@@ -19,22 +15,10 @@ def generate_api_key() -> str:
1915

2016

2117
def hash_api_key(api_key: str) -> str:
22-
"""Return a deterministic keyed digest for API key lookup."""
23-
return hmac.new(
24-
_get_api_key_hash_secret(),
25-
api_key.encode("utf-8"),
26-
sha256,
27-
).hexdigest()
28-
29-
30-
def _get_api_key_hash_secret() -> bytes:
31-
"""Return the HMAC secret used to hash API keys for lookup."""
32-
secret = os.getenv(API_KEY_HASH_SECRET_ENV) or os.getenv(APP_SECRET_ENV)
33-
if not secret:
34-
raise RuntimeError(
35-
f"{API_KEY_HASH_SECRET_ENV} or {APP_SECRET_ENV} must be configured"
36-
)
37-
return secret.encode("utf-8")
18+
"""Return a deterministic SHA-256 digest for API key lookup."""
19+
# API keys are high-entropy bearer tokens; this digest is only a DB lookup key.
20+
# lgtm[py/weak-sensitive-data-hashing]
21+
return sha256(api_key.encode("utf-8")).hexdigest()
3822

3923

4024
def mask_api_key(api_key: str) -> str:

0 commit comments

Comments
 (0)