Skip to content

fix(isCreditCard): anchor Mastercard regex and require 16 digits (#2717) - #2942

Open
tyza66 wants to merge 1 commit into
validatorjs:masterfrom
tyza66:fix/iscreditcard-mastercard-anchoring
Open

tyza66 wants to merge 1 commit into
validatorjs:masterfrom
tyza66:fix/iscreditcard-mastercard-anchoring

Conversation

@tyza66

@tyza66 tyza66 commented Oct 22, 2026

Copy link
Copy Markdown

Related issue

Closes #2717

Description

The Mastercard pattern in src/lib/isCreditCard.js is /^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:

  1. ^5[1-5][0-9]{2} — start-anchored only, matches any string beginning with 51–55 regardless of length.
  2. (222[1-9]|…)[0-9]{12}$ — end-anchored only.

So isCreditCard("5108") matches branch 1 (4 digits, no length limit), passes Luhn, and returns true when it should be false.

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:

mastercard: /^(?:5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12})$/,

I audited the other card regexes in the file as the issue suggested; they are already correctly grouped and double-anchored, so only mastercard needed 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 without provider: "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 test before opening this PR. Please review it as AI-generated code.

…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>
Copilot AI balanced review requested due to automatic review settings October 22, 2026 05:54

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 reached their quota limit.

@codecov

codecov Bot commented Oct 22, 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 (ab46ca0).

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.
📢 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