Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Improve decimal shape validation #47954

Merged
merged 2 commits into from
Aug 4, 2023

Conversation

timacdonald
Copy link
Member

@timacdonald timacdonald commented Aug 4, 2023

The decimal rules checks that a value is numeric and then checks the value against a regex.

The current decimal validation regex is expecting:

[+-]? an optional positive / negative symbol

\d* zero or more digits

. any character, except new lines

\d* zero of more digits

All of the following values would pass this regex:

+
-
123
+123
-123
123@
123.
+123@
-123@
+123.
-123.
123@456
123.456
+123@456
-123@456
+123.456
-123.456

However, we first check that the value is numeric in PHP's eyes, so that reduces the values that get checked against the regex to the following set:

123
+123
-123
123.
+123.
-123.
123.456
+123.456
-123.456

but scientific notation will also pass the is_numeric check. So we add the following to the possible values. You will note, however, that these do not match the regex:

1.2e3
7E-10

When the preg_match happens, it has 0 results in $matches, and the rule interprets this as the number having 0 decimal places.

I believe this is a bug and the decimal rule should in fact check that the string matches the expected pattern, i.e., the decimal rule fails for scientific notation.

Comment on lines +602 to +604
if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) {
return false;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "dot" has been escaped here. I believe this was a mistake in the original PR.

The dot allows 1 of any character. I believe it should only be a literal ..

@timacdonald timacdonald marked this pull request as ready for review August 4, 2023 01:02
@timacdonald timacdonald changed the title [10.x] improve decimal shape validation [10.x] Improve decimal shape validation Aug 4, 2023
@taylorotwell taylorotwell merged commit a207057 into laravel:10.x Aug 4, 2023
20 checks passed
@timacdonald timacdonald deleted the decimal-shape branch September 13, 2023 06:20
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.

None yet

2 participants