Skip to content

Send only valid Drupal aggregate names to PHP - #168

Merged
turegjorup merged 3 commits into
developfrom
feature/block-fake-asset-aggregates
Nov 30, 2026
Merged

turegjorup merged 3 commits into
developfrom
feature/block-fake-asset-aggregates

Conversation

@turegjorup

Copy link
Copy Markdown
Contributor

Make nginx return 404 for CSS and JS aggregate requests with a fake file name, so these requests do not reach Drupal.

Changes

  • templates/drupal/.docker/templates/default.conf.template (also used by drupal-10 and drupal-11 through symlinks):
    • Send a missing aggregate to Drupal only when its name has a valid hash: css_<43 characters>.css or js_<43 characters>.js.
    • Return 404 for all other missing files in sites/*/files/css/ and sites/*/files/js/.
    • nginx continues to serve existing aggregate files as static files.

Why

Since Drupal 10.1, Drupal builds a missing CSS or JS aggregate on request (AssetControllerBase). The include query parameter gives the list of libraries. If the hash in the file name is not correct, Drupal does not write the file to disk. It returns the aggregate with Cache-Control: private, no-store. Thus each request with a fake name starts Drupal and builds the aggregate again.

On dokk1.dk, a scanner sent approximately 7,800 requests in 3 hours to /sites/default/files/css/css_x.css?theme=claro&include=…. Together with the requests to the maintenance scripts that #167 blocks, this caused DatabaseConnectionRefusedException: SQLSTATE[HY000] [2002] Address not available errors, 500 responses and Uptime Kuma alerts.

The hash is Crypt::hmacBase64() of SHA-256, so a valid name has 43 base64url characters. This rule does not stop a request with a fake 43-character hash. It stops the simple flood. Use a rate limit in Traefik for full protection.

Tested on dokk1 with a copy of the production database:

  • Real CSS and JS aggregates: Drupal generates them (200), then nginx serves them as static files (200).
  • css_x.css: nginx returns 404, and the request does not reach PHP.
  • nginx -t passes for the template.

The drupal-8 and drupal-9 templates do not have this change. Drupal 8 and 9 do not build aggregates on request.

try_files $uri /index.php?$query_string;
}

location ~ ^/sites/[^/]+/files/(css|js)/ {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we make this case-insensitive and not anchored to the start of the path?

Drupal also sends these to the aggregate controller, so they still reach Drupal with the current rule:

  • /da/sites/default/files/css/css_x.css (language prefix is stripped before routing)
  • /sites/default/files/CSS/css_x.css (route matching ignores case)
Suggested change
location ~ ^/sites/[^/]+/files/(css|js)/ {
location ~* /sites/[^/]+/files/(css|js)/ {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in a435413. I applied the suggestion.

I also moved both aggregate blocks below the deny rules. Before, a file such as files/css/x.php or files/css/.htaccess matched the aggregate block first, and nginx sent it as plain text.

I tested the old and the new config in nginx/nginx-unprivileged:alpine:

Request Old New
/da/sites/default/files/css/css_x.css 502 (to Drupal) 404
/sites/default/files/CSS/css_x.css 502 (to Drupal) 404
/sites/default/files/css/x.php 200 403
/sites/default/files/css/.htaccess 200 403
Aggregate with a valid hash, not on disk 502 (to Drupal) 502 (to Drupal)
Aggregate with a valid hash, on disk 200 200

@turegjorup
turegjorup requested a review from jekuaitk November 30, 2026 12:56
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.

2 participants