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

Class FilteredIterator: add input validation #604

Merged
merged 1 commit into from
Nov 8, 2021

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Nov 7, 2021

This commit adds input validation to the class constructor.

  • The $data parameter is documented to expect an array, but as the class uses ArrayIterator, any iterable should be accepted as valid.
    For non-iterable data an exception will now be thrown.
  • The $callback parameter has to be a valid callback. In contrast to most input validation, for invalid callbacks passed, this parameter will just be ignored (property not set).
    This is in line with the handling of the property in the rest of the class, where the $callback property may be unset after unserialization anyway.

As for the other methods:

  • The public magic/ArrayIterator methods should not need input validation as they should not be called directly, but only indirectly and when called that way, will receive the correct input type.
  • The other public methods do not take parameters.

Includes adding perfunctory tests for the added input validation.

Open question:

  • Should the input check for the $callback parameter even be added ? After all, a type check is already done before using it in the current() method anyway.

This commit adds input validation to the class constructor.
* The `$data` parameter is documented to expect an `array`, but as the class uses `ArrayIterator`, any iterable should be accepted as valid.
    For non-iterable data an exception will now be thrown.
* The `$callback` parameter has to be a valid callback. In contrast to most input validation, for invalid callbacks passed, this parameter will just be ignored (property not set).
    This is in line with the handling of the property in the rest of the class, where the `$callback` property may be unset after unserialization anyway.

As for the other methods:
* The `public` magic/ArrayIterator methods should not need input validation as they should not be called directly, but only indirectly and when called that way, will receive the correct input type.
* The other `public` methods do not take parameters.

Includes adding perfunctory tests for the added input validation.

**Open question:**
* Should the input check for the `$callback` parameter even be added ? After all, a type check is already done before using it in the `current()` method anyway.
@jrfnl jrfnl added this to the 2.0.0 milestone Nov 7, 2021
@jrfnl jrfnl requested a review from schlessera November 7, 2021 15:45
@jrfnl jrfnl mentioned this pull request Nov 7, 2021
28 tasks
@schlessera
Copy link
Member

schlessera commented Nov 8, 2021

Should the input check for the $callback parameter even be added ? After all, a type check is already done before using it in the current() method anyway.

Yes, the integrity of the class should already be ensured in the constructor. What good is an iterator you can instantiate but then cannot iterate upon?

EDIT: Ah, right, the callback is only added/used conditionally.
Either way, I'd go with the check in the constructor, as it types the property in a strict way, which will improve static analysis. The check in the current() method could then be removed, which is executed per iteration, instead of only once during instantiation, improving performance as a side-effect.

parent::__construct($data);

$this->callback = $callback;
if (is_callable($callback)) {
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned in the comment, I'd stick with the check here, but remove the one in current() (which runs unnecessarily often).

Copy link
Member Author

Choose a reason for hiding this comment

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

We can't remove the check in current() as $callback will be unset when the object is serialized and then unserialized (previous security fix).

@schlessera schlessera merged commit fac6c6b into develop Nov 8, 2021
@schlessera schlessera deleted the feature/filterediterator-add-input-validation branch November 8, 2021 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants