Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n

private function validateDateTime(string $datetime, string $format): bool
{
$dt = \DateTime::createFromFormat($format, $datetime);
try {
$dt = \DateTime::createFromFormat($format, $datetime);
} catch (\Throwable $e) {
return false;
}

if (!$dt) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ private function validateDateTime(string $datetime, string $format): bool
$input = sprintf('%s59%s', substr($datetime, 0, 6), substr($datetime, 8));
}

$dt = \DateTimeImmutable::createFromFormat($format, $input);
try {
$dt = \DateTimeImmutable::createFromFormat($format, $input);
} catch (\Throwable $e) {
return false;
}

if (!$dt) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ private function validateDateTime(string $datetime, string $format): bool
$input = sprintf('%s59%s', substr($datetime, 0, 6), substr($datetime, 8));
}

$dt = \DateTimeImmutable::createFromFormat($format, $input);
try {
$dt = \DateTimeImmutable::createFromFormat($format, $input);
} catch (\Throwable $e) {
return false;
}

if (!$dt) {
return false;
}
Expand Down
6 changes: 5 additions & 1 deletion src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ public function check(&$element, $schema = null, ?JsonPointer $path = null, $i =

protected function validateDateTime($datetime, $format)
{
$dt = \DateTime::createFromFormat($format, (string) $datetime);
try {
$dt = \DateTime::createFromFormat($format, (string) $datetime);
} catch (\Throwable $e) {
return false;
}

if (!$dt) {
return false;
Expand Down
34 changes: 34 additions & 0 deletions tests/Constraints/Draft06/FormatConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints\Draft06;

use Generator;
use JsonSchema\Constraints\Drafts\Draft06\FormatConstraint;
use JsonSchema\Tests\Constraints\VeryBaseTestCase;

class FormatConstraintTest extends VeryBaseTestCase
{
/**
* @dataProvider getInvalidFormats
*/
public function testInvalidFormat($string, $format): void
{
$validator = new FormatConstraint();
$schema = new \stdClass();
$schema->format = $format;

$validator->check($string, $schema);
$this->assertCount(1, $validator->getErrors(), 'Expected 1 error');
}

public function getInvalidFormats(): Generator
{
yield 'Date-time format with value containing null byte' => ["2020-01-01T12:34:56\x00", 'date-time'];

yield 'Date format with value containing null byte' => ["2020-01-01\x00", 'date'];

yield 'Time format with value containing null byte' => ["13:37:00\x00", 'time'];
}
}
34 changes: 34 additions & 0 deletions tests/Constraints/Draft07/FormatConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints\Draft07;

use Generator;
use JsonSchema\Constraints\Drafts\Draft07\FormatConstraint;
use JsonSchema\Tests\Constraints\VeryBaseTestCase;

class FormatConstraintTest extends VeryBaseTestCase
{
/**
* @dataProvider getInvalidFormats
*/
public function testInvalidFormat($string, $format): void
{
$validator = new FormatConstraint();
$schema = new \stdClass();
$schema->format = $format;

$validator->check($string, $schema);
$this->assertCount(1, $validator->getErrors(), 'Expected 1 error');
}

public function getInvalidFormats(): Generator
{
yield 'Date-time format with value containing null byte' => ["2020-01-01T12:34:56\x00", 'date-time'];

yield 'Date format with value containing null byte' => ["2020-01-01\x00", 'date'];

yield 'Time format with value containing null byte' => ["13:37:00\x00", 'time'];
}
}
34 changes: 34 additions & 0 deletions tests/Constraints/Draft2019/FormatConstraintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints\Draft2019;

use Generator;
use JsonSchema\Constraints\Drafts\Draft2019\FormatConstraint;
use JsonSchema\Tests\Constraints\VeryBaseTestCase;

class FormatConstraintTest extends VeryBaseTestCase
{
/**
* @dataProvider getInvalidFormats
*/
public function testInvalidFormat($string, $format): void
{
$validator = new FormatConstraint();
$schema = new \stdClass();
$schema->format = $format;

$validator->check($string, $schema);
$this->assertCount(1, $validator->getErrors(), 'Expected 1 error');
}

public function getInvalidFormats(): Generator
{
yield 'Date-time format with value containing null byte' => ["2020-01-01T12:34:56\x00", 'date-time'];

yield 'Date format with value containing null byte' => ["2020-01-01\x00", 'date'];

yield 'Time format with value containing null byte' => ["13:37:00\x00", 'time'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use JsonSchema\Constraints\Factory;
use JsonSchema\Constraints\FormatConstraint;

class FormatTest extends BaseTestCase
class FormatConstraintTest extends BaseTestCase
{
/** @var bool */
protected $validateSchema = true;
Expand Down Expand Up @@ -196,10 +196,12 @@ public function getInvalidFormats(): \Generator
yield ['199-01-1', 'date'];
yield ['2012-0-11', 'date'];
yield ['2012-10-1', 'date'];
yield 'Date format with value containing null byte' => ["2020-01-01\x00", 'date'];

yield ['24:01:00', 'time'];
yield ['00:00:60', 'time'];
yield ['25:00:00', 'time'];
yield 'Time format with value containing null byte' => ["13:37:00\x00", 'time'];

yield ['invalid_value_2000-05-01T12:12:12Z', 'date-time'];
yield ['2000-05-01T12:12:12Z_invalid_value', 'date-time'];
Expand Down
Loading