From f68819da02ce758d9c15a3725b93a0b86f30e157 Mon Sep 17 00:00:00 2001 From: Juan Denis Date: Sun, 22 Nov 2026 01:50:15 -0500 Subject: [PATCH] Let ACME challenges past the dotfile deny The shipped vhosts deny dotfiles with `location ~ /\.`, which also matches /.well-known/acme-challenge/. nginx prefers a matching regex location over a plain prefix one, so on the plain HTTP vhost (both in one server block) HTTP-01 answered 403 and issuance and renewal failed; a hand fix was undone by the next update re-copying the file. `^~` stops the regex pass. Part of #269. --- .../test_nginx_acme_challenge_location.py | 49 +++++++++++++++++++ nginx/nginx.conf | 2 +- nginx/sites-available/example.conf.template | 2 +- nginx/sites-available/serverkit-insecure.conf | 2 +- nginx/sites-available/serverkit.conf | 2 +- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 backend/tests/test_nginx_acme_challenge_location.py diff --git a/backend/tests/test_nginx_acme_challenge_location.py b/backend/tests/test_nginx_acme_challenge_location.py new file mode 100644 index 00000000..912b97a9 --- /dev/null +++ b/backend/tests/test_nginx_acme_challenge_location.py @@ -0,0 +1,49 @@ +"""The shipped nginx configs must let Let's Encrypt HTTP-01 through (issue #269). + +Each vhost denies dotfiles with the regex location ``location ~ /\\.``. That +regex also matches ``/.well-known/acme-challenge/``, and nginx prefers +a matching regex location over a plain prefix location — so a plain +``location /.well-known/acme-challenge/`` in the same server block loses and +the challenge answers 403. Issuance and renewal then fail, and a hand fix was +undone by the next ``serverkit update`` re-copying the shipped file. + +``^~`` on the prefix location tells nginx to stop there and skip the regex +pass, which is the only form that wins. +""" +import re +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[2] + +CONFIGS = [ + ROOT / 'nginx' / 'nginx.conf', + ROOT / 'nginx' / 'sites-available' / 'serverkit.conf', + ROOT / 'nginx' / 'sites-available' / 'serverkit-insecure.conf', + ROOT / 'nginx' / 'sites-available' / 'example.conf.template', +] + +_ACME = re.compile(r'^\s*location\s+(?P\S+\s+)?/\.well-known/acme-challenge/\s*\{', + re.MULTILINE) +_DOTFILE_DENY = re.compile(r'^\s*location\s+~\*?\s+/\\\.\s*\{', re.MULTILINE) + + +@pytest.mark.parametrize('config', CONFIGS, ids=lambda p: p.name) +def test_acme_challenge_location_beats_the_dotfile_deny(config): + text = config.read_text(encoding='utf-8') + locations = list(_ACME.finditer(text)) + assert locations, f'{config.name}: no ACME challenge location to serve HTTP-01' + for match in locations: + assert (match.group('modifier') or '').strip() == '^~', ( + f'{config.name}: the ACME challenge location must be ' + f'`location ^~ /.well-known/acme-challenge/` or the dotfile deny wins') + + +def test_the_collision_this_guards_is_real(): + """At least one shipped server block carries both locations — the plain + HTTP vhost — so the modifier is load-bearing, not decoration.""" + text = (ROOT / 'nginx' / 'sites-available' / 'serverkit-insecure.conf').read_text( + encoding='utf-8') + assert _ACME.search(text) and _DOTFILE_DENY.search(text) + assert text.count('server {') == 1 diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 66d0624e..f8a03ace 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -67,7 +67,7 @@ http { server_name _; # Allow Let's Encrypt verification - location /.well-known/acme-challenge/ { + location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; } diff --git a/nginx/sites-available/example.conf.template b/nginx/sites-available/example.conf.template index adf08076..bbf024ed 100644 --- a/nginx/sites-available/example.conf.template +++ b/nginx/sites-available/example.conf.template @@ -15,7 +15,7 @@ server { listen 80; server_name example.builditdesign.com; # Change this to your domain - location /.well-known/acme-challenge/ { + location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; } diff --git a/nginx/sites-available/serverkit-insecure.conf b/nginx/sites-available/serverkit-insecure.conf index c417aca0..16c41fc8 100644 --- a/nginx/sites-available/serverkit-insecure.conf +++ b/nginx/sites-available/serverkit-insecure.conf @@ -26,7 +26,7 @@ server { # scanners (Shodan/Censys) ignore this; real hiding is access control. add_header X-Robots-Tag "noindex, nofollow, noarchive" always; - location /.well-known/acme-challenge/ { + location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; } diff --git a/nginx/sites-available/serverkit.conf b/nginx/sites-available/serverkit.conf index eaeb561a..e30ff377 100644 --- a/nginx/sites-available/serverkit.conf +++ b/nginx/sites-available/serverkit.conf @@ -46,7 +46,7 @@ server { server_name _; # Allow Let's Encrypt validation over plain HTTP - location /.well-known/acme-challenge/ { + location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; }