fix(isCreditCard): fix Mastercard regex anchoring bug#2739
Open
algojogacor wants to merge 1 commit into
Open
Conversation
The Mastercard regex had a regex operator precedence bug due to unanchored
alternation. The pattern:
/^5[1-5][0-9]{2}|(222[1-9]|...)[0-9]{12}$/
Due to | having lower precedence than ^ and $, this parsed as:
1. ^5[1-5][0-9]{2} (start-anchored only — matches any string starting
with 51XX-55XX regardless of length)
2. (222[1-9]|...)[0-9]{12}$ (end-anchored only)
This caused isCreditCard to return true for partial strings like '5108'
and short 4-digit inputs.
Fixed by wrapping the alternation in a non-capturing group with both
anchors outside: ^(?:5[1-5][0-9]{14}|...)$
Closes validatorjs#2717
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2739 +/- ##
=========================================
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.
This is an independent contribution submitted as part of an open-source contribution practice. It is not affiliated with any hackathon or competition.
Summary
The Mastercard regex pattern in
isCreditCardhas a regex operator precedence bug that causes it to accept partial strings like"5108"as valid credit card numbers.Root Cause
The original regex:
Due to
|having lower precedence than^and$, this parsed as two unanchored alternatives:^5[1-5][0-9]{2}— start-anchored only, matches any string beginning with 51XX–55XX regardless of length(222[1-9]|...)[0-9]{12}$— end-anchored onlyThis allowed inputs such as
"5108"(4 digits) to pass the format check.Fix
Wrapped the alternation in a non-capturing group so both anchors bind to all branches:
Also fixed the 51-55 branch which was missing its trailing digit quantifier
[0-9]{12}.Testing
"5108"now correctly returnsfalse"5398228707871527"continue to return `true"Closes #2717