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

[12.x] Add validation to cron expression components #53989

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"src/Illuminate/Filesystem/functions.php",
"src/Illuminate/Foundation/helpers.php",
"src/Illuminate/Log/functions.php",
"src/Illuminate/Support/Date/constants.php",
"src/Illuminate/Support/functions.php",
"src/Illuminate/Support/helpers.php"
],
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

trait ManagesFrequencies
{
use ValidatesFrequencies;

/**
* The Cron expression representing the event's frequency.
*
Expand Down Expand Up @@ -386,6 +388,14 @@ public function twiceDailyAt($first = 1, $second = 13, $offset = 0)
*/
protected function hourBasedSchedule($minutes, $hours)
{
if (is_array($hours) || is_int($hours)) {
array_map($this->validateHour(...), (array) $hours);
}

if (is_array($minutes) || is_int($minutes)) {
array_map($this->validateMinute(...), (array) $minutes);
}

$minutes = is_array($minutes) ? implode(',', $minutes) : $minutes;

$hours = is_array($hours) ? implode(',', $hours) : $hours;
Expand Down Expand Up @@ -533,6 +543,8 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0')
{
$this->dailyAt($time);

$this->validateDayOfMonth($dayOfMonth);

return $this->spliceIntoPosition(3, $dayOfMonth);
}

Expand All @@ -546,6 +558,8 @@ public function monthlyOn($dayOfMonth = 1, $time = '0:0')
*/
public function twiceMonthly($first = 1, $second = 16, $time = '0:0')
{
array_map($this->validateDayOfMonth(...), [$first, $second]);

$daysOfMonth = $first.','.$second;

$this->dailyAt($time);
Expand Down Expand Up @@ -619,6 +633,12 @@ public function yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')
{
$this->dailyAt($time);

$this->validateMonth($month);

if (is_int($dayOfMonth)) {
$this->validateDayOfMonth($dayOfMonth);
}

return $this->spliceIntoPosition(3, $dayOfMonth)
->spliceIntoPosition(4, $month);
}
Expand All @@ -633,6 +653,8 @@ public function days($days)
{
$days = is_array($days) ? $days : func_get_args();

array_map($this->validateDayOfWeek(...), $days);

return $this->spliceIntoPosition(5, implode(',', $days));
}

Expand Down
64 changes: 64 additions & 0 deletions src/Illuminate/Console/Scheduling/ValidatesFrequencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Illuminate\Console\Scheduling;

use InvalidArgumentException;

use const Illuminate\Support\Date\{DAYS_PER_WEEK, HOURS_PER_DAY, MINUTES_PER_HOUR, MONTHS_PER_YEAR};

trait ValidatesFrequencies
{
/**
* @param int $hour
*/
protected function validateHour(int $hour)
{
if ($hour < 0 || $hour > HOURS_PER_DAY) {
throw new InvalidArgumentException('Hour cron expression component must be between 0 and '.HOURS_PER_DAY.". [$hour] given");
}
}

/**
* @param int $minute
*/
protected function validateMinute(int $minute)
{
if ($minute < 0 || $minute > MINUTES_PER_HOUR) {
throw new InvalidArgumentException('Minute cron expression component must be between 0 and '.MINUTES_PER_HOUR.". [$minute] given");
}
}

/**
* @param int $dayOfWeek
*/
protected function validateDayOfWeek(int $dayOfWeek)
{
if ($dayOfWeek < 0 || $dayOfWeek > DAYS_PER_WEEK) {
throw new InvalidArgumentException('Day of week cron expression component must be between 0 and '.DAYS_PER_WEEK.". [$dayOfWeek] given");
}
}

/**
* @param int $month
*/
protected function validateMonth(int $month)
{
if ($month < 0 || $month > MONTHS_PER_YEAR) {
throw new InvalidArgumentException('Month cron expression component must be between 0 and '.MONTHS_PER_YEAR.". [$month] given");
}
}

/**
* @param int|string $dayOfMonth
*/
protected function validateDayOfMonth(int|string $dayOfMonth)
{
if (is_string($dayOfMonth)) {
return;
}

if ($dayOfMonth < 0 || $dayOfMonth > 31) {
throw new InvalidArgumentException("Day of month cron expression component must be between 0 and 31 (at most). [$dayOfMonth] given");
}
}
}
23 changes: 23 additions & 0 deletions src/Illuminate/Support/Date/constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Support\Date;

if (! defined(__NAMESPACE__.'\\'.($constant = 'SECONDS_PER_MINUTE'))) {
define(__NAMESPACE__.'\\'.$constant, 60);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'MINUTES_PER_HOUR'))) {
define(__NAMESPACE__.'\\'.$constant, 60);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'HOURS_PER_DAY'))) {
define(__NAMESPACE__.'\\'.$constant, 24);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'DAYS_PER_WEEK'))) {
define(__NAMESPACE__.'\\'.$constant, 7);
}

if (! defined(__NAMESPACE__.'\\'.($constant = 'MONTHS_PER_YEAR'))) {
define(__NAMESPACE__.'\\'.$constant, 12);
}
Loading