diff --git a/changes-entries/setenvif_expr_backref.txt b/changes-entries/setenvif_expr_backref.txt new file mode 100644 index 00000000000..b415d99cd1b --- /dev/null +++ b/changes-entries/setenvif_expr_backref.txt @@ -0,0 +1,4 @@ + *) mod_setenvif: SetEnvIfExpr no longer fills $N backreferences from a + previous rule's match, or from uninitialized memory, when its + expression captured nothing. $N is then empty. + PR 70369 [Arturo Bernal] diff --git a/modules/metadata/mod_setenvif.c b/modules/metadata/mod_setenvif.c index aa1173d7a3e..ebe124b6669 100644 --- a/modules/metadata/mod_setenvif.c +++ b/modules/metadata/mod_setenvif.c @@ -608,6 +608,14 @@ static int match_headers(request_rec *r) * value again rather than reuse val and val_len. */ last_name = NULL; + /* $N in the value must come only from a capturing regex of + * this expression, never from a previous rule's match. + */ + val = ""; + val_len = 0; + for (j = 0; j < AP_MAX_REG_MATCH; ++j) { + regm[j].rm_so = regm[j].rm_eo = -1; + } } /* diff --git a/test/modules/metadata/test_005_setenvif.py b/test/modules/metadata/test_005_setenvif.py index 3cc55c476f9..4d4e11c36d8 100644 --- a/test/modules/metadata/test_005_setenvif.py +++ b/test/modules/metadata/test_005_setenvif.py @@ -48,3 +48,45 @@ def test_metadata_005_01_expr_between_header_rules(self, env): # X-A does not contain "bar", only X-B does assert "SEI_LITERAL=[(none)]" in body assert "SEI_REGEX=[(none)]" in body + + +class TestSetEnvIfExprNoCapture: + + @pytest.fixture(autouse=True, scope='class') + @classmethod + def _class_scope(cls, env): + doc_dir = os.path.join(env.server_dir, "htdocs", "test1") + os.makedirs(doc_dir, exist_ok=True) + with open(os.path.join(doc_dir, "setenvif-nocap.shtml"), "w") as f: + for name in ["SEI_NOCAP", "SEI_ESC"]: + f.write(f'{name}=[]\n') + # A SetEnvIfExpr whose expression captures nothing after a rule + # that did capture: $N must not come from the earlier rule + # (BZ 70369). + conf = HttpdConf(env, extras={ + 'base': f""" + SetEnvIf X-C "^(.+)$" SEI_PRIME=$1 + SetEnvIfExpr "'a' == 'a'" SEI_NOCAP=<$0|$1> + SetEnvIfExpr "'a' == 'a'" SEI_ESC=a\\&b\\$1c + + Options +Includes + AddType text/html .shtml + AddOutputFilter INCLUDES .shtml + + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + + # $N is empty and backslash escapes still apply + @pytest.mark.parametrize("header", [True, False]) + def test_metadata_005_02_expr_no_capture(self, env, header): + url = env.mkurl("http", "test1", "/setenvif-nocap.shtml") + options = ["-H", "X-C: secret-value"] if header else [] + r = env.curl_get(url, options=options) + assert r.response, "no response: server may have crashed" + assert r.response["status"] == 200 + body = r.response["body"].decode("utf-8", errors="replace") + assert "SEI_NOCAP=[<|>]" in body, body + assert "SEI_ESC=[a&b$1c]" in body, body