fix: reject a whitespace-only value in isBoolean() - #617
Closed
raphyabak wants to merge 1 commit into
Closed
Conversation
filter_var() with FILTER_VALIDATE_BOOLEAN trims its input before
matching, so a value that is only spaces or tabs has nothing left to
match and is treated as the valid boolean false - it is not a
'failure' that FILTER_NULL_ON_FAILURE would catch.
isBoolean() only guarded against the exact empty string ('' === $value),
so a value that is empty only after trimming slipped past that guard
and was then accepted as false by filter_var(). A genuinely empty
value already failed correctly, since '' === '' is true.
Trim before the empty check, mirroring the guard notEmpty() already
uses, so a whitespace-only value is rejected the same way an empty
one is.
Collaborator
|
This is a major breaking change, which is why I opened an issue to track this, instead of implementing it. Please do not just point AI an issue without maintainer approval - if I wanted to do that, I could do that myself. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #611
filter_var()withFILTER_VALIDATE_BOOLEANtrims its input before matching, so a value that is only spaces or tabs has nothing left to match and is treated as the valid booleanfalse— it is not a "failure" thatFILTER_NULL_ON_FAILUREwould catch:isBoolean()only guarded against the exact empty string ($value === ''), so a value that is empty only after trimming slipped past that guard and was then accepted asfalsebyfilter_var(). A genuinely empty value already failed correctly, since'' === ''is true — only whitespace-only values slipped through.Fix
Trim before the empty check, mirroring the guard
notEmpty()already uses elsewhere in this same class, so a whitespace-only value is rejected the same way an empty one is.As noted in the issue, this is a behaviour change — a whitespace-only value that currently passes validation would start failing — so it targets
masterrather than a5.xpatch release.Test plan
INVALID_WHITESPACE=" "totests/fixtures/env/booleans.envand wired it intoinvalidBooleanValuesDataProvider(), so it's exercised by the existingtestCanInvalidateNonBooleans/testCanInvalidateNonBooleansIfPresenttests alongside every other invalid-boolean case.isBoolean()passing a whitespace-only value), and pass with the fix.vendor/bin/phpunit): 282 tests, all passing (280 pre-existing + 2 new).vendor/bin/phpstan analyze(configured scope:src, levelmax): no errors.