Skip to content

Commit 7545008

Browse files
core: Log the malformed script header line, not the last output line (#1082)
ap_scan_script_header_err_core_ex() soaked up the rest of the output into the same buffer before logging the bad header, so the error named the last line read (often the body), not the malformed one. PR 69916
1 parent 480ad66 commit 7545008

4 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎changes-entries/69916.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*) core: Log the malformed header line sent by a CGI-style backend
2+
(SCGI, FastCGI), instead of the last line of its output.
3+
PR 69916 [Arturo Bernal]

‎server/util_script.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -624,17 +624,18 @@ AP_DECLARE(int) ap_scan_script_header_err_core_ex(request_rec *r, char *buffer,
624624
}
625625
#endif /*APR_CHARSET_EBCDIC*/
626626
if (!(l = strchr(w, ':'))) {
627+
/* Log before soaking up the output, which overwrites w */
628+
/* Intentional no APLOGNO */
629+
ap_log_rerror(SCRIPT_LOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
630+
"malformed header from script '%s': Bad header: %.30s",
631+
apr_filepath_name_get(r->filename), w);
632+
627633
if (!buffer) {
628634
/* Soak up all the script output - may save an outright kill */
629635
while ((*getsfunc)(w, MAX_STRING_LEN - 1, getsfunc_data) > 0) {
630636
continue;
631637
}
632638
}
633-
634-
/* Intentional no APLOGNO */
635-
ap_log_rerror(SCRIPT_LOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r,
636-
"malformed header from script '%s': Bad header: %.30s",
637-
apr_filepath_name_get(r->filename), w);
638639
return HTTP_INTERNAL_SERVER_ERROR;
639640
}
640641

‎test/modules/proxy/env.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def __init__(self, env: 'HttpdTestEnv'):
8484
self.add_modules(["proxy", "proxy_http", "proxy_ajp", "proxy_balancer",
8585
"proxy_connect", "proxy_uwsgi", "lbmethod_byrequests",
8686
"remoteip"])
87-
self.add_optional_modules(["proxy_hcheck"])
87+
self.add_optional_modules(["proxy_hcheck", "proxy_scgi"])
8888

8989

9090
class ProxyTestEnv(HttpdTestEnv):
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""A backend response line without a colon must be named in the error log,
2+
not the last line of the output read after it (PR 69916).
3+
"""
4+
5+
import re
6+
7+
import pytest
8+
9+
from pyhttpd.conf import HttpdConf
10+
from .env import TCPFaker
11+
12+
13+
class _ScgiFaker(TCPFaker):
14+
15+
@staticmethod
16+
def status_line(data):
17+
# an HTTP status line is not a valid CGI response header
18+
return (b"HTTP/1.1 200 OK\r\n"
19+
b"Content-Type: text/plain\r\n"
20+
b"\r\n"
21+
b"abcdefg")
22+
23+
24+
class TestProxyScgiBadHeader:
25+
26+
@pytest.fixture(scope='class')
27+
@classmethod
28+
def _class_scope(cls, env):
29+
env.require(env.has_shared_module("proxy_scgi"), "mod_proxy_scgi not available",
30+
dep="mod_proxy_scgi",
31+
detected=f"no mod_proxy_scgi.so in {env.libexec_dir}")
32+
faker = _ScgiFaker("127.0.0.1", env.http_port2)
33+
faker._make_response = _ScgiFaker.status_line
34+
faker.start()
35+
conf = HttpdConf(env)
36+
conf.start_vhost(domains=[f"test1.{env.http_tld}"], port=env.http_port)
37+
conf.add([
38+
f"ProxyPass / scgi://127.0.0.1:{env.http_port2}/",
39+
])
40+
conf.end_vhost()
41+
conf.install()
42+
assert env.apache_restart() == 0
43+
yield faker
44+
faker.stop()
45+
46+
# the error log names the malformed header line, not the response body
47+
def test_proxy_12_01(self, env, _class_scope):
48+
r = env.curl_get(env.mkurl("http", "test1", "/"))
49+
assert r.response["status"] == 500
50+
bad = r".*malformed header from script .*: Bad header: HTTP/1\.1 200 OK.*"
51+
body = r".*Bad header: abcdefg.*"
52+
try:
53+
found = env.httpd_error_log.scan_recent(re.compile(bad), timeout=2)
54+
except TimeoutError:
55+
found = False
56+
try:
57+
wrong = env.httpd_error_log.scan_recent(re.compile(body), timeout=0)
58+
except TimeoutError:
59+
wrong = False
60+
env.httpd_error_log.ignore_recent(
61+
lognos=["AH00860"], # error reading response headers
62+
matches=[r".*malformed header from script .*"])
63+
assert found
64+
assert not wrong

0 commit comments

Comments
 (0)