Fix uuid() accepting {, }, and prefixes inside the string#359
Open
edorian wants to merge 1 commit into
Open
Conversation
Instead of stripping "urn:", "uuid:", "{" and "}" from anywhere in the value before matching.
So strings like "5{}50e8400-..." or an interior "urn:" passed the assertion and the un-normalised string was returned to the caller.
Moved the validated into the regex:
- The prefixes are only accepted at the start
- A leading "{" must be paired with a closing "}" using a PCRE conditional
shadowhand
requested changes
Jun 10, 2026
Comment on lines
+2445
to
+2448
| // 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)) { |
Collaborator
There was a problem hiding this comment.
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.
|
|
||
| // 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) { |
Collaborator
There was a problem hiding this comment.
Having this as a separate condition is probably not necessary. The performance difference is incredibly small with PHP 8+
| @@ -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.
This should actually be removed, it is technically not a valid urn: per RFC 4122/9562, since RFC 2141 defines a URN as urn:<NID>:<NSS>.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi,
A hopefully small fix for UUIDs being allowed to contain the prefixes and {} inside the string.
If you'd prefer the regular expression to be multiple and commented inline, I'd be happy to do that as well.
I hope the test cases are self-explanatory regarding the behavioral changes.
Moved the validation into the regex: