Skip to content

Commit 97e6a35

Browse files
committed
MAGE-1377 Address Codacy escaping complaint
1 parent 5b0af37 commit 97e6a35

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Model/Backend/QueueCron.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
class QueueCron extends Value
99
{
10-
const CRON_REGEX = '/^(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)$/';
10+
const CRON_FORMAT_REGEX = '/^(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)\s+(\*|[0-9,\-\/\*]+)$/';
11+
const CRON_DISALLOW_REGEX = '/[^@a-z0-9\*\-,\/ ]/';
12+
1113

1214
protected array $mappings = [
1315
'@yearly' => '0 0 1 1 *',
@@ -27,8 +29,15 @@ public function beforeSave()
2729
$this->setValue($value);
2830
}
2931

30-
if (!preg_match(self::CRON_REGEX, $value)) {
31-
throw new InvalidCronException("Cron expression \"$value\" is not valid.");
32+
if (!preg_match(self::CRON_FORMAT_REGEX, $value)) {
33+
$safeValue = preg_replace(self::CRON_DISALLOW_REGEX, '', (string) $value);
34+
$msg = ($safeValue !== $value)
35+
? 'Cron expression is invalid.'
36+
: sprintf(
37+
'Cron expression "%s" is not valid.',
38+
$safeValue
39+
);
40+
throw new InvalidCronException($msg);
3241
}
3342

3443
return parent::beforeSave();

Test/Unit/Model/Backend/QueueCronTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function setUp(): void
3131
/**
3232
* @dataProvider valuesProvider
3333
*/
34-
public function testInput($value, $isValid): void
34+
public function testInput($value, $isValid, $canReplay = true): void
3535
{
3636
$this->queueCronModel->setValue($value);
3737

@@ -45,8 +45,11 @@ public function testInput($value, $isValid): void
4545
"Cron expression \"$value\" is not valid but it should be."
4646
);
4747

48+
$msg = $canReplay
49+
? "Cron expression \"$value\" is not valid."
50+
: "Cron expression is invalid.";
4851
$this->assertEquals(
49-
"Cron expression \"$value\" is not valid.",
52+
$msg,
5053
$exception->getMessage()
5154
);
5255
}
@@ -94,6 +97,11 @@ public static function valuesProvider(): array
9497
[
9598
'value' => '@foo', // Not working alias
9699
'isValid' => false
100+
],
101+
[
102+
'value' => '"><script>alert(\'XSS\')</script>',
103+
'isValid' => false,
104+
'canReplay' => false
97105
]
98106
];
99107
}

0 commit comments

Comments
 (0)