Repository navigation
fix(data-masking): prevent erase from returning unmasked values - #5975
Merged
Merged
Conversation
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
force-pushed
the
fix/data-masking-erase-fail-open
branch
from
November 16, 2026 09:59
6b3b595 to
d76bd88
Compare
sdangol
approved these changes
Nov 16, 2026
svozza
reviewed
Nov 16, 2026
dreamorosi
enabled auto-merge (squash)
November 16, 2026 15:36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
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 thegflag or using they(sticky) flag, with rules applied to arrays or objects, with emptyfieldsormaskingRules, and with keys named__proto__,constructororprototype.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.*****(as in Python). The replacement always runs on a fresh copy of the regex with thegflag, so every match is replaced and nolastIndexstate is kept between calls. Sticky (y) regexes are rejected.maskLeafnow 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 bothmaskingRulesandfieldsused with a top-level rule. A plainfieldserase without a rule still replaces the whole value.fields: []throws inerase,encryptanddecrypt, andmaskingRules: {}throws inerase. Both throw the newDataMaskingConfigurationError, which is exported from the package. Behaviour withfields: undefineddoesn't change.__proto__,constructorandprototypeare masked when the data has them as own properties, and they're written back withObject.defineProperty. Tests check thatObject.prototypeand the copy's prototype are left untouched.JSON.parsefailure now throwsDataMaskingEncryptionErrorwith a generic message and nocause, so the plaintext can't reach the logs.types.tsandDataMasking.ts, and the docs indocs/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 thegflag. A rule on an array or object now keeps the structure instead of turning it into a string. EmptyfieldsandmaskingRulesnow 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.