Skip to content

fix: reject non-digit characters in valid_cc_number() - #10580

Open
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:fix/credit-card-ctype-digit
Open

gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:fix/credit-card-ctype-digit

Conversation

@gr8man

@gr8man gr8man commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Description

valid_cc_number() used is_numeric() to reject non-numeric values before the Luhn check. is_numeric() accepts strings that ctype_digit() rejects — e.g. decimal points (5351367.37861108), scientific notation, and sign characters — so such input:

  • bypassed digit-only validation and could pass the Luhn check (parity shifted by the skipped non-digit character), and
  • reached $sumTable[...][$number[$i]] with a non-digit index, triggering an Undefined array key warning/ErrorException.

This PR switches to ctype_digit() (already used elsewhere in system/, e.g. FormatRules::valid_numeric).

Checks

  • vendor/bin/phpunit tests/system/Validation/CreditCardRulesTest.php tests/system/Validation/StrictRules/CreditCardRulesTest.php → OK (456 tests)
  • Added regression cases decimal_point_visa and decimal_point_mastercard that error out under the old is_numeric logic and pass after the fix.

Use ctype_digit() instead of is_numeric() so values such as decimal
points, which is_numeric() accepts, can no longer reach the Luhn check.
Previously a string like '5351367.37861108' passed validation and the
Luhn indexer triggered an 'Undefined array key' warning on non-digit
characters.
@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant