Skip to content

fix(isCreditCard): anchor the mastercard alternation as a group - #2976

Open
draarivpatel-ui wants to merge 1 commit into
validatorjs:masterfrom
draarivpatel-ui:fix-mastercard-regex
Open

draarivpatel-ui wants to merge 1 commit into
validatorjs:masterfrom
draarivpatel-ui:fix-mastercard-regex

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #2717

Summary

The mastercard pattern in isCreditCard was:

/^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}$/

The alternation splits the anchors: the first alternative (^5[1-5][0-9]{2}) is anchored at the start but not at the end, and the second is anchored at the end but not at the start. So the regex matches:

  • any string starting with 5[1-5] plus two digits, whatever its length - including the 4-digit 5108, and
  • any string ending with a valid 2-series 16-digit pattern, with an arbitrary prefix - e.g. 1092221000000000009.

Both examples pass the Luhn check, so isCreditCard accepted them end-to-end despite neither being a 16-digit Mastercard number.

Fix

Wrap the prefix alternation in a non-capturing group sharing the [0-9]{12} suffix inside one anchored pattern:

/^(?: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}$/

Both prefix families are 4 digits, so the shared suffix yields exactly 16 digits either way - unchanged acceptance for valid cards (5555555555554444, the full 2221-2720 range), no match for partial, over-long, prefixed or suffixed strings.

Tests

Added regression cases to the should validate credit cards invalid list, including 5108 and 1092221000000000009 (both Luhn-valid, so they genuinely exercise the regex rather than the Luhn check). They fail on the unfixed regex and pass after the fix; npm run build + focused Mocha run (--grep 'credit card'): 9 passing, lint clean.

The mastercard pattern was /^5[1-5][0-9]{2}|(...)[0-9]{12}$/: the first
alternative was anchored at the start but not at the end, and the
second at the end but not at the start. As a result strings of any
length starting with 51-55 plus two digits (e.g. the 4-digit '5108')
or strings merely ending with a valid 2-series pattern (e.g.
'1092221000000000009') matched, and both examples pass the Luhn check,
so isCreditCard accepted them end-to-end.

Wrap the prefix alternation in a group with the shared [0-9]{12}
suffix inside a single anchored pattern, so only proper 16-digit
mastercard numbers match.

Fixes validatorjs#2717
Copilot AI balanced review requested due to automatic review settings November 25, 2026 19:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (9ff3424) to head (af3f4d0).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2976   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2599      2599           
  Branches       658       658           
=========================================
  Hits          2599      2599           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isCreditCard: Mastercard regex anchoring bug accepts 4-digit and partial strings

2 participants