Generated with the help of an LLM.

Problem/Motivation

Drupal's default .htaccess includes a rewrite rule to block access to hidden files and directories:

RewriteRule "/\.|^\.(?!well-known/)" - [F]

Although the [F] flag generates a 403 Forbidden response, requests can ultimately return a Drupal-generated 404 page when Apache is configured to use a custom 403 error document that does not exist.

We discovered this behavior while investigating a production server outage this week. An automated vulnerability scanner sent 125 requests in approximately 20 seconds to nonexistent .env paths on our Drupal site, causing PHP processes to accumulate until the server exhausted its available memory and became unresponsive. The same scanner had sent approximately 3,000 similar requests to other Drupal websites on the server without causing an outage, as those sites were using PHP-FPM rather than CGI. During our investigation, we discovered that requests such as /monitoring/.env were returning Drupal-generated 404 pages despite the existing [F] rewrite rule. We reproduced this behavior on both Drupal 7 and Drupal 11. Further investigation revealed that Apache was configured to use a /403.shtml error page that did not exist. Creating the missing 403 error page, or explicitly defining ErrorDocument 403 in the site's .htaccess, resolved the behavior and allowed Apache to return 403 without invoking Drupal.

For example, a WHM/cPanel server may define:

ErrorDocument 403 /403.shtml

If that file is missing, the resulting error can be handled by Drupal's existing directive:

ErrorDocument 404 /index.php

This behavior was reproduced on both Drupal 7 and Drupal 11. Requests that should be rejected by Apache instead invoke Drupal and PHP, potentially increasing server load during automated vulnerability scanning.

Steps to reproduce

  1. Configure Apache with ErrorDocument 403 /403.shtml.
  2. Ensure that /403.shtml does not exist in the site's document root.
  3. Use Drupal's default .htaccess configuration.
  4. Request a nonexistent hidden file, such as /testing/.env.
  5. Observe that Drupal renders a 404 page rather than Apache returning a 403.
  6. Create the missing 403 error page, or add ErrorDocument 403 default to Drupal's .htaccess.
  7. Repeat the request and observe that Apache returns 403 without invoking Drupal.

Proposed resolution

Consider explicitly defining the default Apache 403 error handler in Drupal's .htaccess:

ErrorDocument 403 default
ErrorDocument 404 /index.php

This would prevent inherited custom error-document configurations from unintentionally routing forbidden requests through Drupal.

Alternatively, document the need for correctly configured Apache 403 error pages. An explicit override would need to account for sites intentionally using custom 403 pages.

Remaining tasks

  • Confirm the underlying Apache error-document processing behavior.
  • Determine whether an explicit ErrorDocument 403 default belongs in Drupal's default .htaccess.
  • Consider compatibility with custom server-level 403 error pages.
  • Add appropriate documentation or test coverage.

User interface changes

None.

Introduced terminology

None.

API changes

None.

Data model changes

None.

Release notes snippet

Not yet determined.

Comments

wlofgren created an issue.