Skip to content
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

Serving static files when using the built-in webserver. #4

Merged
merged 3 commits into from
Sep 21, 2013

Conversation

renan
Copy link
Contributor

@renan renan commented Sep 18, 2013

When using built-in webserver static files are not served, instead exceptions are thrown:

2013-09-18 14:43:29 Error: [Cake\Error\MissingControllerException] Controller class Css could not be found.
Exception Attributes: array (
  'class' => 'Css',
  'plugin' => NULL,
  'prefix' => NULL,
)
Request URL: /css/cake.generic.css
Stack Trace:
#0 /Users/renan/Sites/cakephp/3.0-app/webroot/index.php(41): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#1 {main}
2013-09-18 14:43:29 Error: [Cake\Error\MissingControllerException] Controller class Img could not be found.
Exception Attributes: array (
  'class' => 'Img',
  'plugin' => NULL,
  'prefix' => NULL,
)
Request URL: /img/cake.power.gif
Stack Trace:
#0 /Users/renan/Sites/cakephp/3.0-app/webroot/index.php(41): Cake\Routing\Dispatcher->dispatch(Object(Cake\Network\Request), Object(Cake\Network\Response))
#1 {main}

The current AssetDispatcher only tries to serve themed and plugins' assets.

From PHP's documentation:

If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.

In order to serve the static files from webroot directory it is necessary to return false on the router script and let the webserver serve it.

From PHP's documentation:
> If a PHP file is given on the command line when the web server is started it is treated as a "router" script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script's output is returned to the browser.

In order to serve the static files from webroot directory it is necessary to `return false` on the router script and let the webserver serve it.
@@ -21,6 +21,11 @@
// for built-in server
if (php_sapi_name() === 'cli-server') {
$_SERVER['PHP_SELF'] = '/' . basename(__FILE__);

$filename = __DIR__ . '/' . $_SERVER['REQUEST_URI'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't prevent local file inclusion or directory traversal. We should fail to handle any requests that include .. or the %2c in them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make that %2e.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would not be a problem since you are not supposed to use it on production, where this kind of attack usually happens.

I ended up doing something similar to what AssetDispatcher does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not supposed to, but some uneducated person somewhere will do it. And we'll be the person they blame.

@markstory
Copy link
Member

You branch is called 3.0-webserver, but the merge target is master is that intentional?

@dereuromark
Copy link
Member

The app folder is also "3.0-app", so most likely the target of the PR was left on default - which is master - by accident.

@renan
Copy link
Contributor Author

renan commented Sep 18, 2013

The app repository does not contains a 3.0 branch, yet it is compatible with CakePHP 3.0, hence the branch name. I could have called just webserver-fix. Next time I may create on my fork tho.


$url = urldecode($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url;
if (strpos($url, '..') === false && strpos($url, '.') !== false && file_exists($file) && is_file($file)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't is_file() include file_exists() already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, will fix it up.

@renan
Copy link
Contributor Author

renan commented Sep 18, 2013

@markstory You made me wonder. How will be the versioning on this repository? Starting with 3.0 or with its own versioning?

@markstory
Copy link
Member

It will start on 3.0 :)

@markstory
Copy link
Member

Ignore my previous comments about branches + versions, I'm a tool. I totally didn't notice that this was the cakephp/app repo. Sorry for the confusion.

@renan
Copy link
Contributor Author

renan commented Sep 21, 2013

Any more thoughts on this?

markstory added a commit that referenced this pull request Sep 21, 2013
Serving static files when using the built-in webserver.
@markstory markstory merged commit 9fbc0f2 into master Sep 21, 2013
@josegonzalez josegonzalez deleted the 3.0-webserver-fix branch September 21, 2013 23:14
@Zwaen91 Zwaen91 mentioned this pull request Mar 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants