WP_Route icon indicating copy to clipboard operation
WP_Route copied to clipboard

Routes are not matched correctly to the request

Open darrena092 opened this issue 5 years ago • 1 comments

Hi!

I was just giving this a try today and noticed something quite odd - route handlers were being called when the request uri did not match the route.

This appears to be because of the following in handle():

        $method            = strtoupper($_SERVER['REQUEST_METHOD']);
        $routes              = array_merge($this->routes[$method], $this->routes['ANY']);  
    	$requestURI 		 = $this->requestURI();
    	$tokenizedRequestURI = $this->tokenize($requestURI);

    	foreach($routes as $key => $route){
    		// First, filter routes that do not have equal tokenized lengths
    		if(count($this->tokenize($route->route)) !== count($tokenizedRequestURI)){
    			unset($routes[$key]);
    			continue;
    		}

    		// Add more filtering here as routing gets more complex.
    	}

The routes are only matched based on the number of tokens in use (URLs are tokenised by being split using "/" as the delimiter). So, I could go to /documents/generate/pdf, or /wp/wp-admin/admin.php and it will call the handler for /documents/generate/pdf.

I might submit a PR for this if I get time, but just thought it was worth flagging.

darrena092 avatar Dec 03 '18 17:12 darrena092

This library is basically unusable.

floriansegginger avatar Jan 18 '19 09:01 floriansegginger