You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@axis.apache.org by ro...@apache.org on 2026/08/10 20:07:46 UTC

(axis-axis2-c-core) 03/04: Cover the chunked ceiling and IPv6 classification

This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-c-core.git

commit 99e4bc7cf937fcccb569e731fd44e7f0001d66af
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Mon Aug 10 09:12:15 2026 -1000

    Cover the chunked ceiling and IPv6 classification
    
    Twelve cases across the two fixes. Both suites were checked against the code
    without the fix: the chunked cases drop to two of four, and the IPv6 cases
    fail. A test that passes either way would be worse than none.
    
    The chunked body arrives over a socketpair, not a basic stream. That is not a
    detail: axutil_stream_read_basic returns count-1 bytes to reserve room for a
    NUL, so a one-byte read returns zero, and the chunk header parser reads exactly
    one byte at a time. Over a basic stream it sees nothing, decides the chunk size
    is zero and reports end of chunks, so every case would have passed while
    reading no data at all. This first draft did exactly that.
    
    The IPv6 suite opens the gate before asserting anything and has a control case
    that fails if it did not open, since with the defaults everything is refused
    and all the negative assertions would hold for the wrong reason. Included are
    four spellings of one address, which is what separates parsing from prefix
    matching.
    
    on_data_request is declared in the test: it is exported but appears in no
    header, being reached by function pointer from the XML reader.
    
    Both fixtures now release what they create, and the response endpoint call-site
    fixture added earlier is fixed the same way -- it was leaking its env and
    transport_out_desc. A leaking fixture makes the binary exit non-zero and costs
    the suite the signal that tells a real failure from noise.
    
    Full suite: 47 pass. test_http_transport still reports FAIL for the
    pre-existing SEGV in test_http_client, which reproduces identically on
    origin/master and on 1a8db5ba8, before any of this work.
    
    Co-Authored-By: Claude Opus 5 <no...@anthropic.com>
---
 test/core/addr/test_addr.cc                     | 133 ++++++++++++++++++++
 test/core/transport/http/test_http_transport.cc | 156 ++++++++++++++++++++++++
 2 files changed, 289 insertions(+)

diff --git a/test/core/addr/test_addr.cc b/test/core/addr/test_addr.cc
index dee036739..af42d69eb 100644
--- a/test/core/addr/test_addr.cc
+++ b/test/core/addr/test_addr.cc
@@ -24,6 +24,9 @@
 #include <axutil_error_default.h>
 #include <axis2_endpoint_ref.h>
 #include <axis2_core_utils.h>
+#include <axis2_conf.h>
+#include <axis2_conf_ctx.h>
+#include <axutil_param.h>
 #include <axis2_addr.h>
 #include <stdio.h>
 
@@ -152,3 +155,133 @@ TEST_F(TestAddr, test_response_endpoint_declined_by_default)
     ASSERT_FALSE(axis2_core_utils_is_response_endpoint_allowed(
         m_env, NULL, "file:///etc/passwd"));
 }
+
+/* ----------------------------------------------------------------------
+ * IPv6 response endpoints.
+ *
+ * The address checks were written against dotted quads. An IPv6 literal is
+ * bracketed and full of colons, so the host scan stopped at the first colon
+ * and classified a host of "[" -- every restricted v6 address was allowed
+ * through. These fix the classification at the point it is decided; the tests
+ * in test_http_transport.cc cover the call site that consults it.
+ *
+ * The gate has to be opened first: with the defaults nothing non-anonymous is
+ * allowed, so every case below would pass for the wrong reason.
+ * ---------------------------------------------------------------------- */
+
+class TestResponseEndpointIPv6 : public ::testing::Test
+{
+protected:
+    void SetUp() override
+    {
+        m_allocator = axutil_allocator_init(NULL);
+        m_error = axutil_error_create(m_allocator);
+        m_log = axutil_log_create(m_allocator, NULL, NULL);
+        m_env = axutil_env_create_with_error_log(m_allocator, m_error, m_log);
+        m_conf = axis2_conf_create(m_env);
+        m_conf_ctx = axis2_conf_ctx_create(m_env, m_conf);
+        set_param(AXIS2_ALLOW_NON_ANONYMOUS_RESPONSE_ENDPOINTS, "true");
+    }
+
+    void TearDown() override
+    {
+        if (m_conf_ctx) axis2_conf_ctx_free(m_conf_ctx, m_env);
+        axutil_env_free(m_env);
+    }
+
+    void set_param(const axis2_char_t *name, const axis2_char_t *value)
+    {
+        /* Asymmetric on purpose: axutil_param_create strdups the name but
+         * shallow-copies the value, and axutil_param_free releases both. So
+         * the name must NOT be copied here (that copy would leak) while the
+         * value must be. */
+        axis2_conf_add_param(m_conf, m_env, axutil_param_create(m_env,
+            (axis2_char_t *)name, axutil_strdup(m_env, value)));
+    }
+
+    axis2_bool_t allowed(const axis2_char_t *address)
+    {
+        return axis2_core_utils_is_response_endpoint_allowed(m_env, m_conf_ctx, address);
+    }
+
+    axutil_allocator_t *m_allocator = NULL;
+    axutil_error_t *m_error = NULL;
+    axutil_log_t *m_log = NULL;
+    axutil_env_t *m_env = NULL;
+    axis2_conf_t *m_conf = NULL;
+    axis2_conf_ctx_t *m_conf_ctx = NULL;
+};
+
+TEST_F(TestResponseEndpointIPv6, control_gate_is_actually_open)
+{
+    /* Without this the rest prove nothing: every ASSERT_FALSE below would hold
+     * simply because the feature is off. */
+    ASSERT_TRUE(allowed("https://198.51.100.7/callback"));
+    ASSERT_TRUE(allowed("https://[2001:db8::1]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, refuses_link_local_whatever_the_configuration)
+{
+    ASSERT_FALSE(allowed("https://[fe80::1]/callback"));
+    ASSERT_FALSE(allowed("https://[fe80::1]:8080/callback"));
+    ASSERT_FALSE(allowed("https://[febf::dead:beef]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, refuses_unspecified_and_multicast)
+{
+    ASSERT_FALSE(allowed("https://[::]/callback"));
+    ASSERT_FALSE(allowed("https://[ff02::1]/callback"));
+    ASSERT_FALSE(allowed("https://[ff00::]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, refuses_v4_mapped_metadata_address)
+{
+    /* The headline bypass: ::ffff:169.254.169.254 reaches the same instance
+     * metadata service the dotted quad does. */
+    ASSERT_FALSE(allowed("https://[::ffff:169.254.169.254]/latest/meta-data/"));
+    ASSERT_FALSE(allowed("https://[::169.254.169.254]/latest/meta-data/"));
+    ASSERT_FALSE(allowed("https://[::ffff:0.0.0.0]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, spelling_does_not_change_the_verdict)
+{
+    /* The same address written four ways. A textual prefix check would pass
+     * some of these; only parsing rejects all four. */
+    ASSERT_FALSE(allowed("https://[fe80::1]/callback"));
+    ASSERT_FALSE(allowed("https://[FE80::1]/callback"));
+    ASSERT_FALSE(allowed("https://[fe80:0:0:0:0:0:0:1]/callback"));
+    ASSERT_FALSE(allowed("https://[fe80:0000:0000:0000:0000:0000:0000:0001]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, loopback_and_unique_local_follow_the_private_switch)
+{
+    /* Off by default, because a callback inside the same network is how most
+     * decoupled deployments are wired. */
+    ASSERT_TRUE(allowed("https://[::1]/callback"));
+    ASSERT_TRUE(allowed("https://[fd00::1]/callback"));
+
+    set_param(AXIS2_BLOCK_PRIVATE_NETWORK_RESPONSE_ENDPOINTS, "true");
+    ASSERT_FALSE(allowed("https://[::1]/callback"));
+    ASSERT_FALSE(allowed("https://[fd00::1]/callback"));
+    ASSERT_FALSE(allowed("https://[fc00::1]/callback"));
+    ASSERT_FALSE(allowed("https://[::ffff:127.0.0.1]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, refuses_malformed_bracketed_hosts)
+{
+    /* Nothing legitimate produces these, so they must not fall through to the
+     * name path unclassified. */
+    ASSERT_FALSE(allowed("https://[notanaddress]/callback"));
+    ASSERT_FALSE(allowed("https://[fe80::1/callback"));
+    ASSERT_FALSE(allowed("https://[fe80::1::2]/callback"));
+    ASSERT_FALSE(allowed("https://[12345::1]/callback"));
+    ASSERT_FALSE(allowed("https://[fe80::1%eth0]/callback"));
+}
+
+TEST_F(TestResponseEndpointIPv6, ipv4_classification_still_holds)
+{
+    ASSERT_FALSE(allowed("https://169.254.169.254/latest/meta-data/"));
+    ASSERT_FALSE(allowed("https://0.0.0.0/callback"));
+    ASSERT_FALSE(allowed("https://239.255.255.250/callback"));
+    ASSERT_TRUE(allowed("https://198.51.100.7/callback"));
+}
diff --git a/test/core/transport/http/test_http_transport.cc b/test/core/transport/http/test_http_transport.cc
index 8114ad926..f037b73b6 100644
--- a/test/core/transport/http/test_http_transport.cc
+++ b/test/core/transport/http/test_http_transport.cc
@@ -36,6 +36,8 @@
 #include <axis2_addr.h>
 #include <axis2_core_utils.h>
 #include <axis2_http_transport_sender.h>
+#include <axutil_http_chunked_stream.h>
+#include <axutil_stream.h>
 #include <axutil_url.h>
 #include <axis2_http_client.h>
 #ifdef AXIS2_JSON_ENABLED
@@ -1528,6 +1530,8 @@ protected:
     void TearDown() override
     {
         if (m_conf_ctx) axis2_conf_ctx_free(m_conf_ctx, m_env);
+        if (m_out_desc) axis2_transport_out_desc_free(m_out_desc, m_env);
+        axutil_env_free(m_env);
     }
 
     /* A message context shaped the way the sender sees a response: a
@@ -1607,3 +1611,155 @@ TEST_F(TestResponseEndpointCallSite, client_side_send_is_not_screened)
     AXIS2_TRANSPORT_SENDER_FREE(sender, m_env);
     axis2_msg_ctx_free(mc, m_env);
 }
+
+/* ----------------------------------------------------------------------
+ * The ceiling on a chunked request body.
+ *
+ * The workers refuse an oversized body by looking at Content-Length, which a
+ * chunked request does not send. Such a request arrives with content_length of
+ * -1, so the worker check is skipped, and unread_len -- the other bound -- is
+ * disabled for the same reason: on_data_request only consults it when
+ * content_length is not -1. The read was therefore unbounded, and the caller
+ * decided how much the server buffered.
+ *
+ * on_data_request is the single point every chunked read passes through, from
+ * either worker and from all three callback contexts, so that is where the
+ * bound lives and where these test it. It is not in a public header -- it is
+ * reached by function pointer from the XML reader -- hence the declaration.
+ * ---------------------------------------------------------------------- */
+
+extern "C" int AXIS2_CALL
+axis2_http_transport_utils_on_data_request(char *buffer, int size, void *ctx);
+
+class TestChunkedBodyCeiling : public ::testing::Test
+{
+protected:
+    void SetUp() override
+    {
+        m_allocator = axutil_allocator_init(NULL);
+        m_error = axutil_error_create(m_allocator);
+        m_log = axutil_log_create(m_allocator, NULL, NULL);
+        m_env = axutil_env_create_with_error_log(m_allocator, m_error, m_log);
+    }
+
+    void TearDown() override
+    {
+        axutil_env_free(m_env);
+    }
+
+    /* A chunked body of total_len bytes, split into 64-byte chunks. */
+    std::string encode(int total_len)
+    {
+        std::string out;
+        int done = 0;
+        while (done < total_len)
+        {
+            int n = (total_len - done < 64) ? (total_len - done) : 64;
+            char hdr[16];
+            snprintf(hdr, sizeof(hdr), "%x\r\n", n);
+            out += hdr;
+            out.append((size_t)n, 'A');
+            out += "\r\n";
+            done += n;
+        }
+        out += "0\r\n\r\n";
+        return out;
+    }
+
+    /* Drain the body through the callback. Returns the terminating value: 0 on
+     * a clean end of stream, -1 when the ceiling refused it. bytes receives
+     * how much was handed to the parser before that.
+     *
+     * The body arrives over a socketpair rather than a basic stream. That is
+     * not incidental: axutil_stream_read_basic reads count-1 bytes to reserve
+     * room for a NUL, so a one-byte read returns zero, and the chunk-header
+     * parser reads exactly one byte at a time. A basic stream therefore looks
+     * like an immediate end of chunks and every case below would pass without
+     * reading anything. A socket stream is also what the workers really hand
+     * this code.
+     */
+    int drain(int max_body_size, int total_len, int *bytes)
+    {
+        std::string wire = encode(total_len);
+        axis2_callback_info_t cb;
+        char buf[128];
+        int sv[2];
+        int len = 0;
+        size_t off = 0;
+
+        if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0)
+        {
+            ADD_FAILURE() << "socketpair failed";
+            return -2;
+        }
+        while (off < wire.size())
+        {
+            ssize_t w = write(sv[1], wire.data() + off, wire.size() - off);
+            if (w <= 0) break;
+            off += (size_t)w;
+        }
+        shutdown(sv[1], SHUT_WR);
+
+        cb.env = m_env;
+        cb.in_stream = axutil_stream_create_socket(m_env, sv[0]);
+        cb.content_length = -1;   /* what a chunked request actually arrives with */
+        cb.unread_len = -1;
+        cb.chunked_stream = axutil_http_chunked_stream_create(
+            m_env, (axutil_stream_t *)cb.in_stream);
+        cb.max_body_size = max_body_size;
+        cb.body_read = 0;
+
+        *bytes = 0;
+        while ((len = axis2_http_transport_utils_on_data_request(
+                    buf, (int)sizeof(buf), &cb)) > 0)
+        {
+            *bytes += len;
+        }
+
+        axutil_http_chunked_stream_free(cb.chunked_stream, m_env);
+        axutil_stream_free((axutil_stream_t *)cb.in_stream, m_env);
+        close(sv[1]);
+        return len;
+    }
+
+    axutil_allocator_t *m_allocator = NULL;
+    axutil_error_t *m_error = NULL;
+    axutil_log_t *m_log = NULL;
+    axutil_env_t *m_env = NULL;
+};
+
+TEST_F(TestChunkedBodyCeiling, refuses_a_body_over_the_ceiling)
+{
+    int bytes = 0;
+    ASSERT_EQ(-1, drain(256, 4096, &bytes));
+
+    /* Overshoot is bounded by the buffer in hand, not by the body on the wire.
+     * Without the ceiling this would have been the whole 4096. */
+    ASSERT_LE(bytes, 256 + 128);
+}
+
+TEST_F(TestChunkedBodyCeiling, accepts_a_body_under_the_ceiling)
+{
+    int bytes = 0;
+    ASSERT_EQ(0, drain(4096, 1024, &bytes));
+    ASSERT_EQ(1024, bytes);
+}
+
+TEST_F(TestChunkedBodyCeiling, unlimited_still_means_unlimited)
+{
+    /* maxRequestSize of -1 restores the old behaviour and must not be read as
+     * a ceiling of -1, which would refuse everything. */
+    int bytes = 0;
+    ASSERT_EQ(0, drain((int)AXIS2_MAX_REQUEST_SIZE_UNLIMITED, 4096, &bytes));
+    ASSERT_EQ(4096, bytes);
+}
+
+TEST_F(TestChunkedBodyCeiling, boundary_is_not_off_by_one)
+{
+    int bytes = 0;
+    ASSERT_EQ(0, drain(1024, 1024, &bytes));
+    ASSERT_EQ(1024, bytes);
+
+    bytes = 0;
+    ASSERT_EQ(-1, drain(1023, 1024, &bytes));
+}