Bug 52403 - FallbackResource doesn't work for non-existent PHP files in webroot
Summary: FallbackResource doesn't work for non-existent PHP files in webroot
Status: NEW
Alias: None
Product: Apache httpd-2
Classification: Unclassified
Component: mod_dir (show other bugs)
Version: 2.2.20
Hardware: PC Mac OS X 10.4
: P2 normal with 6 votes (vote)
Target Milestone: ---
Assignee: Apache HTTPD Bugs Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-31 00:33 UTC by Parry
Modified: 2015-10-11 18:57 UTC (History)
4 users (show)



Attachments
Proposed patch (3.49 KB, text/plain)
2012-10-21 05:51 UTC, Christophe JAILLET
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Parry 2011-12-31 00:33:19 UTC
Server info:

Linux ubuntu-server 3.0.0-14-server #23-Ubuntu SMP Mon Nov 21 20:49:05 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
Server version: Apache/2.2.20 (Ubuntu)
PHP 5.3.6-13ubuntu3.3 with Suhosin-Patch (cli) (built: Dec 13 2011 18:18:37) 

I am using the default apache website except that I have the "FallbackResource /index.php" configured under <Directory /var/www/>. The fallback to the index.php seems to work great EXCEPT when I request a non-existent PHP file from the webroot directory. Any other file extension works fine in the webroot but any filename with the .php extension gets a 404 error response. If I call that non-existent PHP file from a bogus/non-existent directory, it works.

For example:
GET /a.php <--404 Error
GET /bogus/a.php <--Falls back to /index.php
GET /a.html <--Falls back to /index.php

See output below:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /a.php HTTP/1.1
HOST: localhost
Connection: close

HTTP/1.1 404 Not Found
Date: Sat, 31 Dec 2011 00:21:54 GMT
Server: Apache/2.2.20 (Ubuntu)
Vary: Accept-Encoding
Content-Length: 278
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /a.php was not found on this server.</p>
<hr>
<address>Apache/2.2.20 (Ubuntu) Server at localhost Port 80</address>
</body></html>
Connection closed by foreign host.


Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /nodir/a.php HTTP/1.1
HOST: localhost
Connection: close

HTTP/1.1 200 OK
Date: Sat, 31 Dec 2011 00:22:11 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.3
Vary: Accept-Encoding
Content-Length: 25
Connection: close
Content-Type: text/html

/nodir/a.php
localhost
Connection closed by foreign host.



Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /a.html HTTP/1.1 
HOST: localhost
Connection: close

HTTP/1.1 200 OK
Date: Sat, 31 Dec 2011 00:32:38 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.3
Vary: Accept-Encoding
Content-Length: 20
Connection: close
Content-Type: text/html

/a.html
localhost
Connection closed by foreign host.


Thanks,
Parry
Comment 1 Parry 2011-12-31 02:40:26 UTC
I am able to get this working correctly using mod_rewrite with the following basic lines in .htaccess but would obviously prefer the FallbackResource instead:

(.htaccess in webroot)
RewriteEngine On
RewriteCond /var/www/%{REQUEST_FILENAME} !-f
RewriteCond /var/www/%{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /a.php HTTP/1.1
HOST: localhost
Connection: close

HTTP/1.1 200 OK
Date: Sat, 31 Dec 2011 02:37:48 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.3
Vary: Accept-Encoding
Content-Length: 43
Connection: close
Content-Type: text/html

/a.php
localhost

Connection closed by foreign host.
Comment 2 Christophe JAILLET 2012-10-21 05:51:10 UTC
Created attachment 29501 [details]
Proposed patch
Comment 3 Christophe JAILLET 2012-10-21 05:54:20 UTC
Comment on attachment 29501 [details]
Proposed patch

Grrr, bugzilla will turn me crazy...
Why does it change the bug you are working on by its own ????

The patch is related to PR53402
Comment 4 Spencer Rinehart 2013-10-17 14:21:18 UTC
I believe this is due to mod_dir.c line 366 (https://github.com/apache/httpd/blob/trunk/modules/mappers/mod_dir.c#L366), although I'm not positive.  I'd like to see this fixed, and am willing to contribute, but I'm unsure exactly what change needs to be made.

The comment says that the (r->finfo.filetype == APR_NOFILE) check means that there is nothing in the filesystem, but that doesn't seem to be the case for nonexistant php files when the request specifies the .php extension.

Is the issue that the filetype should be getting set to APR_NOFILE in this case but isn't?  Or is it the check that is incorrect: is checking for APR_NOFILE not the correct way to check that the file doesn't exist?
Comment 5 Richard George 2014-07-21 09:22:55 UTC
This (or a very similar) issue causes OSX clients ONLY to fail to connect to virtual hosts configured with any DirectoryIndex line together with a FallbackResource line, unless "DirectoryIndex disabled" is used.

This has been tested with the following clients:

OSX 10.9.4 Firefox 30.0                 - FAILS
OSX 10.9.4 Safari 7.0.5                 - FAILS
OSX 10.9.4 command-line telnet port 80  - FAILS

Ubuntu 14.04 links (command-line browser) - WORKS
Ubuntu 14.04 command-line telnet port 80  - WORKS
Windows 7 Firefox 30.0                    - WORKS

All working machines were VirtualBox VMs inside the OSX boxes. When the page loads, it loads instantly, when it fails, it never connects and eventually times out.

The following server config shows the problem. Server is Ubuntu 14.04 64 bit, Apache 2.4.7-1ubuntu4 on an AWS EC2 t2.small instance

<VirtualHost *:80>
        ServerName tdd-deciphered.com
        ServerAlias tddd.ec2.phase.org

        ServerAdmin webmaster@localhost

#  OSX clients cannot connect when these DirectoryIndex directives are missing
        DirectoryIndex disabled
        DirectoryIndex index.php

        DocumentRoot /home/wechsler/repos/tdd-deciphered/web/
        <Directory />
                Options +FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/wechsler/repos/tdd-deciphered/web>
#  OSX clients cannot connect when this directive is used
#               DirectoryIndex index.html index.php
                Options +Indexes +FollowSymLinks +MultiViews
                AllowOverride All
                Require all granted

                FallbackResource /index.php
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/tdd-error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/tdd-access.log combined
</VirtualHost>

This particular issue would appear to be a doozy of an edge case
Comment 6 Richard George 2014-07-21 09:31:42 UTC
FWIW the OSX problem only happens on *desktop* OSX, it was also tested on iOS devices where it worked fine, and on a Windows 7? laptop it also loaded fine.