Skip to content

Block web access to Drupal's maintenance scripts - #167

Merged
cableman merged 2 commits into
developfrom
feature/install_php
Nov 26, 2026
Merged

cableman merged 2 commits into
developfrom
feature/install_php

Conversation

@cableman

Copy link
Copy Markdown
Contributor

@cableman
cableman requested a review from turegjorup November 26, 2026 09:34
@cableman
cableman force-pushed the feature/install_php branch from 86bf63d to 2d6f802 Compare November 26, 2026 09:35

@turegjorup turegjorup left a comment

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.

Use ^~ prefix locations for the maintenance script blocks. Then the blocks do not depend on their position in the config.

Problem

#163 adds include /etc/nginx/conf.d/server.d/*.conf; at the top of the server block. nginx checks regex locations in config order and uses the first match. Thus, a regex location in a project server.d/ file comes before the regex block in this PR. If that location matches .php, it gets the request, and the block has no effect.

A server-level rewrite in server.d/ (the README example in #163) is safe. nginx selects the location after the rewrite. The problem is a project location ~ ... rule, for example a PHP-FPM block.

Test

I ran nginx:alpine with the config from this PR, the include from #163, and this file in server.d/:

location ~ \.php(/|$) {
    return 200 "PROJECT PHP $uri\n";
}
URL This PR (~ regex) With ^~ prefixes
/core/install.php 200 (project block) 404
/core/install.php/foo 200 (project block) 404
/update.php/selection 200 (project block) 404
/core/rebuild.php 200 (project block) 404
/index.php 200 200

Requested change

Replace the regex block with one prefix location for each script, in drupal, drupal-8 and drupal-9:

    # Block web access to Drupal's maintenance scripts (update, install, cache
    # rebuild, and authorize). Use drush through ssh for these tasks instead.
    # "^~" stops nginx from checking regex locations, so rules in server.d/
    # cannot override these blocks.
    location ^~ /update.php {
        return 404;
    }

    location ^~ /install.php {
        return 404;
    }

    location ^~ /core/install.php {
        return 404;
    }

    location ^~ /core/rebuild.php {
        return 404;
    }

    location ^~ /core/authorize.php {
        return 404;
    }
  • nginx selects prefix locations by the longest match, not by position. These blocks win against all regex locations in server.d/.
  • A prefix also matches /core/install.php/foo, so the (/|$) suffix is not necessary.
  • The Drupal.org guide Securing the installer recommends location ^~ /core/install.php for the same reason.

A project can still override a block with a longer ^~ prefix or an exact match (location = ...) in server.d/. Nobody writes that by accident, so I think that this risk is acceptable.

@cableman
cableman requested a review from turegjorup November 26, 2026 09:59
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