fix(isCreditCard): anchor mastercard regex so partial strings are rejected#2747
Open
LorenzoGalassi wants to merge 1 commit into
Open
fix(isCreditCard): anchor mastercard regex so partial strings are rejected#2747LorenzoGalassi wants to merge 1 commit into
LorenzoGalassi wants to merge 1 commit into
Conversation
…ected
The mastercard pattern was /^5[1-5][0-9]{2}|(...)[0-9]{12}$/. Because the
alternation was not grouped, | bound looser than the ^ and $ anchors, so
the regex matched either "starts with 51-55" (no length bound) or "ends
with a 2-series BIN + 12 digits". As a result isCreditCard("5108"), which
is also Luhn-valid, incorrectly returned true.
Wrap the alternation in a non-capturing group so both anchors and the
trailing [0-9]{12} apply to every branch, and add 5108 to the Mastercard
and no-provider invalid test lists.
Closes validatorjs#2717
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2747 +/- ##
=========================================
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.
Closes #2717
Problem
isCreditCard("5108")returnstrue, and so do many other short or partial strings beginning with51-55.The mastercard pattern is:
mastercard: /^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}$/|binds looser than the^and$anchors, so this is really the union of two half-anchored patterns:^5[1-5][0-9]{2}— start-anchored only, matches any string starting with51XX-55XXregardless of length.(222[1-9]|...|2720)[0-9]{12}$— end-anchored only.So format validation degenerated to "starts with 51-55" or "ends with a 2-series BIN + 12 digits".
"5108"matches branch 1 and also happens to be Luhn-valid, soisCreditCardreturnedtrue.Fix
Wrap the alternation in a non-capturing group so both anchors and the trailing
[0-9]{12}apply to every branch:mastercard: /^(?: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}$/All Mastercard numbers are 16 digits (4-digit BIN prefix + 12), which the grouped pattern now enforces for both the
5xand2xranges.I audited the other six card patterns in this file for the same
|-precedence issue; onlymastercardwas affected. (unionpaylooks unusual but its branches are correctly wrapped in^(...)$, so it is not impacted.)Tests
Added
5108to the invalid lists in both the Mastercard-provider and no-providerisCreditCardtest blocks.npm run build+ fullvalidators.test.jssuite: 249 passing.isCreditCard("5108")andisCreditCard("5108", { provider: "Mastercard" })) and nothing else.51-55and the2221-27202-series BINs) continue to validate;"5108","5108foo"now correctly returnfalse.eslint src testpasses.