Problem/Motivation

TckkValidator::validate() (src/TckkValidator.php) has three bugs that make it both accept invalid numbers and reject valid ones:

  1. Leading zero check never matches. $tc_kimlik[0] === 0 compares a string with an integer, so it is always FALSE. Numbers starting with 0 are not rejected; 00000000000 passes.
  2. Wrong modulo for the 10th digit. abs($even - $odd) % 10 gives the wrong result when 7 * (d1+d3+d5+d7+d9) - (d2+d4+d6+d8) is negative. The check digit must be the mathematical (non-negative) modulo.
  3. Non-digits are stripped before validation. preg_replace('/[^0-9]/', '', ...) runs before the checks, so input such as 1000000014a6 validates, while the raw value (with the letter) is what gets stored.

Steps to reproduce

drush php:eval '$v = \Drupal::service("tckk_field.validator");
foreach (["10000000146", "00000000000", "19090909018", "19090909096", "1000000014a6"] as $n) {
  echo $n, " ", var_export($v->validate($n), TRUE), "\n";
}'
Input Current result Expected
10000000146 valid valid
00000000000 valid invalid (leading zero)
19090909018 invalid valid (negative difference case)
19090909096 valid invalid (negative difference case)
1000000014a6 valid invalid (non-digit)

Proposed resolution

  • Reject anything that does not match /^[1-9][0-9]{10}$/ instead of stripping characters.
  • Compute the 10th digit as ((7 * $odd - $even) % 10 + 10) % 10.
  • Add a unit test (tests/src/Unit/TckkValidatorTest.php) covering the cases above.

Remaining tasks

  • Patch / merge request
  • Unit tests
  • Review

User interface changes

None.

API changes

None. validate() keeps its signature; it only returns correct results.

Data model changes

None. Previously stored values containing non-digits or a leading zero would fail validation on the next save.

Issue fork tckk_field-3630848

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

orkutmuratyilmaz created an issue. See original summary.

  • b08f1248 committed on 1.0.x
    Issue #3630848: Fix TckkValidator accepting invalid and rejecting valid...
orkutmuratyilmaz’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.