Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes-entries/setenvif_expr_backref.txt
Original file line number Diff line number Diff line change
@@ -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]
8 changes: 8 additions & 0 deletions modules/metadata/mod_setenvif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/*
Expand Down
42 changes: 42 additions & 0 deletions test/modules/metadata/test_005_setenvif.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}=[<!--#echo var="{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
<Directory "{doc_dir}">
Options +Includes
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</Directory>
""",
})
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=[&lt;|&gt;]" in body, body
assert "SEI_ESC=[a&amp;b$1c]" in body, body
Loading