Skip to content

Bug: DataMasking.erase with masking_rules ignores JSON string input #8636

Description

@LemonCondor

Expected Behaviour

DataMasking.erase() accepts a JSON string with fields=. With masking_rules=, the same JSON string input should be parsed too, and the matching fields masked.

Current Behaviour

With masking_rules=, the rule paths are evaluated against the raw string, so nothing ever matches:

  • by default it raises DataMaskingFieldNotFoundError: Field or expression email not found;
  • with raise_on_missing_field=False it only logs a warning and returns the string unmasked.

Code snippet

import json
from aws_lambda_powertools.utilities.data_masking import DataMasking

dm = DataMasking()
rules = {"email": {"regex_pattern": "(.)(.*)(@.*)", "mask_format": r"\1****\3"}}
data = {"email": "john@example.com"}

dm.erase(data, masking_rules=rules)              # {'email': 'j****@example.com'}
dm.erase(json.dumps(data), fields=["email"])     # works, JSON string is parsed
dm.erase(json.dumps(data), masking_rules=rules)  # DataMaskingFieldNotFoundError

Possible Solution

_apply_masking_rules (aws_lambda_powertools/utilities/data_masking/base.py) could deserialize str input with the configured json_deserializer before evaluating the paths, as the fields code path already does in _normalize_data_to_parse.

Steps to Reproduce

Run the snippet above.

Powertools for AWS Lambda (Python) version

latest (3.36.0, also on develop)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

aws_lambda_powertools.utilities.data_masking.exceptions.DataMaskingFieldNotFoundError: Field or expression email not found

Activity

  1. powertools-for-aws-oss-automation commented on Nov 9, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  2. github-actions commented on Nov 10, 2026

    @github-actions
    Contributor

    This is now released under 3.40.0 version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions