You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by jo...@apache.org on 2026/10/08 20:04:38 UTC

svn commit: r1938927 - in httpd/httpd/trunk: changes-entries modules/metadata test/modules/metadata

Author: jorton
Date: Thu Oct  8 20:04:38 2026
New Revision: 1938927

Log:
mod_remoteip: PROXY protocol: handle a peer going away before the header,
and test the LOCAL command.

* modules/metadata/mod_remoteip.c (remoteip_input_filter): Log EOF while
  reading the PROXY protocol header at info level, as the peer going away;
  other read errors are still logged as errors.  Skip metadata buckets
  rather than copying from them: the EOS bucket arriving before the EOF
  was passed to memcpy() as a NULL source.

* test/modules/metadata/test_004_remoteip.py (test_metadata_004_03,
  test_metadata_004_04, test_metadata_004_05, test_metadata_004_06): Test
  PROXY protocol v2 with the PROXY and LOCAL commands, with and without an
  address block, and a peer disconnecting after a partial header.

PR: 63893
Co-Authored-By: Claude Fable 5.1 <no...@anthropic.com>
GitHub: closes #805

Added:
   httpd/httpd/trunk/changes-entries/remoteip-pp-eof.txt
Modified:
   httpd/httpd/trunk/modules/metadata/mod_remoteip.c
   httpd/httpd/trunk/test/modules/metadata/test_004_remoteip.py

Added: httpd/httpd/trunk/changes-entries/remoteip-pp-eof.txt
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ httpd/httpd/trunk/changes-entries/remoteip-pp-eof.txt	Thu Oct  8 20:04:38 2026	(r1938927)
@@ -0,0 +1,4 @@
+  *) mod_remoteip: Log a connection closed before a complete PROXY protocol
+     header was received at info level rather than as an error, and fix an
+     undefined memcpy() with a NULL source in that case.  PR 63893.
+     [Joe Orton]

Modified: httpd/httpd/trunk/modules/metadata/mod_remoteip.c
==============================================================================
--- httpd/httpd/trunk/modules/metadata/mod_remoteip.c	Thu Oct  8 19:27:43 2026	(r1938926)
+++ httpd/httpd/trunk/modules/metadata/mod_remoteip.c	Thu Oct  8 20:04:38 2026	(r1938927)
@@ -1134,6 +1134,14 @@ static apr_status_t remoteip_input_filte
             apr_off_t got, want = ctx->need - ctx->rcvd;
 
             ret = ap_get_brigade(f->next, ctx->bb, ctx->mode, block, want);
+            if (APR_STATUS_IS_EOF(ret)) {
+                /* The peer went away before sending a whole header, as a
+                 * health check may. */
+                ap_log_cerror(APLOG_MARK, APLOG_INFO, ret, f->c, APLOGNO()
+                              "RemoteIPProxyProtocol: connection closed "
+                              "before a complete header was received");
+                return ret;
+            }
             if (ret != APR_SUCCESS) {
                 ap_log_cerror(APLOG_MARK, APLOG_ERR, ret, f->c, APLOGNO(10184)
                               "failed reading input");
@@ -1157,6 +1165,13 @@ static apr_status_t remoteip_input_filte
         while (!ctx->done && !APR_BRIGADE_EMPTY(ctx->bb)) {
             b = APR_BRIGADE_FIRST(ctx->bb);
 
+            if (APR_BUCKET_IS_METADATA(b)) {
+                /* Nothing to copy from EOS or FLUSH; EOF is reported by
+                 * the next read. */
+                apr_bucket_delete(b);
+                continue;
+            }
+
             ret = apr_bucket_read(b, &ptr, &len, block);
             if (APR_STATUS_IS_EAGAIN(ret) && block == APR_NONBLOCK_READ) {
                 return APR_SUCCESS;

Modified: httpd/httpd/trunk/test/modules/metadata/test_004_remoteip.py
==============================================================================
--- httpd/httpd/trunk/test/modules/metadata/test_004_remoteip.py	Thu Oct  8 19:27:43 2026	(r1938926)
+++ httpd/httpd/trunk/test/modules/metadata/test_004_remoteip.py	Thu Oct  8 20:04:38 2026	(r1938927)
@@ -1,5 +1,7 @@
 import os
 import re
+import socket
+import time
 
 import pytest
 
@@ -115,3 +117,100 @@ class TestRemoteIp:
         assert r.response["status"] == 200
         with open(log_path) as f:
             assert f.read().strip() == f"{self.CLIENT} {proxies}"
+
+    # -- PROXY protocol --------------------------------------------------
+
+    # A v2 header: the signature, then version/command, family/transport,
+    # the length of what follows, and the addresses.  The commands are LOCAL
+    # (0) and PROXY (1); a health checker such as an AWS NLB sends LOCAL,
+    # for which the receiver must keep the connection's own address and
+    # ignore any address information present (bug 63893).
+    PP2_SIG = b"\x0d\x0a\x0d\x0a\x00\x0d\x0a\x51\x55\x49\x54\x0a"
+    PP2_LOCAL = 0x20
+    PP2_PROXY = 0x21
+    PP2_UNSPEC = 0x00
+    PP2_TCP4 = 0x11
+    # CLIENT:4321 -> CDN:80, as a PROXY protocol TCPv4 address block
+    PP2_ADDRS = (socket.inet_aton(CLIENT) + socket.inet_aton(CDN)
+                 + (4321).to_bytes(2, "big") + (80).to_bytes(2, "big"))
+
+    @classmethod
+    def pp2_header(cls, cmd, fam=0x00, addrs=b""):
+        return cls.PP2_SIG + bytes([cmd, fam]) + len(addrs).to_bytes(2, "big") + addrs
+
+    def pp_install(self, env):
+        """PROXY protocol on its own port, so the harness's own requests
+        on the usual ports are unaffected; log the client address seen."""
+        conf = HttpdConf(env, extras={
+            "base": [
+                f"Listen {env.http_port2}",
+                f'CustomLog logs/{self.LOG_FILE} "%a"',
+            ]
+        })
+        conf.start_vhost(domains=[f"test1.{env.http_tld}"], port=env.http_port2,
+                         doc_root="htdocs/test1")
+        conf.add(["RemoteIPProxyProtocol On"])
+        conf.end_vhost()
+        conf.install()
+        assert env.apache_restart() == 0
+
+    def pp_request(self, env, header, request=True):
+        """Send a PROXY protocol header, then a request unless told not to;
+        return (status, client address logged) - None for each if nothing
+        came back."""
+        log_path = os.path.join(env.server_logs_dir, self.LOG_FILE)
+        open(log_path, 'w').close()
+        sock = socket.create_connection(("127.0.0.1", env.http_port2), timeout=5)
+        sock.sendall(header)
+        if request:
+            sock.sendall(f"GET / HTTP/1.1\r\nHost: test1.{env.http_tld}\r\n"
+                         f"Connection: close\r\n\r\n".encode())
+        data = b""
+        try:
+            while True:
+                chunk = sock.recv(65536)
+                if not chunk:
+                    break
+                data += chunk
+        except (socket.timeout, ConnectionError):
+            pass
+        sock.close()
+        status = int(data.split(b" ", 2)[1]) if data.startswith(b"HTTP/") else None
+        logged = None
+        for _ in range(20):
+            with open(log_path) as f:
+                logged = f.read().strip() or None
+            if logged or status is None:
+                break
+            time.sleep(0.1)
+        return status, logged
+
+    # Control: a PROXY command is honoured, the client is the one it names.
+    def test_metadata_004_03(self, env):
+        self.pp_install(env)
+        hdr = self.pp2_header(self.PP2_PROXY, self.PP2_TCP4, self.PP2_ADDRS)
+        assert self.pp_request(env, hdr) == (200, self.CLIENT)
+
+    # A LOCAL command (ver_cmd 0x20) is valid: the request is served as
+    # coming from the connection's own address, and nothing is logged as
+    # an error.  Bug 63893.
+    def test_metadata_004_04(self, env):
+        self.pp_install(env)
+        hdr = self.pp2_header(self.PP2_LOCAL, self.PP2_UNSPEC)
+        assert self.pp_request(env, hdr) == (200, self.PEER)
+
+    # LOCAL with an address block present: it must be ignored.
+    def test_metadata_004_05(self, env):
+        self.pp_install(env)
+        hdr = self.pp2_header(self.PP2_LOCAL, self.PP2_TCP4, self.PP2_ADDRS)
+        assert self.pp_request(env, hdr) == (200, self.PEER)
+
+    # A health check which connects, sends an incomplete header and goes
+    # away is not an error worth logging (bug 63893 comment 12).
+    def test_metadata_004_06(self, env):
+        self.pp_install(env)
+        pos = env.httpd_error_log.current_pos()
+        assert self.pp_request(env, self.PP2_SIG, request=False) == (None, None)
+        logged = env.httpd_error_log.wait_for(re.compile(r".*AH10184"), pos,
+                                              timeout=1)
+        assert not logged, "peer going away mid-header was logged as an error"