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
3 changes: 3 additions & 0 deletions changes-entries/proxy-html-metafix-bounds.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_proxy_html: Fix out-of-bounds reads in metafix() when a
<meta http-equiv> tag appears at the end of the response buffer with
ProxyHTMLMeta enabled. [Arshiya Tabasum]
21 changes: 11 additions & 10 deletions modules/filters/mod_proxy_html.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ static meta *metafix(request_rec *r, const char *buf, apr_size_t len)
size_t offs = 0;
const char *p;
const char *q;
const char *end = buf + len;
char *header;
char *content;
ap_regmatch_t pmatch[2];
Expand All @@ -699,8 +700,8 @@ static meta *metafix(request_rec *r, const char *buf, apr_size_t len)
header = NULL;
content = NULL;
p = buf+offs+pmatch[1].rm_eo;
while (!apr_isalpha(*++p));
for (q = p; apr_isalnum(*q) || (*q == '-'); ++q);
while (++p < end && !apr_isalpha(*p));
for (q = p; q < end && (apr_isalnum(*q) || (*q == '-')); ++q);
header = apr_pstrmemdup(r->pool, p, q-p);
if (!ap_cstr_casecmpn(header, "Content-Type", 12)) {
ret = apr_palloc(r->pool, sizeof(meta));
Expand All @@ -713,22 +714,22 @@ static meta *metafix(request_rec *r, const char *buf, apr_size_t len)
pmatch[0].rm_eo - pmatch[0].rm_so);
/* if it doesn't contain "content", ignore, don't crash! */
if (p != NULL) {
while (*p) {
while (p < end && *p) {
p += 7;
while (apr_isspace(*p))
while (p < end && apr_isspace(*p))
++p;
/* XXX Should we search for another content= pattern? */
if (*p != '=')
if (p >= end || *p != '=')
break;
while (*p && apr_isspace(*++p));
if ((*p == '\'') || (*p == '"')) {
while (++p < end && apr_isspace(*p));
if (p < end && ((*p == '\'') || (*p == '"'))) {
delim = *p++;
for (q = p; *q && *q != delim; ++q);
for (q = p; q < end && *q != delim; ++q);
/* No terminating delimiter found? Skip the bogus directive */
if (*q != delim)
if (q >= end || *q != delim)
break;
} else {
for (q = p; *q && !apr_isspace(*q) && (*q != '>'); ++q);
for (q = p; q < end && !apr_isspace(*q) && (*q != '>'); ++q);
}
content = apr_pstrmemdup(r->pool, p, q-p);
break;
Expand Down
1 change: 1 addition & 0 deletions test/modules/filters/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, env: 'HttpdTestEnv'):
self.add_source_dir(os.path.dirname(inspect.getfile(FiltersTestSetup)))
self.add_modules(["substitute", "sed"])
self.add_cgi_module()
self.add_optional_modules(["xml2enc", "proxy_html"])


class FiltersTestEnv(HttpdTestEnv):
Expand Down
81 changes: 81 additions & 0 deletions test/modules/filters/test_004_proxy_html_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os

import pytest

from pyhttpd.conf import HttpdConf

from .env import FiltersTestEnv

# ProxyHTMLMeta lifts <meta http-equiv> tags into response headers. The
# scan for the header name and the content value must stop at the end of
# the data read from the backend, which is not NUL terminated, so a tag
# right at the end of the body must not be read past.
#
# The documents are 4096 bytes, ending in the tag, so that an over-read on
# an unfixed server is more likely to run off the end of the mapped file.
# Without a guard page or ASan it may still go unnoticed.
DOC_SIZE = 4096
HEAD = "<html><head><title>t</title>"

WELL_FORMED = (HEAD + '<meta http-equiv="X-Meta-Test" content="lifted">'
+ "</head><body>ok</body></html>")
END_TAGS = {
"no_name": "<meta http-equiv>",
"open_content": "<meta content='0;url=/x http-equiv=Refresh>",
}


def pad_to(text, size):
return HEAD + " " * (size - len(HEAD) - len(text)) + text


@pytest.mark.skipif(not FiltersTestEnv.has_shared_module("proxy_html"),
reason="mod_proxy_html not available")
class TestProxyHtmlMeta:

@pytest.fixture(autouse=True, scope='class')
def _class_scope(self, env):
doc_dir = os.path.join(env.server_dir, "htdocs", "test1", "phtml")
os.makedirs(doc_dir, exist_ok=True)
with open(os.path.join(doc_dir, "well_formed.html"), "w") as f:
f.write(WELL_FORMED)
for name, tag in END_TAGS.items():
text = pad_to(tag, DOC_SIZE)
assert len(text) == DOC_SIZE
with open(os.path.join(doc_dir, f"{name}.html"), "w") as f:
f.write(text)
# "No i18n support found" when mod_xml2enc is not loaded.
env.httpd_error_log.add_ignored_lognos(["AH01422"])
conf = HttpdConf(env, extras={
f"test1.{env.http_tld}": """
<Location "/phtml/">
SetEnv PROXY_HTML_FORCE 1
ProxyHTMLEnable On
ProxyHTMLMeta On
ProxyHTMLLinks a href
</Location>
""",
})
conf.add_vhost_test1()
conf.install()
assert env.apache_restart() == 0

def get(self, env, name):
return env.curl_get(env.mkurl("http", "test1", f"/phtml/{name}.html"))

# A well-formed tag is still lifted into a response header.
def test_filters_004_01(self, env):
r = self.get(env, "well_formed")
assert r.response, "no response"
assert r.response["status"] == 200
assert r.response["header"].get("x-meta-test") == "lifted"

# A tag at the very end of the body is served without a crash and,
# having no usable header name or content value, lifts no header.
@pytest.mark.parametrize("name", list(END_TAGS))
def test_filters_004_02(self, env, name):
r = self.get(env, name)
assert r.response, f"{name}: no response"
assert r.response["status"] == 200, \
f"{name}: status {r.response['status']}"
assert "refresh" not in r.response["header"]