Add automatic credit card masking - #8
Open
alkinbg wants to merge 1 commit into
Open
Conversation
alkinbg
force-pushed
the
issue-2-hide-credit-cards
branch
from
August 13, 2026 13:43
6a631f2 to
b6c70c3
Compare
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.
Closes #2
What changed
Added automatic credit card number detection to
Protect::protect().Card numbers no longer need to be registered explicitly as sensitive values.
Candidates are detected in scalar values, validated using the Luhn checksum
and then passed through the existing
Redact::redact()mechanism.The implementation supports:
I kept the detection separate from the actual redaction logic so existing
custom
Redactcallbacks continue to work unchanged.Why the detection is a little more than a regex
A simple regex works for basic cases, but it becomes unreliable when other
numbers are next to the PAN.
For example:
4111 1111 1111 1111 123or:
Order 123 4111 1111 1111 1111A greedy match can include the extra digits, fail Luhn validation and leave
the actual card number unprotected.
_redactCreditCardSequence()therefore examines the numeric groups inside thematched sequence and redacts only the valid PAN range.
False positives
The detector does not redact a value based only on its length.
Candidates must pass Luhn validation, and repeated-digit values such as
0000000000000000are ignored.Contiguous values longer than 19 digits are also left untouched, even when
they contain a valid PAN as a prefix.
Backward compatibility
One issue I noticed while implementing this was that running every scalar
through
preg_replace_callback()converts non-string values to strings.The implementation returns the original scalar when no redaction occurred, so
integers, floats and booleans keep their original type.
Tests
Added coverage for:
RedactcallbacksFull test suite:
128 tests, 179 assertions