From 89c764514aa97d459155091e76813e69f99cb62b Mon Sep 17 00:00:00 2001 From: Michael Sauer Date: Fri, 11 Sep 2026 23:50:45 +0200 Subject: [PATCH] fix: cJSON-Verschachtelung begrenzen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C1: Ein POST an /api/v1/device/state mit tief verschachteltem JSON (z.B. 20 Ebenen "[[[[...") lässt cJSONs rekursiven Parser den 6-KB-Stack des HTTP-Tasks überlaufen - CJSON_NESTING_LIMIT war mit 1000 viel zu hoch und FreeRTOS' Stack-Overflow-Checking ist aus. Begrenzt auf 8 Ebenen, sowohl für die Firmware (third_party/cJSON/cJSON.c) als auch für die Host-Tests (nl_cjson), damit beide dasselbe Verhalten zeigen. Test: test_post_validation deckt jetzt einen Body mit 20 verschachtelten Arrays ab (400 "invalid json"). --- CMakeLists.txt | 4 ++++ test/CMakeLists.txt | 2 ++ test/test_api.c | 1 + 3 files changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23b4194..d777537 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,6 +73,10 @@ set(NL_FW_SOURCES src/sys/nl_app_api.c third_party/cJSON/cJSON.c) +# C1: deeply nested JSON must not overflow the 6 KB HTTP task stack (parse_value recurses). +set_source_files_properties(third_party/cJSON/cJSON.c PROPERTIES + COMPILE_DEFINITIONS "CJSON_NESTING_LIMIT=8") + set(NL_FW ninlight_${NINLIGHT_DEVICE}) add_executable(${NL_FW} ${NL_SDK_BOOT_SOURCES} ${NL_FW_SOURCES} ${NL_SDK_OBJECTS}) set_target_properties(${NL_FW} PROPERTIES SUFFIX ".elf") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a771b37..6b98238 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,6 +15,8 @@ enable_testing() # cJSON is third-party code: build it without -Werror. add_library(nl_cjson STATIC ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/cJSON/cJSON.c) target_include_directories(nl_cjson PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/cJSON) +# C1: match the firmware's nesting limit so host tests exercise the same behaviour. +target_compile_definitions(nl_cjson PUBLIC CJSON_NESTING_LIMIT=8) set(NL_SRC ${CMAKE_CURRENT_SOURCE_DIR}/../src) diff --git a/test/test_api.c b/test/test_api.c index 794d609..2e6fd05 100644 --- a/test/test_api.c +++ b/test/test_api.c @@ -100,6 +100,7 @@ static void test_post_validation(void) static const struct { const char *body; const char *err; } CASES[] = { {"not json", "invalid json"}, {"[1,2]", "invalid json"}, + {"{\"a\":[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]}", "invalid json"}, /* C1: 20 levels > CJSON_NESTING_LIMIT */ {"{\"on\":1}", "invalid on"}, {"{\"brightness\":0}", "invalid brightness"}, {"{\"brightness\":101}", "invalid brightness"},