Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aws_lambda_powertools/utilities/data_masking/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ def _apply_masking_rules(self, data: dict, masking_rules: dict) -> dict:
Returns:
dict: The masked data dictionary
"""
if isinstance(data, str):
# Same as `fields`: JSON strings are parsed so path expressions can be resolved
data = self.json_deserializer(data)

result = deepcopy(data)

for path, rule in masking_rules.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ def test_mask_nested_field_success(data_masker):
assert data_masked == {"user": {"contact": {"details": {"address": {"street": "123 Main St", "zip": "xxx"}}}}}


def test_erase_json_string_with_masking_rules(data_masker):
# GIVEN the data type is a JSON string
data = json.dumps({"email": "johndoe@example.com", "address": {"zip": 13000, "street": "123 Main St"}})

# WHEN erase is called with masking rules
masking_rules = {
"email": {"regex_pattern": "(.)(.*)(@.*)", "mask_format": r"\1****\3"},
"address.zip": {"custom_mask": "xxx"},
}
masked = data_masker.erase(data=data, masking_rules=masking_rules)

# THEN the JSON string is parsed and the fields are masked, as with `fields`
assert masked == {"email": "j****@example.com", "address": {"zip": "xxx", "street": "123 Main St"}}


def test_erase_dictionary_with_masking_rules(data_masker):
# GIVEN a dictionary with nested sensitive data
data = {"user": {"name": "John Doe", "ssn": "123-45-6789", "address": {"street": "123 Main St", "zip": "12345"}}}
Expand Down