Bug 59426

Summary: Problem with mod_rewrite, especially with ap_context_document_root()
Product: Apache httpd-2 Reporter: Daniel Betz <dbetz>
Component: mod_rewriteAssignee: Apache HTTPD Bugs Mailing List <bugs>
Status: NEW ---    
Severity: normal    
Priority: P2    
Version: 2.4.20   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Daniel Betz 2016-05-04 05:46:56 UTC
I have an easy rewrite Rule, which rewrites everything to /index.php, when the filename or directory doesnt exist.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


Sometimes and really random i become an bad request error 400 back.
After debugging a litte bit i found, that there were changes made with the "context docroot"

Here are the rewrite logs from an failed and from an working request:
FAILED:
rewrite '2014/tree/' -> '/index.php'
trying to replace prefix /www/471639_96450/gn2-hosting.de/host260161/host260155/danielbaer.eu/ with / trying to replace context docroot  with context prefix internal redirect with index.php [INTERNAL REDIRECT]

WORKING:
rewrite '2014/tree/' -> '/index.php'
trying to replace prefix /www/471639_96450/gn2-hosting.de/host260161/host260155/danielbaer.eu/ with / trying to replace context docroot \xa0/\xde\v\xd8/\xde\v\x901\xde\v with context prefix internal redirect with /index.php [INTERNAL REDIRECT]


It doesnt work, when when ap_context_document_root(r) is empty, but not NULL.
Sometimes it gives me strange docroot folders back like \xa0/\xde\v\xd8/\xde\v\x901\xde\v

In 2.4.19 ( i think ) a new option in mod_rewrite was added and i think there could be an error there:
            /* No base URL, or r->filename wasn't still under dconf->directory
             * or, r->filename wasn't still under the document root.
             * If there's a context document root AND a context prefix, and
             * the context document root is a prefix of r->filename, replace.
             * This allows a relative substitution on a path found by mod_userdir
             * or mod_alias without baking in a RewriteBase.
             */
            if (tmpfilename == r->filename &&
                !(dconf->options & OPTION_IGNORE_CONTEXT_INFO)) {
                if ((ccp = ap_context_document_root(r)) != NULL) {
                    const char *prefix = ap_context_prefix(r);
                    if (prefix != NULL) {
                        rewritelog((r, 2, dconf->directory, "trying to replace "
                                    "context docroot %s with context prefix %s",
                                    ccp, prefix));
                        r->filename = subst_prefix_path(r, r->filename,
                                ccp, prefix);
                    }
                }
            }

After adding "RewriteOptions IgnoreContextInfo" the random errors are gone.

The setup is a little bit "special". I have an modified mod_vhost_ldap and mod_fastcgi.
The Documentroot, SuexecUserGroup, PHP Version, etc. comes from mod_vhost_ldap and switches the FastCGI Socket for every domain and php version in the VirtualHost.

Yesterday it failed in an bad request about 240 times from 150.000 requests.
Not often, but often enough, so that our customers cry :)

Heres the relevant part of my httpd.conf. mod_http2 isnt enabled, but i can give it an try

<IfDefine FCGI>
  LoadModule fastcgi_module     mod_fastcgi.so
  LoadModule vhost_ldap_module  mod_vhost_ldap.so

  LDAPSharedCacheSize 2000000
  LDAPCacheEntries 4096
  LDAPCacheTTL 5
  LDAPOpCacheEntries 4096
  LDAPOpCacheTTL 5

  FastCgiExternalServer /etc/httpd/fastcgi/php-fcgi-starter -flush -socket /etc/httpd/fastcgi/php5-53LATEST -idle-timeout 3600
  Action php-fastcgi /php/php-fcgi-starter


</IfDefine>

<VirtualHost IP:80>
  ServerName server.hostname
  SuexecUserGroup domcgi nobody
  DocumentRoot /kunden/shadow/htdocs/

   ScriptAlias /php/ /etc/httpd/fastcgi/

    VhostLDAPEnabled on
    VhostLDAPUrl "ldap://localhost/sid=2542,sec=hosting,o=xxxxx,c=de"
    VhostLdapBindDN "cn=readonly,sid=2542,sec=hosting,o=xxxxx,c=de"
    VhostLDAPBindPassword "noone"
  </IfDefine>


</virtualhost>