fix(isCreditCard): anchor Mastercard regex to reject partial strings#2741
Open
abhu85 wants to merge 1 commit into
Open
fix(isCreditCard): anchor Mastercard regex to reject partial strings#2741abhu85 wants to merge 1 commit into
abhu85 wants to merge 1 commit into
Conversation
The Mastercard pattern used a top-level alternation with no grouping, so
the ^ and $ anchors each bound to only one branch:
^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}$
Branch 1 was start-anchored only, so any string beginning with 51-55
(e.g. "5108") that also passed the Luhn check validated as a Mastercard;
branch 2 was end-anchored only. Wrapping the alternation in a
non-capturing group makes both anchors apply to every branch, restoring
the intended full 16-digit format check.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2741 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 114 114
Lines 2587 2587
Branches 656 656
=========================================
Hits 2587 2587 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
What
The Mastercard pattern in
src/lib/isCreditCard.jsused a top-level alternation with no grouping, so the^and$anchors each bound to only one branch:/^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}$/Because
|has lower precedence than the anchors, this parses as two patterns:^5[1-5][0-9]{2}— start-anchored only, matches any string beginning with51–55regardless of length.(222[1-9]|…|2720)[0-9]{12}$— end-anchored only.So
isCreditCard("5108")returnedtrue:"5108"matches branch 1 and also passes the Luhn check (5×2=10→1, +0, +1, +8 = 10). Fifty 4-digit Luhn-valid strings in the51–55range are wrongly accepted.Fix
Wrap the alternation in a non-capturing group so both anchors apply to every branch, restoring the intended full 16-digit format:
/^(?: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}$/After the fix
isCreditCard("5108")returnsfalse, and all previously-valid 16-digit Mastercard numbers (5-series and 2-series) continue to validate.I audited the other card patterns in the file: only
mastercardhad a correctness bug. (unionpayhas a redundant nested^^but is functionally correct; left untouched to keep this PR scoped.)References
51–55(legacy) and2221–2720(2017 expansion), 16 digits.Checklist
"5108"added to the no-provider and Mastercardinvalidfixtures