Repository navigation
Conversation
…idatorjs#2717) The Mastercard pattern /^5[1-5][0-9]{2}|(222[1-9]|...)[0-9]{12}$/ has an unparenthesized alternation: because | binds looser than the anchors, the engine parses it as ^5[1-5][0-9]{2} OR (...)[0-9]{12}$, so any string starting with 51-55 matches the first branch regardless of length. isCreditCard('5108') therefore returned true (Luhn passes). Wrap the alternation in a non-capturing group with ^...$ and require the full 16-digit form for the 51-55 branch (5[1-5][0-9]{14}), matching the 2-series branch. A focused regression test asserts '5108' and a spaced partial are rejected; it fails before the fix and passes after. Closes validatorjs#2717 Assisted-by: Alpha (https://github.com/Mutantcat-Working-Group/Alpha) Signed-off-by: tyza66 <shun_@outlook.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2942 +/- ##
=========================================
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 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.
Related issue
Closes #2717
Description
The Mastercard pattern in
src/lib/isCreditCard.jsis/^5[1-5][0-9]{2}|(222[1-9]|…)[0-9]{12}$/. Because|has lower precedence than the^/$anchors, the engine parses it as two independent alternatives:^5[1-5][0-9]{2}— start-anchored only, matches any string beginning with51–55regardless of length.(222[1-9]|…)[0-9]{12}$— end-anchored only.So
isCreditCard("5108")matches branch 1 (4 digits, no length limit), passes Luhn, and returnstruewhen it should befalse.This wraps the alternation in a non-capturing group with
^…$and requires the full 16-digit form for the 51–55 branch (5[1-5][0-9]{14}), matching the 2-series branch:I audited the other card regexes in the file as the issue suggested; they are already correctly grouped and double-anchored, so only
mastercardneeded the change.Testing
Added a focused regression test (
should reject short and partial Mastercard numbers (#2717)) asserting"5108"and a spaced partial are rejected, with and withoutprovider: "mastercard". It fails before the fix (isCreditCard("5108") passed but should have failed) and passes after.npm test(build + ESLint + full Mocha suite): 324 passing, 0 failing, coverage 100% statements / 100% lines. No existing tests changed behavior.AI assistance
This change was written with AI assistance from Alpha. I confirmed the bug against the current code (issue #2717's repro), audited the other card regexes, implemented the anchoring fix, added the regression test, verified it fails before the fix and passes after, and ran
npm testbefore opening this PR. Please review it as AI-generated code.