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/18 14:19:00 UTC

(axis-axis2-c-core) 03/16: Stop two security tests from crashing the suites they belong to

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 8a0523c47d76527d7134f3e95676ac9f65d1a3c0
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Mon Aug 17 04:26:42 2026 -1000

    Stop two security tests from crashing the suites they belong to
    
    Both asserted that a NULL argument was handled safely and segfaulted
    instead, taking the tests declared after them with the process. axutil_strdup
    now checks env explicitly, since AXIS2_ENV_CHECK expands to nothing. The JSON
    case asked json_tokener_parse for NULL handling that json-c does not offer;
    assert the zero-length buffer it does define. That crash had been hiding a
    wrong expectation about trailing commas, now split into its own test.
    
    Co-Authored-By: Claude Opus 5 (1M context) <no...@anthropic.com>
---
 test/core/security/test_json_security.cc | 59 +++++++++++++++++++++++++++++---
 util/src/string.c                        |  9 +++++
 2 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/test/core/security/test_json_security.cc b/test/core/security/test_json_security.cc
index d2b6e4b1a..7763f242f 100644
--- a/test/core/security/test_json_security.cc
+++ b/test/core/security/test_json_security.cc
@@ -86,11 +86,34 @@ TEST_F(JSONSecurityTest, ValidJSONParses) {
 }
 
 /**
- * Test that NULL input is handled safely
+ * Test that a NUL-terminated-but-empty buffer is handled safely.
+ *
+ * This case used to pass NULL to json_tokener_parse and assert it returned
+ * NULL. json-c does not offer that: its contract is a NUL-terminated string,
+ * and it calls strlen() on what it is given, so NULL segfaulted inside the
+ * library. Because a SEGV takes the whole gtest process with it, that one
+ * assertion cost the eight tests declared after it -- including
+ * DeeplyNestedJSONDepthLimit, which is what covers the documented 64-level
+ * limit. The suite reported a failure for the crash and silent nothing for the
+ * tests that never ran.
+ *
+ * Axis2/C guards this at its own boundary instead, which is where the
+ * guarantee actually belongs: every json_tokener_parse call site checks its
+ * input first -- axis2_h2_transport_utils.c after its allocation,
+ * axis2_json_rpc_msg_recv.c at the parameter check in the direct-invoke path
+ * and at the caller of the Android static path, and
+ * axis2_apache2_request_processor_json_impl.c after its buffer read. Those
+ * guards, not json-c, are what keep NULL away from the parser, and they are
+ * exercised by the transport tests rather than from here.
+ *
+ * What is left to check at this level is the degenerate input json-c does
+ * define behaviour for, which is a zero-length string.
  */
-TEST_F(JSONSecurityTest, NullInputSafe) {
-    json_object *obj = json_tokener_parse(NULL);
-    EXPECT_EQ(obj, nullptr) << "NULL input should return NULL";
+TEST_F(JSONSecurityTest, ZeroLengthBufferSafe) {
+    const char buffer[1] = { '\0' };
+
+    json_object *obj = json_tokener_parse(buffer);
+    EXPECT_EQ(obj, nullptr) << "Zero-length input should return NULL";
 }
 
 /**
@@ -111,7 +134,6 @@ TEST_F(JSONSecurityTest, MalformedJSONRejected) {
         "{\"key\"",
         "}{",
         "[[[",
-        "{\"a\":\"b\",}",  /* trailing comma */
         NULL
     };
 
@@ -124,6 +146,33 @@ TEST_F(JSONSecurityTest, MalformedJSONRejected) {
     }
 }
 
+/**
+ * A trailing comma is accepted, which is json-c's documented default.
+ *
+ * This case used to sit in the list above, asserting rejection. It never ran:
+ * the NULL-input case earlier in this file segfaulted and took the process with
+ * it, so the wrong expectation was never contradicted. Rejecting a trailing
+ * comma requires JSON_TOKENER_STRICT, which none of the Axis2/C call sites set
+ * -- they all use json_tokener_parse(), whose flags default to lenient.
+ *
+ * Pinned rather than removed because it is the kind of leniency worth noticing
+ * if it ever changes: a document this server accepts and a stricter parser in
+ * front of or behind it rejects is exactly the disagreement that lets two hops
+ * read one request differently. Switching the transport to strict mode is a
+ * deliberate decision with a compatibility cost for clients that emit trailing
+ * commas today, so it is recorded here rather than made here.
+ */
+TEST_F(JSONSecurityTest, TrailingCommaAcceptedByDefault) {
+    json_object *obj = json_tokener_parse("{\"a\":\"b\",}");
+
+    EXPECT_NE(obj, nullptr)
+        << "json-c accepts a trailing comma unless JSON_TOKENER_STRICT is set; "
+           "if this now returns NULL the parser or its flags changed";
+    if (obj) {
+        json_object_put(obj);
+    }
+}
+
 /**
  * Test deeply nested JSON handling
  * json-c has a default depth limit (typically 32)
diff --git a/util/src/string.c b/util/src/string.c
index 4a3f8f019..a9328f2b9 100644
--- a/util/src/string.c
+++ b/util/src/string.c
@@ -261,6 +261,15 @@ axutil_strdup(
     const axutil_env_t *env,
     const void *ptr)
 {
+    /* Checked explicitly rather than with AXIS2_ENV_CHECK, which expands to
+     * nothing (see axutil_env.h) and so guards none of the call sites that
+     * appear to use it. Without this, a NULL env reached env->allocator below
+     * and crashed, which is what the existing NullEnvHandling test asserts
+     * against; the ptr check alone does not cover it. */
+    if(!env)
+    {
+        return NULL;
+    }
     if(ptr)
     {
         int len = axutil_strlen(ptr);