Skip to content

Commit

Permalink
Support regexes in model whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
fbinz committed May 30, 2024
1 parent 95f8540 commit 777f65e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ and booleans (usually) can't contain sensitive personal data. These fields will

Whitelists a list of models which will not be checked during `scrub_validation` and when
activating the strict mode. Defaults to the non-privacy-related Django base models.
Items can either be full model names (e.g. `auth.Group`) or regular expression patterns matching
against the full model name (e.g. `re.compile(auth.*)` to whitelist all auth models).

(default: ['auth.Group', 'auth.Permission', 'contenttypes.ContentType', 'sessions.Session', 'sites.Site',
'django_scrubber.FakeData',))
Expand Down
16 changes: 15 additions & 1 deletion django_scrubber/services/validator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from django.apps import apps

from django_scrubber import settings_with_fallback
Expand All @@ -8,6 +10,15 @@ class ScrubberValidatorService:
Service to validate if all text-based fields are being scrubbed within your project and dependencies.
"""

@staticmethod
def check_pattern(pattern: str | re.Pattern, value):
if isinstance(pattern, str):
return pattern == value
elif isinstance(pattern, re.Pattern):
return pattern.fullmatch(value)
else:
raise ValueError("Invalid pattern type")

def process(self) -> dict:
from django_scrubber.management.commands.scrub_data import _get_model_scrubbers

Expand All @@ -24,7 +35,10 @@ def process(self) -> dict:
for model in model_list:

# Check if model is whitelisted
if model._meta.label in model_whitelist:
if any(
self.check_pattern(pattern, model._meta.label)
for pattern in model_whitelist
):
continue

text_based_fields = []
Expand Down
13 changes: 13 additions & 0 deletions tests/services/test_validator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

try:
from unittest import mock
except ImportError:
Expand Down Expand Up @@ -62,3 +64,14 @@ def test_process_scrubber_required_field_type_variable_used(self):
result = service.process()

self.assertEqual(len(result), 0)

@override_settings(
SCRUBBER_REQUIRED_FIELD_MODEL_WHITELIST=[re.compile("auth.*")],
)
def test_process_scrubber_required_field_model_whitelist_regex(self):
service = ScrubberValidatorService()
result = service.process()

model_list = tuple(result.keys())
self.assertNotIn('auth.User', model_list)
self.assertNotIn('auth.Permission', model_list)

0 comments on commit 777f65e

Please sign in to comment.