-
Notifications
You must be signed in to change notification settings - Fork 162
Fix uuid() accepting {, }, and prefixes inside the string
#359
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2442,24 +2442,18 @@ public static function uuid(mixed $value, string|callable $message = ''): string | |
| { | ||
| static::string($value, $message); | ||
|
|
||
| $originalValue = $value; | ||
| $value = \str_replace(['urn:', 'uuid:', '{', '}'], '', $value); | ||
|
|
||
| // The nil UUID is special form of UUID that is specified to have all | ||
| // 128 bits set to zero. | ||
| if ('00000000-0000-0000-0000-000000000000' === $value) { | ||
| return $originalValue; | ||
| } | ||
|
|
||
| if (!\preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/D', $value)) { | ||
| // Accepts the plain form (including the nil UUID with all 128 bits | ||
| // set to zero), optionally preceded by "urn:" and/or "uuid:" and | ||
| // optionally wrapped in a matching pair of curly braces. | ||
| if (!\preg_match('/^(?:urn:)?(?:uuid:)?(\{)?[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}(?(1)\})$/D', $value)) { | ||
|
Comment on lines
+2445
to
+2448
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to break out these different forms, such that it looks like: if (\str_starts_with($value, 'urn:uuid:') && \preg_match(...)) {
return $value;
}
if (\str_starts_with($value, 'uuid:') && \preg_match(...)) {
return $value;
}
if (\str_starts_with($value, '{') && \str_ends_with('}') && \preg_match(...)) {
return $value;
}
if (\preg_match(...)) {
return $value;
}
// resolve message, report invalidWhile more verbose, having separate forks for each form makes intent much more clear and provides more meaningful code coverage.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given So do you want something like this that reuses the main regex: diff --git a/src/Assert.php b/src/Assert.php
index 089b020..31c861b 100644
--- a/src/Assert.php
+++ b/src/Assert.php
@@ -2442,18 +2442,33 @@ class Assert
{
static::string($value, $message);
- // Accepts the plain form (including the nil UUID with all 128 bits
- // set to zero), optionally preceded by "urn:" and/or "uuid:" and
- // optionally wrapped in a matching pair of curly braces.
- if (!\preg_match('/^(?:urn:)?(?:uuid:)?(\{)?[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}(?(1)\})$/D', $value)) {
- $message = self::resolveMessage($message);
- static::reportInvalidArgument(\sprintf(
- $message ?: 'Value %s is not a valid UUID.',
- static::valueToString($value)
- ));
+ $uuid = '[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}';
+
+ if (\str_starts_with($value, 'urn:uuid:') && \preg_match('/^urn:uuid:'.$uuid.'$/D', $value)) {
+ return $value;
}
- return $value;
+ if (\str_starts_with($value, 'uuid:') && \preg_match('/^uuid:(?:'.$uuid.'|\{'.$uuid.'\})$/D', $value)) {
+ return $value;
+ }
+
+ if (\str_starts_with($value, '{') && \str_ends_with($value, '}') && \preg_match('/^\{'.$uuid.'\}$/D', $value)) {
+ return $value;
+ }
+
+ if (\preg_match('/^'.$uuid.'$/D', $value)) {
+ return $value;
+ }
+
+ $message = self::resolveMessage($message);
+ static::reportInvalidArgument(\sprintf(
+ $message ?: 'Value %s is not a valid UUID.',
+ static::valueToString($value)
+ ));
}Alternatively stripping the I'm happy to do whatever you want here, no stake in the specific solution. Just trying to understand :)
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your suggested code looks good to me. |
||
| $message = self::resolveMessage($message); | ||
| static::reportInvalidArgument(\sprintf( | ||
| $message ?: 'Value %s is not a valid UUID.', | ||
| static::valueToString($value) | ||
| )); | ||
| } | ||
|
|
||
| return $originalValue; | ||
| return $value; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -588,7 +588,9 @@ public static function getTests(): array | |
| ['isNonEmptyMap', [[1, 2, 3]], false], | ||
| ['uuid', ['00000000-0000-0000-0000-000000000000'], true], | ||
| ['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should actually be removed, it is technically not a valid |
||
| ['uuid', ['urn:uuid:ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true], | ||
| ['uuid', ['uuid:{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], true], | ||
| ['uuid', ['{ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], true], | ||
| ['uuid', ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], true], | ||
| ['uuid', ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'], true], | ||
| ['uuid', ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'], true], | ||
|
|
@@ -603,6 +605,12 @@ public static function getTests(): array | |
| ['uuid', ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'], false], | ||
| ['uuid', ['af6f8cb-c57d-11e1-9b21-0800200c9a66'], false], | ||
| ['uuid', ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'], false], | ||
| ['uuid', ['f{}f6f8cb0-c57d-21e1-9b21-0800200c9a66'], false], | ||
| ['uuid', ['ff6f8cb0-c57d-21e1-9b21-08002urn:00c9a66'], false], | ||
| ['uuid', ['urn:ff6f8cb0-c57d-21e1-9b21-0800200c9a66uuid:'], false], | ||
| ['uuid', ['{}{}ff6f8cb0-c57d-21e1-9b21-0800200c9a66{}{}'], false], | ||
| ['uuid', ['{ff6f8cb0-c57d-21e1-9b21-0800200c9a66'], false], | ||
| ['uuid', ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66}'], false], | ||
| ['throws', [function () { throw new LogicException('test'); }, 'LogicException'], true], | ||
| ['throws', [function () { throw new LogicException('test'); }, 'IllogicException'], false], | ||
| ['throws', [function () { throw new Exception('test'); }], true], | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.