Skip to content

fix(MasterCard): Limit the Mastercard 2 series range to 2221 through 2720 - #1881

Open
zigzagdev wants to merge 2 commits into
Respect:mainfrom
zigzagdev:fix/limit-mastercard-number
Open

zigzagdev wants to merge 2 commits into
Respect:mainfrom
zigzagdev:fix/limit-mastercard-number

Conversation

@zigzagdev

Copy link
Copy Markdown

Summary

The Mastercard pattern now accepts only the 2 series prefixes that Mastercard actually issues.

Motivation

The current pattern accepts every 2 series prefix from 2200 to 2799.
Mastercard only issues 2 series cards in the BIN range 222100 to 272099, so numbers starting with 2200 to 2220 or 2721 to 2799 are wrongly accepted as Mastercard.

referral source

What I have done

I changed the Mastercard regex so that the 2 series part matches only prefixes from 2221 to 2720.
The 51 to 55 range and the 16 digit length stay the same, so every number that is really a Mastercard still passes.

Test Plans

Added the following cases for CreditCard::MASTERCARD.

  • Valid: the lower bound 2221000000000009 and the upper bound 2720000000000005
  • Invalid: 2200000000000004 and 2721000000000004

Test Results

docker run --rm validation-test vendor/bin/phpunit --testsuite=unit
PHPUnit 12.5.33 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.5.12
Configuration: /app/phpunit.xml.dist

.............................................................   61 / 3899 (  1%)
.............................................................  122 / 3899 (  3%)
.............................................................  183 / 3899 (  4%)
.............................................................  244 / 3899 (  6%)
.............................................................  305 / 3899 (  7%)
.............................................................  366 / 3899 (  9%)
.............................................................  427 / 3899 ( 10%)
.............................................................  488 / 3899 ( 12%)
.............................................................  549 / 3899 ( 14%)
.............................................................  610 / 3899 ( 15%)
.............................................................  671 / 3899 ( 17%)
.............................................................  732 / 3899 ( 18%)
.............................................................  793 / 3899 ( 20%)
.............................................................  854 / 3899 ( 21%)
.............................................................  915 / 3899 ( 23%)
.............................................................  976 / 3899 ( 25%)
............................................................. 1037 / 3899 ( 26%)
............................................................. 1098 / 3899 ( 28%)
............................................................. 1159 / 3899 ( 29%)
............................................................. 1220 / 3899 ( 31%)
............................................................. 1281 / 3899 ( 32%)
............................................................. 1342 / 3899 ( 34%)
............................................................. 1403 / 3899 ( 35%)
............................................................. 1464 / 3899 ( 37%)
............................................................. 1525 / 3899 ( 39%)
............................................................. 1586 / 3899 ( 40%)
............................................................. 1647 / 3899 ( 42%)
............................................................. 1708 / 3899 ( 43%)
............................................................. 1769 / 3899 ( 45%)
............................................................. 1830 / 3899 ( 46%)
............................................................. 1891 / 3899 ( 48%)
............................................................. 1952 / 3899 ( 50%)
............................................................. 2013 / 3899 ( 51%)
............................................................. 2074 / 3899 ( 53%)
............................................................. 2135 / 3899 ( 54%)
............................................................. 2196 / 3899 ( 56%)
............................................................. 2257 / 3899 ( 57%)
............................................................. 2318 / 3899 ( 59%)
............................................................. 2379 / 3899 ( 61%)
............................................................. 2440 / 3899 ( 62%)
............................................................. 2501 / 3899 ( 64%)
............................................................. 2562 / 3899 ( 65%)
............................................................. 2623 / 3899 ( 67%)
............................................................. 2684 / 3899 ( 68%)
............................................................. 2745 / 3899 ( 70%)
............................................................. 2806 / 3899 ( 71%)
............................................................. 2867 / 3899 ( 73%)
............................................................. 2928 / 3899 ( 75%)
............................................................. 2989 / 3899 ( 76%)
............................................................. 3050 / 3899 ( 78%)
............................................................. 3111 / 3899 ( 79%)
............................................................. 3172 / 3899 ( 81%)
............................................................. 3233 / 3899 ( 82%)
............................................................. 3294 / 3899 ( 84%)
............................................................. 3355 / 3899 ( 86%)
............................................................. 3416 / 3899 ( 87%)
............................................................. 3477 / 3899 ( 89%)
............................................................. 3538 / 3899 ( 90%)
............................................................. 3599 / 3899 ( 92%)
............................................................. 3660 / 3899 ( 93%)
............................................................. 3721 / 3899 ( 95%)
............................................................. 3782 / 3899 ( 96%)
............................................................. 3843 / 3899 ( 98%)
........................................................      3899 / 3899 (100%)

Time: 00:10.543, Memory: 50.00 MB

OK (3899 tests, 4104 assertions)

The pattern 2[2-7] accepted every prefix from 2200 to 2799, but Mastercard
only issues 2-series cards in the 222100-272099 BIN range, so numbers such
as 2200 (Mir) were wrongly accepted as Mastercard.
Cover the lower (2221) and upper (2720) bounds, plus Luhn-valid numbers
just outside the range (2200, 2721) that must be rejected.
@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.18%. Comparing base (ecccef1) to head (36ebf1a).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #1881   +/-   ##
=========================================
  Coverage     97.18%   97.18%           
  Complexity     1096     1096           
=========================================
  Files           198      198           
  Lines          2555     2555           
=========================================
  Hits           2483     2483           
  Misses           72       72           

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

This branch has not been deployed

No deployments
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.

1 participant