Repository navigation
fix(isCreditCard): anchor the mastercard alternation as a group - #2976
Open
draarivpatel-ui wants to merge 1 commit into
Open
draarivpatel-ui wants to merge 1 commit into
draarivpatel-ui wants to merge 1 commit into
Conversation
The mastercard pattern was /^5[1-5][0-9]{2}|(...)[0-9]{12}$/: the first
alternative was anchored at the start but not at the end, and the
second at the end but not at the start. As a result strings of any
length starting with 51-55 plus two digits (e.g. the 4-digit '5108')
or strings merely ending with a valid 2-series pattern (e.g.
'1092221000000000009') matched, and both examples pass the Luhn check,
so isCreditCard accepted them end-to-end.
Wrap the prefix alternation in a group with the shared [0-9]{12}
suffix inside a single anchored pattern, so only proper 16-digit
mastercard numbers match.
Fixes validatorjs#2717
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2976 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2599 2599
Branches 658 658
=========================================
Hits 2599 2599 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Nov 28, 2026
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.
Fixes #2717
Summary
The mastercard pattern in
isCreditCardwas:/^5[1-5][0-9]{2}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/The alternation splits the anchors: the first alternative (
^5[1-5][0-9]{2}) is anchored at the start but not at the end, and the second is anchored at the end but not at the start. So the regex matches:5[1-5]plus two digits, whatever its length - including the 4-digit5108, and1092221000000000009.Both examples pass the Luhn check, so
isCreditCardaccepted them end-to-end despite neither being a 16-digit Mastercard number.Fix
Wrap the prefix alternation in a non-capturing group sharing the
[0-9]{12}suffix inside one anchored pattern:/^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/Both prefix families are 4 digits, so the shared suffix yields exactly 16 digits either way - unchanged acceptance for valid cards (
5555555555554444, the full 2221-2720 range), no match for partial, over-long, prefixed or suffixed strings.Tests
Added regression cases to the
should validate credit cardsinvalid list, including5108and1092221000000000009(both Luhn-valid, so they genuinely exercise the regex rather than the Luhn check). They fail on the unfixed regex and pass after the fix;npm run build+ focused Mocha run (--grep 'credit card'): 9 passing, lint clean.