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:
|
|
Heads-up rather than a request: there are now eight open PRs for this same anchoring fix, and mine is one of them:
I checked that they are not merely similar but the same change. Every one of them replaces this line: with a variant that wraps the alternation so So exactly one of these should land, and the rest closed as duplicates. I have no claim on being the one — mine is the oldest except for the five June/July/August ones, and it adds more tests (28 lines covering the two branches, the partial-prefix rejection and the Luhn-invalid case) but that is not a reason to prefer it over someone else's. If you pick another, I will close mine as soon as it lands. The one thing I would ask is that whichever is chosen comes with a test that fails on the current |
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.