-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Hostname route should throw an exception if route definition contains disallowed character #6870
Conversation
… disallowed character Fixes zendframework#5656
@@ -116,6 +116,9 @@ protected function parseRouteDefinition($def) | |||
|
|||
while ($currentPos < $length) { | |||
preg_match('(\G(?P<literal>[a-z0-9-.]*)(?P<token>[:{\[\]]|$))', $def, $matches, 0, $currentPos); | |||
if (empty($matches)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamlundrigan doesn't preg_match()
return a bool
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It returns a bool
in case of an error. When there are not matches 0
is returned.
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative:
if ( 1 !== preg_match('(\G(?P<literal>[a-z0-9-.]*)(?P<token>[:{\[\]]|$))', $def, $matches, 0, $currentPos) ) {
throw new Exception\RuntimeException('Matched hostname literal contains a disallowed character');
}
Functionally identical, just all on one line. @Ocramius I can update the PR if you'd prefer I check the result this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why so complicated?
// No matches found, or error occured - distinction not necessary, as both cases are actually faulty here
if (!preg_match(..., $matches, ...)) {
throw ...;
}
// else: We DO have $matches here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, I think @Pittiplatsch's approach is good enuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
…in-route-definition' into develop Close #6870
@adamlundrigan merged into |
If a hostname route is constructed with a route containing a disallowed character (one outside of
[a-z0-9-.]
) a RuntimeException should be thrown, as the route is misconfigured.Fixes #5656