Obscure bug with InternalRequests
- Dominant language
- PHP
- Stars
- 9.4k
- Forks
- 1.3k
- PR merge metrics
- No merged PRs in 30d
Description
Been a while since I posted a bug!
When Dingo is routing internally (with Dingo\Api\Http\InternalRequest) via Laravels CLI (artisan) command, any routes that contain the word 'artisan' are broken, as Symfony's base request class determines an incorrect baseUrl based on SCRIPT_FILENAME.
Using an example route `user/{permalink}`, the following request uri would be affected when routing internally: `/users/partisan123`
You get a 404 on the route match in RouteCollection:
```
File: /usr/www/app/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php
Line: 161
#0 /usr/www/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(821): Illuminate\Routing\RouteCollection->match(Object(Dingo\Api\Http\InternalRequest))
#1 /usr/www/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(691): Illuminate\Routing\Router->findRoute(Object(Dingo\Api\Http\InternalRequest))
#2 /usr/www/app/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\InternalRequest))
#3 /usr/www/app/vendor/dingo/api/src/Routing/Adapter/Laravel.php(83): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\InternalRequest))
#4 /usr/www/app/vendor/dingo/api/src/Routing/Router.php(574): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\InternalRequest), 'v1')
#5 /usr/www/app/vendor/dingo/api/src/Dispatcher.php(540): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\InternalRequest))
#6 /usr/www/app/vendor/dingo/api/src/Dispatcher.php(445): Dingo\Api\Dispatcher->dispatch(Object(Dingo\Api\Http\InternalRequest))
```
Tracked it down to this - line ~1787 in Symfony\Component\HttpFoundation\Request::prepareBaseUrl():
```
// If using mod_rewrite or ISAPI_Rewrite strip the script filename
// out of baseUrl. $pos !== 0 makes sure it is not matching a value
// from PATH_INFO or QUERY_STRING
if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && $pos !== 0) {
$baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
}
```
This incorrectly outputs a base URL here of `/users/partisan`, which then causes the Request object to generate a path of `123`.
The correct output should be a bank base URL with a path of `/users/partisan123`.
The solution is to set the SCRIPT_FILENAME env variable to nothing for internal requests. Will submit a PR shortly.
Contributor guide
Assessment
This issue has not been assessed yet.