Handle bad interval for cron schedule#10619
Handle bad interval for cron schedule#10619westonruter wants to merge 12 commits intoWordPress:trunkfrom
Conversation
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
…into trac-64404
| __( 'Event schedule does not exist.' ) | ||
| sprintf( | ||
| /* translators: %s is the interval encoded as JSON */ | ||
| __( 'Event schedule is invalid. Interval must be positive integer, but got: %s' ), |
There was a problem hiding this comment.
Maybe the $recurrence should be included here as well?
There was a problem hiding this comment.
I think this would be good if a named schedule is used, ie if the code doesn't fall back to
wordpress-develop/src/wp-includes/cron.php
Lines 361 to 368 in 8b0250c
peterwilsoncc
left a comment
There was a problem hiding this comment.
I've added a few notes inline.
|
|
||
| // Now we assume something is wrong and fail to schedule. | ||
| if ( 0 === $interval ) { | ||
| if ( ! is_int( $interval ) || $interval <= 0 ) { |
There was a problem hiding this comment.
| if ( ! is_int( $interval ) || $interval <= 0 ) { | |
| if ( ! is_numeric( $interval ) || $interval <= 0 ) { |
Matches the behaivour of the existing check in wp_schedule_event()
| __( 'Event schedule does not exist.' ) | ||
| sprintf( | ||
| /* translators: %s is the interval encoded as JSON */ | ||
| __( 'Event schedule is invalid. Interval must be positive integer, but got: %s' ), |
There was a problem hiding this comment.
I think this would be good if a named schedule is used, ie if the code doesn't fall back to
wordpress-develop/src/wp-includes/cron.php
Lines 361 to 368 in 8b0250c
…into trac-64404
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
|
There's a failing test: |
| __( 'Event schedule with recurrence "%$1s" does not exist or has is invalid. Interval must be positive integer, but got: %$2s' ), | ||
| $recurrence, | ||
| wp_json_encode( $interval ) | ||
| ) |
There was a problem hiding this comment.
It took me a couple of reads to understand what this is trying to tell me. The interval is an internal implementation detail that's not used by anyone calling this function, so I'm not sure it's an actionable message.
Is it possible to reliably distinguish between a schedule that doesn't exist and a schedule that exists but has an invalid interval? If so, let's use two separate error messages, one for each case, then make each error message clearer.
…into trac-64404
Co-authored-by: John Blackbourn <[email protected]>
… wp_reschedule_event() Co-authored-by: John Blackbourn <[email protected]>
There was a problem hiding this comment.
Pull request overview
This pull request adds validation to handle invalid intervals for cron schedules, addressing ticket 64404. When a cron schedule is registered with an invalid interval (non-numeric or ≤ 0), the scheduling functions now return an appropriate error instead of allowing invalid data.
Changes:
- Adds interval validation in
wp_schedule_event()andwp_reschedule_event()to check for non-numeric or non-positive values - Introduces a new 'invalid_interval' error code to distinguish interval problems from missing schedules
- Adds comprehensive test coverage for negative interval scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/wp-includes/cron.php | Adds validation for invalid intervals in both wp_schedule_event() (lines 319-328) and wp_reschedule_event() (lines 458-474), with appropriate error messages distinguishing between missing schedules and invalid intervals |
| tests/phpunit/tests/cron.php | Adds test_invalid_schedule_interval_returns_error() test case with a schedule containing a negative interval, verifies both functions return the correct error code, and adds ticket reference to related test |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
rbcorrales
left a comment
There was a problem hiding this comment.
Tested the PR locally, and left a couple of inline comments on an edge case in the interval validation with a suggestion.
| return false; | ||
| } | ||
|
|
||
| if ( ! is_numeric( $event->interval ) || $event->interval <= 0 ) { |
There was a problem hiding this comment.
Fractional floats between 0 and 1 (like 0.5, 0.1, 0.99) pass this validation because is_numeric(0.5) is true and 0.5 > 0. But PHP's % operator casts floats to int, so 0.5 becomes 0 and the modulo in wp_reschedule_event() still throws DivisionByZeroError.
Casting to (int) here catches these values at validation time, the same way the arithmetic would truncate them:
| if ( ! is_numeric( $event->interval ) || $event->interval <= 0 ) { | |
| if ( ! is_numeric( $event->interval ) || (int) $event->interval <= 0 ) { |
I verified it locally without the cast, wp_schedule_event( time(), 'half_second_schedule', ... ) succeeds and wp_reschedule_event() crashes. With the cast, both return invalid_interval correctly.
|
|
||
| // Now we assume something is wrong and fail to schedule. | ||
| if ( 0 === $interval ) { | ||
| if ( ! is_numeric( $interval ) || $interval <= 0 ) { |
There was a problem hiding this comment.
Same suggestion here:
| if ( ! is_numeric( $interval ) || $interval <= 0 ) { | |
| if ( ! is_numeric( $interval ) || (int) $interval <= 0 ) { |
Trac ticket: https://core.trac.wordpress.org/ticket/64404
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.