From af3f4d019e39c5bf7c793ec4140a9859b946a192 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Wed, 25 Nov 2026 13:43:35 -0600 Subject: [PATCH] fix(isCreditCard): anchor the mastercard alternation as a group 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 #2717 --- src/lib/isCreditCard.js | 2 +- test/validators.test.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib/isCreditCard.js b/src/lib/isCreditCard.js index 938679d39..706e3fb61 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]{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}$/; 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..5d4901380 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -6432,6 +6432,9 @@ describe('Validators', () => { '623491788middle2863855', '6234917882863855suffix', '4716989580001715213', + '5108', + '510805769813', + '1092221000000000009', ], }); });