Skip to content

fix(isCreditCard): anchor Mastercard alternation so partial strings are rejected - #2854

Open
stevechen256-source wants to merge 1 commit into
validatorjs:masterfrom
stevechen256-source:fix/isCreditCard-mastercard-anchoring
Open

fix(isCreditCard): anchor Mastercard alternation so partial strings are rejected#2854
stevechen256-source wants to merge 1 commit into
validatorjs:masterfrom
stevechen256-source:fix/isCreditCard-mastercard-anchoring

Conversation

@stevechen256-source

Copy link
Copy Markdown

What & Why

isCreditCard("5108") (and any trailing-junk / short string beginning with 51-55, e.g. "5108foo") incorrectly returned true because the Mastercard regex's | alternation was not grouped, so only the first branch carried the ^ anchor. This let any string starting with a valid 2-series BIN prefix pass regardless of length. It is a correctness/security bug for any call site that trusts isCreditCard for format validation.

Changes

  • src/lib/isCreditCard.js: wrap the Mastercard alternation in (?:...) so ^ and $ anchor the whole pattern. Valid 16-digit Mastercard numbers (incl. 2-series 2221–2720) are unaffected.

Test plan

  • mocha --grep "credit card" passes (9 passing).
  • Full suite passes: 252 passing, 0 failing.
  • isCreditCard("5108")false; isCreditCard("5108foo")false.
  • isCreditCard("5398228707871527") and isCreditCard("2222155765072228")true (unchanged).

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