diff --git a/changes-entries/rewrite_expr_backref.txt b/changes-entries/rewrite_expr_backref.txt new file mode 100644 index 00000000000..5614071c638 --- /dev/null +++ b/changes-entries/rewrite_expr_backref.txt @@ -0,0 +1,4 @@ + *) mod_rewrite: Fix a crash, or stray bytes in the substitution, when a + %N backreference follows a "RewriteCond expr" that matched without + capturing anything. %N now expands to an empty string there. + PR 70369 [Arturo Bernal] diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c index d3ffd72f340..9bd61470389 100644 --- a/modules/mappers/mod_rewrite.c +++ b/modules/mappers/mod_rewrite.c @@ -4280,7 +4280,7 @@ static cond_return_type apply_rewrite_cond(rewritecond_entry *p, rewrite_ctx *ct case CONDPAT_AP_EXPR: { - const char *err, *source; + const char *err, *source = NULL; rc = ap_expr_exec_re(r, p->expr, AP_MAX_REG_MATCH, regmatch, &source, &err); if (rc < 0 || err) { diff --git a/test/modules/mappers/test_001_rewrite.py b/test/modules/mappers/test_001_rewrite.py index 05ca17b04a3..b6f4446a05f 100644 --- a/test/modules/mappers/test_001_rewrite.py +++ b/test/modules/mappers/test_001_rewrite.py @@ -141,3 +141,37 @@ def test_rewrite_002_02_escape_directs(self, env): r'.*:warn\].*', r'.*:trace\d\].*']) assert found, \ "Log should show 'escaping ... for redirect' for tab in target" + + +class TestRewriteExprBackref: + + @pytest.fixture(autouse=True, scope='class') + @classmethod + def _class_scope(cls, env): + conf = HttpdConf(env, extras={ + f"test1.{env.http_tld}": """ + RewriteEngine On + RewriteCond %{HTTP:X-Data} ^(.+)$ + RewriteRule ^/exprref-prime/ - [E=PRIME:%1] + RewriteCond %{HTTP_HOST} ^(.+)$ + RewriteCond expr "'a' == 'a'" + RewriteRule ^/exprref/(.*)$ /target/[%1] [R=302,L] + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + + # %1 after a "RewriteCond expr" that captured nothing expands to an + # empty string (BZ 70369) + def test_rewrite_003_01_expr_cond_no_capture(self, env): + prime = env.mkurl("http", "test1", "/exprref-prime/") + url = env.mkurl("http", "test1", "/exprref/abc") + for _ in range(10): + # leave stack data from an earlier request behind + env.curl_raw(prime, options=["-H", f"X-Data: {'Z' * 80}"]) + r = env.curl_raw(url, options=["--max-redirs", "0"]) + assert r.exit_code == 0, f"{r}" + assert r.response["status"] == 302 + loc = r.response["header"]["location"] + assert loc.endswith("/target/%5b%5d"), f"Location: {loc}"