Skip to content

Commit

Permalink
Make regexps more strict and refuse trailing newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Nov 19, 2024
1 parent 15c1f35 commit 66f34ff
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/CustomAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
trait CustomAssertionTrait
{
/** @var string */
private static string $nmtoken_regex = '/^[\w.:-]+$/u';
private static string $nmtoken_regex = '/^[\w.:-]+$/Du';

/** @var string */
private static string $nmtokens_regex = '/^([\w.:-]+)([\s][\w.:-]+)*$/u';
private static string $nmtokens_regex = '/^([\w.:-]+)([\s][\w.:-]+)*$/Du';

/** @var string */
private static string $datetime_regex = '/-?[0-9]{4}-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-999])?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?/i';
private static string $datetime_regex = '/-?[0-9]{4}-(((0(1|3|5|7|8)|1(0|2))-(0[1-9]|(1|2)[0-9]|3[0-1]))|((0(4|6|9)|11)-(0[1-9]|(1|2)[0-9]|30))|(02-(0[1-9]|(1|2)[0-9])))T([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])(\.[0-999])?((\+|-)([0-1][0-9]|2[0-4]):(0[0-9]|[1-5][0-9])|Z)?/Di';

/** @var string */
private static string $duration_regex = '/^([-+]?)P(?!$)(?:(?<years>\d+(?:[\.\,]\d+)?)Y)?(?:(?<months>\d+(?:[\.\,]\d+)?)M)?(?:(?<weeks>\d+(?:[\.\,]\d+)?)W)?(?:(?<days>\d+(?:[\.\,]\d+)?)D)?(T(?=\d)(?:(?<hours>\d+(?:[\.\,]\d+)?)H)?(?:(?<minutes>\d+(?:[\.\,]\d+)?)M)?(?:(?<seconds>\d+(?:[\.\,]\d+)?)S)?)?$/';
private static string $duration_regex = '/^([-+]?)P(?!$)(?:(?<years>\d+(?:[\.\,]\d+)?)Y)?(?:(?<months>\d+(?:[\.\,]\d+)?)M)?(?:(?<weeks>\d+(?:[\.\,]\d+)?)W)?(?:(?<days>\d+(?:[\.\,]\d+)?)D)?(T(?=\d)(?:(?<hours>\d+(?:[\.\,]\d+)?)H)?(?:(?<minutes>\d+(?:[\.\,]\d+)?)M)?(?:(?<seconds>\d+(?:[\.\,]\d+)?)S)?)?$/D';

/** @var string */
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/';
private static string $qname_regex = '/^[a-zA-Z_][\w.-]*:[a-zA-Z_][\w.-]*$/D';

/** @var string */
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/';
private static string $ncname_regex = '/^[a-zA-Z_][\w.-]*$/D';

/** @var string */
private static string $base64_regex = '/^(?:[a-z0-9+\/]{4})*(?:[a-z0-9+\/]{2}==|[a-z0-9+\/]{3}=)?$/i';
Expand Down
2 changes: 2 additions & 0 deletions tests/Assert/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public static function provideDuration(): array
[false, 'P2M1Y'],
[false, 'P'],
[false, 'PT15.S'],
// Trailing newlines are forbidden
[false, "P20M\n"],
];
}
}
2 changes: 2 additions & 0 deletions tests/Assert/NCNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static function provideNCName(): array
[false, 'Te*st'],
[false, '1Test'],
[false, 'Te:st'],
// Trailing newlines are forbidden
[false, "Test\n"],
];
}
}
2 changes: 2 additions & 0 deletions tests/Assert/NMTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public static function provideNMToken(): array
[false, 'foo bar'],
// Commas are forbidden
[false, 'foo,bar'],
// Trailing newlines are forbidden
[false, "foobar\n"],
];
}
}
2 changes: 2 additions & 0 deletions tests/Assert/NMTokensTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static function provideNMTokens(): array
[false, 'foo "bar" baz'],
// Commas are forbidden
[false, 'foo,bar'],
// Trailing newlines are forbidden
[false, "foobar\n"],
];
}
}
2 changes: 2 additions & 0 deletions tests/Assert/QNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static function provideQName(): array
[true, 'Test'],
[false, '1Test'],
[false, 'Te*st'],
// Trailing newlines are forbidden
[false, "some:Test\n"],
];
}
}

0 comments on commit 66f34ff

Please sign in to comment.