diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index 938679d39..950c8c30c 100644 --- a/src/lib/isCreditCard.js +++ b/src/lib/isCreditCard.js @@ -6,7 +6,7 @@ const cards = { dinersclub: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, discover: /^6(?:011|5[0-9][0-9])[0-9]{12,15}$/, jcb: /^(?:2131|1800|35\d{3})\d{11}$/, - mastercard: /^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}$/, // /^[25][1-7][0-9]{14}$/; + 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})$/, unionpay: /^(6[27][0-9]{14}|^(81[0-9]{14,17}))$/, visa: /^(?:4[0-9]{12})(?:[0-9]{3,6})?$/, }; diff --git a/test/validators.test.js b/test/validators.test.js index 98d2a12ff..e4e88b01b 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -6635,6 +6635,34 @@ describe('Validators', () => { }); + it('should reject short and partial Mastercard numbers (#2717)', () => { + // The Mastercard pattern's unanchored alternation (`^5[1-5][0-9]{2}|...`) + // used to match any 51-55 prefix regardless of length (#2717). + test({ + validator: 'isCreditCard', + valid: [ + '5105105105105100', + '5398228707871527', + ], + invalid: [ + '5108', + '5108 5108 5108 5108 5108', + ], + }); + test({ + validator: 'isCreditCard', + args: [{ provider: 'mastercard' }], + valid: [ + '5105105105105100', + ], + invalid: [ + '5108', + '5108 5108 5108 5108 5108', + ], + }); + }); + + it('should validate Union Pay provided credit cards', () => { test({ validator: 'isCreditCard',