8000 bug #9212 [Validator] Force Luhn Validator to only work with strings … · symfony/symfony@bde28cb · GitHub
[go: up one dir, main page]

Skip to content

Commit bde28cb

Browse files
committed
bug #9212 [Validator] Force Luhn Validator to only work with strings (Richtermeister)
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #9212). Discussion ---------- [Validator] Force Luhn Validator to only work with strings The Luhn Validator fails to work with float or large integers (internally turned into float by php, depending on precision setting). This is problematic because developers might use number or integer form fields to capture credit card data, which will lead to a validation error even though the form input itself was valid. This commit makes validator throw UnexpectedTypeException on non-string input to avoid this confusion. | Q | A | ------------- | --- | Bug fix? | [yes] | New feature? | [no] | BC breaks? | [yes] | Deprecations? | [no] | Tests pass? | [yes] | Fixed tickets | | License | MIT | Doc PR | Commits ------- a9dfd37 Force Luhn Validator to only work with strings
2 parents 78e9acb + 1e410c7 commit bde28cb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Symfony/Component/Validator/Constraints/LuhnValidator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617

1718
/**
1819
* Validates a PAN using the LUHN Algorithm
@@ -38,6 +39,13 @@ public function validate($value, Constraint $constraint)
3839
return;
3940
}
4041

42+
/**
43+
* need to work with strings only because long numbers are treated as floats and don't work with strlen
44+
*/
45+
if (!is_string($value)) {
46+
throw new UnexpectedTypeException($value, 'string');
47+
}
48+
4149
if (!is_numeric($value)) {
4250
$this->context->addViolation($constraint->message);
4351

src/Symfony/Component/Validator/Tests/Constraints/LuhnValidatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,28 @@ public function getInvalidNumbers()
103103
array('1234567812345678'),
104104
array('4222222222222222'),
105105
array('0000000000000000'),
106+
);
107+
}
108+
109+
/**
110+
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
111+
* @dataProvider getInvalidTypes
112+
*/
113+
public function testInvalidTypes($number)
114+
{
115+
$constraint = new Luhn();
116+
117+
$this->validator->validate($number, $constraint);
118+
}
119+
120+
public function getInvalidTypes()
121+
{
122+
return array(
106123
array(0),
124+
array(123),
125+
array(42424242424242424242),
126+
array(378282246310005),
127+
array(371449635398431),
107128
);
108129
}
109130
}

0 commit comments

Comments
 (0)
0