-
Notifications
You must be signed in to change notification settings - Fork 8k
Add FILTER_VALIDATE_STRLEN for UTF-8 string length validation #21429
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
Open
masakielastic
wants to merge
7
commits into
php:master
Choose a base branch
from
masakielastic:feature/filter-validate-str
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c49e040
filter: Add FILTER_VALIDATE_STR filter for string length validation
masakielastic 25f05b8
ext/filter: Update filter handlers to return zend_result
masakielastic 81aa54c
Filter: Clarify comment for php_utf8_strlen()
masakielastic ae9d58b
Filter: Validate min_range and max_range option values
masakielastic d827d36
Filter: Rename min_range/max_range options to min_len/max_len
masakielastic 3902fae
Filter: Add tests for invalid min_len/max_len options
masakielastic ceab7ee
filter: Rename FILTER_VALIDATE_STR to FILTER_VALIDATE_STRLEN
masakielastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| --TEST-- | ||
| FILTER_VALIDATE_STR: Emoji string length | ||
| --SKIPIF-- | ||
| <?php if (!extension_loaded("filter")) die("skip"); ?> | ||
| --FILE-- | ||
| <?php | ||
| $options1 = ['options' => ['min_len' => 2, 'max_len' => 2]]; | ||
| $options2 = ['options' => ['max_len' => 2, 'default' => 'error']]; | ||
| $options3 = ['options' => ['min_len' => 0]]; | ||
|
|
||
| var_dump( | ||
| filter_var('ab', filter_id('validate_strlen'), $options1), | ||
| filter_var('🐘🐘', FILTER_VALIDATE_STRLEN, $options1), | ||
| filter_var('🐘', FILTER_VALIDATE_STRLEN, $options1), | ||
| filter_var('🐘🐘🐘', FILTER_VALIDATE_STRLEN, $options1), | ||
| filter_var('🐘🐘🐘', FILTER_VALIDATE_STRLEN, $options2), | ||
| filter_var('', FILTER_VALIDATE_STRLEN, $options3), | ||
| ); | ||
| ?> | ||
| --EXPECT-- | ||
| string(2) "ab" | ||
| string(8) "🐘🐘" | ||
| bool(false) | ||
| bool(false) | ||
| string(5) "error" | ||
| string(0) "" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| --TEST-- | ||
| FILTER_VALIDATE_STR: invalid min_len/max_len options | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| echo "--- min_len negative ---\n"; | ||
| var_dump(filter_var("abc", FILTER_VALIDATE_STRLEN, [ | ||
| "options" => ["min_len" => -1] | ||
| ])); | ||
|
|
||
| echo "--- max_len negative ---\n"; | ||
| var_dump(filter_var("abc", FILTER_VALIDATE_STRLEN, [ | ||
| "options" => ["max_len" => -1] | ||
| ])); | ||
|
|
||
| echo "--- min_len greater than max_len ---\n"; | ||
| var_dump(filter_var("abc", FILTER_VALIDATE_STRLEN, [ | ||
| "options" => [ | ||
| "min_len" => 10, | ||
| "max_len" => 5 | ||
| ] | ||
| ])); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| --- min_len negative --- | ||
|
|
||
| Warning: filter_var(): min_len must be greater than or equal to 0 in %s on line %d | ||
| bool(false) | ||
| --- max_len negative --- | ||
|
|
||
| Warning: filter_var(): max_len must be greater than or equal to 0 in %s on line %d | ||
| bool(false) | ||
| --- min_len greater than max_len --- | ||
|
|
||
| Warning: filter_var(): min_len must be less than or equal to max_len in %s on line %d | ||
| bool(false) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --TEST-- | ||
| FILTER_VALIDATE_STR: Invalid input types | ||
| --SKIPIF-- | ||
| <?php if (!extension_loaded("filter")) die("skip"); ?> | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| /** | ||
| * According to the filter_var() manual: | ||
| * https://www.php.net/manual/en/function.filter-var.php | ||
| * - Scalar types (int, float, bool) are automatically converted to strings before filtering. | ||
| * - Non-scalar types (array, object, resource, null) always result in false. | ||
| * | ||
| * This test ensures FILTER_VALIDATE_STRLEN behaves accordingly for various input types. | ||
| */ | ||
|
|
||
| $options = ['options' => ['min_len' => 2, 'max_len' => 4]]; | ||
| $handle = fopen("php://memory", "r"); | ||
| class Dummy { public $x = 1; } | ||
|
|
||
| var_dump( | ||
| filter_var(1234, FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var(3.14, FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var(['a', 'b'], FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var(new Dummy(), FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var(NULL, FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var(true, FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var(false, FILTER_VALIDATE_STRLEN, $options), | ||
| filter_var($handle, FILTER_VALIDATE_STRLEN, $options) | ||
| ); | ||
|
|
||
| fclose($handle); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| string(4) "1234" | ||
| string(4) "3.14" | ||
| bool(false) | ||
| bool(false) | ||
| bool(false) | ||
| bool(false) | ||
| bool(false) | ||
| bool(false) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| --TEST-- | ||
| FILTER_VALIDATE_STR: Unicode maximal subpart validation (Table 3-11 reference) | ||
| --SKIPIF-- | ||
| <?php if (!extension_loaded("filter")) die("skip"); ?> | ||
| --FILE-- | ||
| <?php | ||
| /** | ||
| * These tests verify that FILTER_VALIDATE_STR correctly counts invalid or truncated UTF-8 sequences | ||
| * as single characters, per Unicode Standard Section 3.9.6: "U+FFFD Substitution of Maximal Subparts" | ||
| * (https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-3/#G66453). | ||
| * | ||
| * The filter only performs validation and does not modify the original data. | ||
| * As long as the length constraint is satisfied, the input byte string is returned. | ||
| * | ||
| * Example 1 (single invalid byte): | ||
| * Input: 61 80 ("a" followed by invalid \x80) | ||
| * Validation: 2 code points (min_range = 2, max_range = 2) → returns original input | ||
| * Expected hex: 6180 | ||
| * | ||
| * Example 2 (Table 3-11 inspired): | ||
| * Input: E1 80 E2 F0 91 92 F1 BF 41 | ||
| * Validation: 5 code points (max_range = 5) → returns original input | ||
| * Expected hex: e180e2f09192f1bf41 | ||
| * | ||
| */ | ||
|
|
||
| $options1 = ['options' => ['min_range' => 2, 'max_range' => 2]]; | ||
| $options2 = ['options' => ['max_range' => 5]]; | ||
|
|
||
| echo bin2hex(filter_var("a\x80", FILTER_VALIDATE_STRLEN, $options1)), "\n"; | ||
| echo bin2hex(filter_var("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", FILTER_VALIDATE_STRLEN, $options2)), "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| 6180 | ||
| e180e2f09192f1bf41 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: look like a copy/paste mistake (same for INADDR_NONE) ?
already defined just above.