From 6cdf1cf2883de6ca504b105d13932d6506e501fe Mon Sep 17 00:00:00 2001 From: ylavic Date: Fri, 27 Nov 2026 16:04:05 +0100 Subject: [PATCH] mod_proxy_balancer: ignore b_nwrkr_flags from the balancer-manager A worker added through the balancer-manager is now created disabled with no other status flag: b_nwrkr_flags is dropped from its requests. balancer_manage()'s callers can still create a worker marked 'B' (mod_proxy_beacon), but an add asking for any other flag is refused. * modules/proxy/mod_proxy_balancer.c (balancer_handler): Drop b_nwrkr_flags from the request's parameters. (balancer_process_balancer_worker): Refuse an add with a status flag other than PROXY_WORKER_BEACON_FLAG. * test/modules/proxy/test_12_balancer.py: Check that a worker added with b_nwrkr_flags through the balancer-manager is created without them. --- modules/proxy/mod_proxy_balancer.c | 29 ++++++++++++- test/modules/proxy/test_12_balancer.py | 57 +++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/modules/proxy/mod_proxy_balancer.c b/modules/proxy/mod_proxy_balancer.c index 5d706b6adc9..4191765aa5b 100644 --- a/modules/proxy/mod_proxy_balancer.c +++ b/modules/proxy/mod_proxy_balancer.c @@ -1448,8 +1448,28 @@ static int balancer_process_balancer_worker(request_rec *r, proxy_server_conf *c if ((val = apr_table_get(params, "b_wyes")) && (*val == '1' && *(val+1) == '\0') && (val = apr_table_get(params, "b_nwrkr"))) { - int rc = balancer_add_worker(r, bsel, conf, val, - apr_table_get(params, "b_nwrkr_flags")); + const char *flags = apr_table_get(params, "b_nwrkr_flags"); + int rc; + + /* mod_proxy_beacon's mark is the only status flag that a worker + * can be created with (balancer_handler() drops b_nwrkr_flags + * from the balancer-manager's requests). + */ + if (flags) { + const char *f; + for (f = flags; *f; ++f) { + if (*f != PROXY_WORKER_BEACON_FLAG) { + ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10704) + "%s: refusing to add worker %s with " + "status flags \"%s\" (only '%c' is " + "allowed)", bsel->s->name, val, flags, + PROXY_WORKER_BEACON_FLAG); + return HTTP_BAD_REQUEST; + } + } + } + + rc = balancer_add_worker(r, bsel, conf, val, flags); if (rc != APR_SUCCESS) { return rc; } @@ -2050,6 +2070,11 @@ static int balancer_handler(request_rec *r) apr_brigade_flatten(ib, buf, &len); buf[len] = '\0'; push2table(buf, params, NULL, r->pool); + + /* The status flags of a new worker are for balancer_manage()'s + * callers only: the balancer-manager creates it disabled. + */ + apr_table_unset(params, "b_nwrkr_flags"); } /* Ignore parameters if this looks like XSRF */ diff --git a/test/modules/proxy/test_12_balancer.py b/test/modules/proxy/test_12_balancer.py index 4e0c7f1bb5f..35ff2fd6c31 100644 --- a/test/modules/proxy/test_12_balancer.py +++ b/test/modules/proxy/test_12_balancer.py @@ -358,6 +358,18 @@ def page_workers(self): assert m, f"balancer://{self.balancer} not listed on {self.domain}" return re.findall(r"\s*([^<]*)", m.group(1)) + def page_statuses(self): + """{name: status} of the balancer's workers on the XML page.""" + status, _, page = self.request("GET", "/balancer-manager?xml=1") + assert status == 200 + m = re.search(rf"balancer://{self.balancer}" + r"(.*?)", page, re.S) + assert m, f"balancer://{self.balancer} not listed on {self.domain}" + return dict(re.findall(r"\s*([^<]*)" + r".*?" + r"([^<]*)", + m.group(1), re.S)) + def status_workers(self): status, _, page = self.request("GET", "/server-status?auto") assert status == 200 @@ -382,13 +394,15 @@ def check_walks(self, members): loc = self.reverse_map("miss") assert loc == "http://127.0.0.2:1/x", f"/rmap/miss on {self.domain}: {loc}" - def add_worker(self, url): + def add_worker(self, url, flags=None): _, _, page = self.request("GET", "/balancer-manager") m = re.search(rf"b={self.balancer}&[^\"]*?nonce=([0-9a-fA-F-]+)", page.replace("&", "&")) assert m, f"no nonce for balancer://{self.balancer} on {self.domain}" body = (f"b={self.balancer}&b_wyes=1&nonce={m.group(1)}&b_nwrkr=" + urllib.parse.quote(url, safe="")) + if flags is not None: + body += "&b_nwrkr_flags=" + urllib.parse.quote(flags, safe="") status, _, _ = self.request("POST", "/balancer-manager", body.encode()) return status @@ -620,3 +634,44 @@ def test_proxy_12_08_persist_growth_kept(self, env): assert sorted(c.status_workers()) == added finally: c.close() + + +class TestBalancerAddFlags: + """b_nwrkr_flags in a balancer-manager request: the status flags that a + worker can be created with are for balancer_manage()'s callers + (mod_proxy_beacon), the balancer-manager ignores them and creates the + worker disabled, without them.""" + + @pytest.fixture(autouse=True, scope='class') + @classmethod + def _class_scope(cls, env): + conf = HttpdConf(env) + conf.start_vhost(domains=[_fixed_domain(env, 0)], port=env.http_port) + conf.add(["", + f" BalancerMember {_member_url(env, 'app0')}", + " ProxySet growth=4", + "", + "ProxyPass /flags balancer://flags", + "", + " SetHandler balancer-manager", + " Require all granted", + ""]) + conf.end_vhost() + conf.install() + assert env.apache_restart() == 0 + + # A URL that starts with a worker's URL matches that worker, so no name + # here is the prefix of another. + @pytest.mark.parametrize("name, flags", [("init", "O"), ("beacon", "B"), + ("mixed", "OBHS")]) + def test_proxy_12_09_add_flags_ignored(self, env, name, flags): + url = _member_url(env, name) + c = Client(env, 0, "flags") + try: + assert c.add_worker(url, flags) == 200 + status = c.page_statuses().get(url) + finally: + c.close() + assert status is not None, f"{url} not added" + assert status.split() == ["Init", "Dis"], \ + f"{url} added with b_nwrkr_flags={flags}: status {status!r}"