Skip to content

Commit

Permalink
Add check to detect invalid char in key (incuna#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
marteinn committed Dec 15, 2022
1 parent b14dcb6 commit 53d04d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
19 changes: 19 additions & 0 deletions pgcrypto/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,22 @@ def _contains_pgp_public_key_field(models):
if isinstance(field, PGPPublicKeyFieldMixin):
return True
return False


@register()
def check_pgcrypto_key_is_valid(app_configs, **kwargs):
"""Make sure PGCRYPTO_KEY does not contain not supported chars."""
PGCRYPTO_KEY = getattr(settings, "PGCRYPTO_KEY", None)
if not PGCRYPTO_KEY:
return []

if "'" in PGCRYPTO_KEY:
return [
Error(
"Invalid char in PGCRYPTO_KEY setting",
hint="Remove the ' char from key",
id="pgcrypto.E002",
),
]

return []
21 changes: 18 additions & 3 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from django.conf import settings
from django.test import override_settings, TestCase

from pgcrypto.checks import check_required_settings_exist
from pgcrypto.checks import (
check_pgcrypto_key_is_valid,
check_required_settings_exist,
)


class TestChecks(TestCase):
# noqa: D103
class TestChecksRequiredSettings(TestCase):
def test_pgcrypto_key_exist(self):
errors = check_required_settings_exist(None)
self.assertEqual(len(errors), 0)
Expand Down Expand Up @@ -70,3 +72,16 @@ def test_missing_private_pgp_key_raises_error(self):
self.assertEqual(errors[0].id, "pgcrypto.E001")

settings.DATABASES["diff_keys"]["PRIVATE_PGP_KEY"] = key_value


class TestInvalidCharInPgcryptoKey(TestCase):
@override_settings(PGCRYPTO_KEY="random123")
def test_pgcrypto_key_exist(self):
errors = check_pgcrypto_key_is_valid(None)
self.assertEqual(len(errors), 0)

@override_settings(PGCRYPTO_KEY="rando'm123")
def test_pgcrypto_key_exist(self):
errors = check_pgcrypto_key_is_valid(None)
self.assertEqual(len(errors), 1)
self.assertEqual(errors[0].id, "pgcrypto.E002")

0 comments on commit 53d04d6

Please sign in to comment.