Skip to content

Commit 7551313

Browse files
author
Max Azatian
committed
JWT_SECRET_KEY fix 3: key length
1 parent 0e4cce4 commit 7551313

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

backend/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PROJECT_NAME=integr8scode
2-
SECRET_KEY=your_secret_key_here
2+
SECRET_KEY=CHANGE_ME_this_is_a_dev_key_min_32_chars_required
33
ALGORITHM=HS256
44
ACCESS_TOKEN_EXPIRE_MINUTES=30
55
MONGODB_URL=mongodb://mongo:27017/integr8scode

backend/app/config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ class Settings(BaseSettings):
4545

4646
@field_validator("SECRET_KEY")
4747
@classmethod
48-
def validate_secret_key(cls, v: Optional[str]) -> str:
48+
def validate_secret_key(cls, v: Optional[str], info) -> str:
4949
if not v:
5050
raise ValueError("SECRET_KEY environment variable must be set")
5151
if len(v) < 32:
5252
raise ValueError("SECRET_KEY must be at least 32 characters long")
53+
54+
# Check if we're in testing mode
55+
testing = info.data.get('TESTING', False)
56+
57+
# Allow CHANGE_ME prefix only in development/testing
5358
if v == "your_secret_key_here" or v == "default_secret_key":
5459
raise ValueError("SECRET_KEY must not use default placeholder values")
60+
if v.startswith("CHANGE_ME") and not testing:
61+
raise ValueError("SECRET_KEY must not use default placeholder values (CHANGE_ME detected)")
62+
5563
return v
5664

5765
class Config:

backend/tests/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PROJECT_NAME=integr8scode_test
2-
SECRET_KEY=your_test_secret_key_here
2+
SECRET_KEY=thisisatestsecrektkeythatshouldbe32characterslong
33
MONGODB_URL=mongodb://localhost:27017
44
KUBERNETES_CONFIG_PATH=/../kubeconfig.yaml
55
TESTING=true

backend/tests/unit/test_config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def test_secret_key_too_short(self) -> None:
2929
assert "SECRET_KEY must be at least 32 characters long" in errors[0]["msg"]
3030

3131
def test_secret_key_default_placeholder(self) -> None:
32+
# These should always fail
3233
test_cases = ["your_secret_key_here", "default_secret_key"]
3334

3435
for placeholder in test_cases:
@@ -40,6 +41,24 @@ def test_secret_key_default_placeholder(self) -> None:
4041
assert len(errors) == 1
4142
assert errors[0]["loc"] == ("SECRET_KEY",)
4243
assert "SECRET_KEY must not use default placeholder values" in errors[0]["msg"]
44+
45+
def test_secret_key_change_me_without_testing(self) -> None:
46+
# CHANGE_ME should fail when not in testing mode
47+
with mock.patch.dict(os.environ, {"SECRET_KEY": "CHANGE_ME_this_is_a_dev_key_min_32_chars_required", "TESTING": "false"}, clear=True):
48+
with pytest.raises(ValidationError) as exc_info:
49+
Settings()
50+
51+
errors = exc_info.value.errors()
52+
assert len(errors) == 1
53+
assert errors[0]["loc"] == ("SECRET_KEY",)
54+
assert "CHANGE_ME detected" in errors[0]["msg"]
55+
56+
def test_secret_key_change_me_with_testing(self) -> None:
57+
# CHANGE_ME should be allowed in testing mode
58+
with mock.patch.dict(os.environ, {"SECRET_KEY": "CHANGE_ME_this_is_a_dev_key_min_32_chars_required", "TESTING": "true"}, clear=True):
59+
settings = Settings()
60+
assert settings.SECRET_KEY == "CHANGE_ME_this_is_a_dev_key_min_32_chars_required"
61+
assert settings.TESTING is True
4362

4463
def test_secret_key_valid(self) -> None:
4564
valid_key = "a" * 32 # 32 character key

0 commit comments

Comments
 (0)