From 1022167478ba9f68caec9059cedc8b498f7a9118 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Tue, 3 Nov 2026 08:34:01 +0100 Subject: [PATCH] core: Fix "Options=" restriction ignoring Nonfatal=Override set_options() (server/core.c) rejects an .htaccess Options value outside what an "Options=" restriction in AllowOverride allows by returning a plain fatal error string directly from the directive handler, independently of the override-class check in invoke_cmd() that actually implements Nonfatal=Override -- that check only runs before the handler is invoked, to decide whether the "Options" directive is allowed in .htaccess at all. So e.g. with: Options +ExecCGI +Includes +SymLinksIfOwnerMatch -FollowSymLinks AllowOverride Nonfatal=Override Options=Includes an .htaccess with "Options +FollowSymLinks" (outside what "Options=Includes" permits) always failed the whole request with a 500, even though Nonfatal=Override was configured. Per the documented Nonfatal=Override semantics ("disallowed ... directives will be ignored") and the precedent in invoke_cmd(), a forbidden option anywhere in the line voids the complete directive, not just that token: save opts/opts_add/opts_remove on entry, keep scanning the rest of the line on a denial (so a genuine syntax error later in the same line is still fatal, as documented), and roll back at the end if any token was denied, leaving the directory's inherited options exactly as if the directive had never been seen. PR 69835 --- changes-entries/69835.txt | 7 + docs/log-message-tags/next-number | 2 +- server/core.c | 27 +++ .../core/test_013_allow_override_nonfatal.py | 215 ++++++++++++++++++ 4 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 changes-entries/69835.txt create mode 100644 test/modules/core/test_013_allow_override_nonfatal.py diff --git a/changes-entries/69835.txt b/changes-entries/69835.txt new file mode 100644 index 00000000000..e3856e0b8bd --- /dev/null +++ b/changes-entries/69835.txt @@ -0,0 +1,7 @@ + *) core: Fix "AllowOverride Nonfatal=Override" not applying to an + "Options=" restriction: an .htaccess Options value outside what + the restriction allows failed the whole request with a 500, even + though Nonfatal=Override was configured. Once ignored, such an + "Options" directive no longer affects the inherited options either. + PR 69835 [Arturo Bernal] + diff --git a/docs/log-message-tags/next-number b/docs/log-message-tags/next-number index 5d1df1abb36..0a29e80c5e8 100644 --- a/docs/log-message-tags/next-number +++ b/docs/log-message-tags/next-number @@ -1 +1 @@ -10687 +10703 diff --git a/server/core.c b/server/core.c index a6928ef916c..14356dd4201 100644 --- a/server/core.c +++ b/server/core.c @@ -2057,9 +2057,16 @@ static const char *set_options(cmd_parms *cmd, void *d_, const char *l) { core_dir_config *d = d_; allow_options_t opt; + /* Saved so a prohibited option anywhere in the line can undo + * whatever earlier tokens already applied; see "denied" below. + */ + const allow_options_t saved_opts = d->opts; + const allow_options_t saved_opts_add = d->opts_add; + const allow_options_t saved_opts_remove = d->opts_remove; int first = 1; int merge = 0; int all_none = 0; + int denied = 0; char action; while (l[0]) { @@ -2129,6 +2136,15 @@ static const char *set_options(cmd_parms *cmd, void *d_, const char *l) } if ( (cmd->override_opts & opt) != opt ) { + if (cmd->override & NONFATAL_OVERRIDE) { + ap_log_perror(APLOG_MARK, APLOG_WARNING, 0, cmd->temp_pool, + APLOGNO(10699) + "Option %s in .htaccess forbidden by " + "AllowOverride, Options ignored", w); + denied = 1; + first = 0; + continue; + } return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL); } else if (action == '-') { @@ -2149,6 +2165,17 @@ static const char *set_options(cmd_parms *cmd, void *d_, const char *l) first = 0; } + /* A prohibited option anywhere in the line means the complete + * directive is ignored, not just that one token: undo whatever + * earlier tokens on the same line already applied, leaving d exactly + * as if this Options directive had never been seen. + */ + if (denied) { + d->opts = saved_opts; + d->opts_add = saved_opts_add; + d->opts_remove = saved_opts_remove; + } + return NULL; } diff --git a/test/modules/core/test_013_allow_override_nonfatal.py b/test/modules/core/test_013_allow_override_nonfatal.py new file mode 100644 index 00000000000..a2da8c94642 --- /dev/null +++ b/test/modules/core/test_013_allow_override_nonfatal.py @@ -0,0 +1,215 @@ +import os + +import pytest + +from pyhttpd.conf import HttpdConf + +# set_options() enforces AllowOverride's per-option "Options=X" restriction +# itself, independently of the override-class check in invoke_cmd() that +# implements Nonfatal=Override. An .htaccess option outside that per-option +# allowance used to always fail the whole request with a 500, even with +# Nonfatal=Override configured (BZ 69835). +AH_OPTION_IGNORED = "AH10699" + + +class TestAllowOverrideNonfatal: + + @pytest.fixture(autouse=True, scope="class") + def _class_scope(self, env): + doc_dir = os.path.join(env.server_docs_dir, "allowoverride") + os.makedirs(doc_dir, exist_ok=True) + with open(os.path.join(doc_dir, "index.html"), "w") as f: + f.write("ok\n") + with open(os.path.join(doc_dir, ".htaccess"), "w") as f: + f.write("Options +FollowSymLinks\n") + conf = HttpdConf(env, extras={ + "base": f""" + + Options +ExecCGI +Includes +SymLinksIfOwnerMatch -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + + """, + }) + conf.install() + assert env.apache_restart() == 0 + env.httpd_error_log.add_ignored_lognos([AH_OPTION_IGNORED]) + + # FollowSymLinks is outside what "Options=Includes" permits, so the + # .htaccess attempt to enable it must be rejected and ignored -- not + # turn the whole request into a 500 -- since the directory also sets + # Nonfatal=Override. + def test_core_013_01(self, env): + env.httpd_error_log.clear_log() + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride/index.html")) + assert r.response["status"] == 200 + with open(env.httpd_error_log.path) as fd: + log = fd.read() + assert AH_OPTION_IGNORED in log + assert "not allowed here" not in log + + +# A rejected "Options" directive must leave the inherited option set +# untouched, not just skip applying the one forbidden token while still +# mutating/replacing the rest of the effective state (BZ 69835 follow-up). +# Both absolute ("Options X") and merge ("Options +X") syntax are covered, +# using SSI processing (Options Includes) as the observable effect: if +# Includes ends up disabled for the directory, mod_include passes the +# "#echo" tag through unprocessed instead of evaluating it. +SSI_MARKER = '' +AH_INCLUDES_NOT_SET = "AH01374" + + +def _write_ssi_dir(env, name): + doc_dir = os.path.join(env.server_docs_dir, name) + os.makedirs(doc_dir, exist_ok=True) + with open(os.path.join(doc_dir, "index.shtml"), "w") as f: + f.write(SSI_MARKER + "\n") + return doc_dir + + +class TestAllowOverrideNonfatalAbsoluteState: + + @pytest.fixture(autouse=True, scope="class") + def _class_scope(self, env): + single_dir = _write_ssi_dir(env, "allowoverride_abs_single") + with open(os.path.join(single_dir, ".htaccess"), "w") as f: + f.write("Options FollowSymLinks\n") + mixed_dir = _write_ssi_dir(env, "allowoverride_abs_mixed") + with open(os.path.join(mixed_dir, ".htaccess"), "w") as f: + f.write("Options FollowSymLinks Includes\n") + conf = HttpdConf(env, extras={ + "base": f""" + + Options +ExecCGI +Includes -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + AddType text/html .shtml + AddOutputFilter INCLUDES .shtml + + + Options +ExecCGI +Includes -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + AddType text/html .shtml + AddOutputFilter INCLUDES .shtml + + """, + }) + conf.install() + assert env.apache_restart() == 0 + env.httpd_error_log.add_ignored_lognos([AH_OPTION_IGNORED, AH_INCLUDES_NOT_SET]) + + # "Options FollowSymLinks" (absolute syntax): FollowSymLinks is outside + # "Options=Includes", so under Nonfatal=Override the directive must be + # ignored entirely, not reset the directory's options to None. + def test_core_013_02(self, env): + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride_abs_single/index.shtml")) + assert r.response["status"] == 200 + body = r.response["body"].decode("utf-8") + assert SSI_MARKER not in body + + # "Options FollowSymLinks Includes": one listed option (Includes) is + # permitted, but the directive as a whole still contains a forbidden + # token, so the whole directive -- not just the bad token -- must be + # ignored, leaving the inherited Includes in place. + def test_core_013_03(self, env): + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride_abs_mixed/index.shtml")) + assert r.response["status"] == 200 + body = r.response["body"].decode("utf-8") + assert SSI_MARKER not in body + + +class TestAllowOverrideNonfatalMergeState: + + @pytest.fixture(autouse=True, scope="class") + def _class_scope(self, env): + mixed_dir = _write_ssi_dir(env, "allowoverride_merge_mixed") + with open(os.path.join(mixed_dir, ".htaccess"), "w") as f: + f.write("Options +Includes +FollowSymLinks\n") + forbidden_dir = _write_ssi_dir(env, "allowoverride_merge_forbidden") + with open(os.path.join(forbidden_dir, ".htaccess"), "w") as f: + f.write("Options +FollowSymLinks\n") + allowed_dir = _write_ssi_dir(env, "allowoverride_merge_allowed") + with open(os.path.join(allowed_dir, ".htaccess"), "w") as f: + f.write("Options +Includes\n") + conf = HttpdConf(env, extras={ + "base": f""" + + Options +ExecCGI -Includes -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + AddType text/html .shtml + AddOutputFilter INCLUDES .shtml + + + Options +ExecCGI -Includes -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + AddType text/html .shtml + AddOutputFilter INCLUDES .shtml + + + Options +ExecCGI -Includes -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + AddType text/html .shtml + AddOutputFilter INCLUDES .shtml + + """, + }) + conf.install() + assert env.apache_restart() == 0 + env.httpd_error_log.add_ignored_lognos([AH_OPTION_IGNORED, AH_INCLUDES_NOT_SET]) + + # "Options +Includes +FollowSymLinks": Includes alone would be allowed, + # but FollowSymLinks is forbidden, so the complete directive must be + # ignored -- Includes must NOT leak in by applying only the allowed + # token while silently dropping the forbidden one. + def test_core_013_04(self, env): + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride_merge_mixed/index.shtml")) + assert r.response["status"] == 200 + body = r.response["body"].decode("utf-8") + assert SSI_MARKER in body + + # "Options +FollowSymLinks": the only token is forbidden, directive is + # ignored, inherited (disabled) Includes is unaffected. + def test_core_013_05(self, env): + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride_merge_forbidden/index.shtml")) + assert r.response["status"] == 200 + body = r.response["body"].decode("utf-8") + assert SSI_MARKER in body + + # "Options +Includes": the only token is permitted, so normal merge + # behavior is unaffected by the Nonfatal=Override handling and Includes + # is actually enabled. + def test_core_013_06(self, env): + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride_merge_allowed/index.shtml")) + assert r.response["status"] == 200 + body = r.response["body"].decode("utf-8") + assert SSI_MARKER not in body + + +class TestAllowOverrideNonfatalSyntaxError: + + @pytest.fixture(autouse=True, scope="class") + def _class_scope(self, env): + doc_dir = os.path.join(env.server_docs_dir, "allowoverride_syntax_error") + os.makedirs(doc_dir, exist_ok=True) + with open(os.path.join(doc_dir, "index.html"), "w") as f: + f.write("ok\n") + with open(os.path.join(doc_dir, ".htaccess"), "w") as f: + f.write("Options +FollowSymLinks +BogusOption\n") + conf = HttpdConf(env, extras={ + "base": f""" + + Options +ExecCGI -Includes -FollowSymLinks + AllowOverride Nonfatal=Override Options=Includes + + """, + }) + conf.install() + assert env.apache_restart() == 0 + env.httpd_error_log.add_ignored_lognos([AH_OPTION_IGNORED]) + + # A Nonfatal=Override-forbidden token earlier in the line must not + # cause a genuine syntax error later in the same directive to be + # skipped: "BogusOption" is not a real option, so this must still be + # a 500, exactly as without Nonfatal=Override. + def test_core_013_07(self, env): + r = env.curl_get(env.mkurl("http", "htdocs", "/allowoverride_syntax_error/index.html")) + assert r.response["status"] == 500