Skip to content

Commit 5012a01

Browse files
committed
Be lenient in what we accept, like dateTime/time with trailing sub-second zeros
1 parent 7520e73 commit 5012a01

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/XMLSchema/Type/DateTimeValue.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
1212
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
1313

14+
use function rtrim;
15+
use function strlen;
16+
use function strpos;
17+
use function substr;
18+
1419
/**
1520
* @package simplesaml/xml-common
1621
*/
@@ -28,7 +33,28 @@ class DateTimeValue extends AbstractAnySimpleType
2833
*/
2934
protected function sanitizeValue(string $value): string
3035
{
31-
return static::collapseWhitespace(static::normalizeWhitespace($value));
36+
$normalized = static::collapseWhitespace(static::normalizeWhitespace($value));
37+
38+
// Trim any trailing zero's from the sub-seconds
39+
$decimal = strrpos($normalized, '.');
40+
if ($decimal !== false) {
41+
$timezone = strrpos($normalized, '+') ?? strrpos($normalized, '-') ?? strrpos($normalized, 'Z');
42+
if ($timezone !== false) {
43+
$subseconds = substr($normalized, $decimal + 1, strlen($normalized) - $timezone);
44+
} else {
45+
$subseconds = substr($normalized, $decimal + 1);
46+
}
47+
48+
$subseconds = rtrim($subseconds, '0');
49+
if ($subseconds === '') {
50+
return substr($normalized, 0, $decimal);
51+
}
52+
return substr($normalized, 0, $decimal + 1)
53+
. $subseconds
54+
. (($timezone === false) ? '' : substr($normalized, $timezone));
55+
}
56+
57+
return $normalized;
3258
}
3359

3460

src/XMLSchema/Type/TimeValue.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
99
use SimpleSAML\XMLSchema\Type\Interface\AbstractAnySimpleType;
1010

11+
use function rtrim;
12+
use function strlen;
13+
use function strpos;
14+
use function substr;
15+
1116
/**
1217
* @package simplesaml/xml-common
1318
*/
@@ -23,7 +28,28 @@ class TimeValue extends AbstractAnySimpleType
2328
*/
2429
protected function sanitizeValue(string $value): string
2530
{
26-
return static::collapseWhitespace(static::normalizeWhitespace($value));
31+
$normalized = static::collapseWhitespace(static::normalizeWhitespace($value));
32+
33+
// Trim any trailing zero's from the sub-seconds
34+
$decimal = strrpos($normalized, '.');
35+
if ($decimal !== false) {
36+
$timezone = strrpos($normalized, '+') ?? strrpos($normalized, '-') ?? strrpos($normalized, 'Z');
37+
if ($timezone !== false) {
38+
$subseconds = substr($normalized, $decimal + 1, strlen($normalized) - $timezone);
39+
} else {
40+
$subseconds = substr($normalized, $decimal + 1);
41+
}
42+
43+
$subseconds = rtrim($subseconds, '0');
44+
if ($subseconds === '') {
45+
return substr($normalized, 0, $decimal);
46+
}
47+
return substr($normalized, 0, $decimal + 1)
48+
. $subseconds
49+
. (($timezone === false) ? '' : substr($normalized, $timezone));
50+
}
51+
52+
return $normalized;
2753
}
2854

2955

tests/XMLSchema/Type/DateTimeValueTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public static function provideValidDateTime(): array
6161
{
6262
return [
6363
'whitespace collapse' => [true, ' 2001-10-26T21:32:52 '],
64+
'trailing sub-second zero' => [true, '2001-10-26T21:32:52.1230'],
65+
'all trailing sub-second zero' => [true, '2001-10-26T21:32:52.00'],
6466
];
6567
}
6668

tests/XMLSchema/Type/TimeValueTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public static function provideValidTime(): array
4747
{
4848
return [
4949
'whitespace collapse' => [true, "\n 21:32:52.12679\t "],
50+
'trailing sub-second zero' => [true, '21:32:52.1230'],
51+
'all trailing sub-second zero' => [true, '21:32:52.00'],
5052
];
5153
}
5254

0 commit comments

Comments
 (0)