Skip to content

fix(data-masking): prevent erase from returning unmasked values - #5975

Merged
dreamorosi merged 2 commits into
mainfrom
fix/data-masking-erase-fail-open
Nov 16, 2026
Merged

dreamorosi merged 2 commits into
mainfrom
fix/data-masking-erase-fail-open

Conversation

@dreamorosi

Copy link
Copy Markdown
Contributor

Summary

Changes

Please provide a summary of what's being changed

erase() could return values it was asked to mask without throwing or logging anything. This happened when a regex didn't match, with regexes lacking the g flag or using the y (sticky) flag, with rules applied to arrays or objects, with empty fields or maskingRules, and with keys named __proto__, constructor or prototype. decrypt() could also put decrypted plaintext into error messages. This PR makes these cases mask the value or throw, matching how the Python utility fails closed.

  • Regex rules: the pattern is tested on a fresh copy first. If nothing matches, the value is replaced with ***** (as in Python). The replacement always runs on a fresh copy of the regex with the g flag, so every match is replaced and no lastIndex state is kept between calls. Sticky (y) regexes are rejected.
  • Rules on containers: maskLeaf now goes into arrays and plain objects and applies the rule to each leaf. Structure and types are kept, and no element is left unmasked. This covers both maskingRules and fields used with a top-level rule. A plain fields erase without a rule still replaces the whole value.
  • Empty options: fields: [] throws in erase, encrypt and decrypt, and maskingRules: {} throws in erase. Both throw the new DataMaskingConfigurationError, which is exported from the package. Behaviour with fields: undefined doesn't change.
  • Reserved keys: paths now only reach own properties, never the prototype chain. __proto__, constructor and prototype are masked when the data has them as own properties, and they're written back with Object.defineProperty. Tests check that Object.prototype and the copy's prototype are left untouched.
  • Decrypt errors: a JSON.parse failure now throws DataMaskingEncryptionError with a generic message and no cause, so the plaintext can't reach the logs.
  • Updated the TSDoc in types.ts and DataMasking.ts, and the docs in docs/features/data-masking.md. Unit tests cover each case, and coverage stays at 100%.

Behaviour changes: a regex rule that matches nothing now returns *****. Every occurrence is now replaced, even without the g flag. A rule on an array or object now keeps the structure instead of turning it into a string. Empty fields and maskingRules now throw. We're shipping these as bug fixes because the old behaviour could leak data without any warning.

Issue number: closes #5972


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi self-assigned this Nov 16, 2026
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Nov 16, 2026
@dreamorosi
dreamorosi requested a review from svozza November 16, 2026 09:57
erase could hand back data it was asked to mask, with no error or warning:
a regex rule that did not match returned the original value, a pattern
without the `g` flag masked only the first occurrence, a sticky pattern
alternated between calls, a rule on an array or object stringified the
container and left the elements that did not match in plaintext, an empty
`fields` list or `maskingRules` object masked nothing, and keys named
`__proto__`, `constructor` or `prototype` were skipped silently.

Patterns are now applied through a fresh global copy, fall back to the
default mask when they match nothing, and sticky patterns are rejected.
Rules recurse into arrays and plain objects so every leaf is masked and
the structure survives. Empty `fields` and `maskingRules` throw the new
DataMaskingConfigurationError, in encrypt and decrypt too. Reserved keys
resolve when the data carries them as own properties and are written back
with Object.defineProperty, so they are masked without reaching the
prototype chain.

decrypt no longer passes on the SyntaxError from JSON.parse, whose
message quotes the decrypted plaintext, and throws a
DataMaskingEncryptionError without it instead.

Closes #5972
@dreamorosi
dreamorosi force-pushed the fix/data-masking-erase-fail-open branch from 6b3b595 to d76bd88 Compare November 16, 2026 09:59
@dreamorosi
dreamorosi requested a review from sdangol November 16, 2026 12:06
@dreamorosi
dreamorosi enabled auto-merge (squash) November 16, 2026 15:36
@dreamorosi
dreamorosi merged commit 06178e3 into main Nov 16, 2026
43 checks passed
@dreamorosi
dreamorosi deleted the fix/data-masking-erase-fail-open branch November 16, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL PRs between 500-999 LOC, often PRs that grown with feedback

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Data Masking erase() can return sensitive values in plaintext without an error

3 participants