fix: kleinere Netzwerk-Härtungen

M2: WLAN-Passwort muss leer (offenes Netz) oder 8..64 Zeichen lang sein
(400 "invalid password" bei 1..7 Zeichen). CMake prüft NINLIGHT_WIFI_PASSWORD
ebenso (0 oder 8..63 Zeichen) und der Zeichen-Blacklist-Check bekommt `;`
dazu.

M4: Ein Authorization-Wert, der länger als NL_TOKEN_LEN-1 (64) ist, wird
von nl_http_parse() nicht mehr stillschweigend abgeschnitten, sondern setzt
`auth_too_long` in nl_http_request_t; nl_api_handle() behandelt eine solche
Anfrage als unauthentifiziert (außer bei offenem Zugriff), damit ein
abgeschnittener Wert nie zufällig auf ein kürzeres Token passt.

M5: get_int() prüfte die (long)-Cast-Gleichheit vor dem Bereichscheck -
bei sehr großen oder NaN-Werten ist der Cast von double nach long
undefiniertes Verhalten. Bereichs- und NaN-Check laufen jetzt vor dem Cast.

T1: sdk/Platform/Drivers/internalflash/wm_internal_fls.c: tls_fls_init()
ignorierte den Rückgabewert von tls_os_sem_create(&s_fls2_lock, 1) - jetzt
eine TLS_DBGPRT_ERR-Meldung, falls das fehlschlägt (dokumentiert in
sdk/PATCHES.md).

Tests: test_api.c (Passwortlängen, auth_too_long, 1e300-Brightness),
test_http.c (auth_too_long wird gesetzt). Für M5 zeigt der Host-Test kein
sichtbares RED, weil der (double)->(long)-Cast auf dieser Plattform/diesem
Compiler keinen Crash auslöst (implementation-defined statt UB-Crash) - der
Fix wird trotzdem angewendet, da er auf dem Zielcompiler (arm-none-eabi 4.9)
nicht garantiert sicher ist.
This commit is contained in:
Michael Sauer
2026-09-11 23:52:03 +02:00
parent d098623121
commit 2e90b8fd06
8 changed files with 74 additions and 8 deletions
+24
View File
@@ -106,6 +106,7 @@ static void test_post_validation(void)
{"{\"brightness\":101}", "invalid brightness"},
{"{\"brightness\":1.5}", "invalid brightness"},
{"{\"brightness\":\"50\"}", "invalid brightness"},
{"{\"brightness\":1e300}", "invalid brightness"}, /* M5: range check before the (long) cast */
{"{\"color\":{\"r\":1,\"g\":2}}", "invalid color"},
{"{\"color\":{\"r\":1,\"g\":2,\"b\":256}}", "invalid color"},
{"{\"color_temp\":999}", "invalid color_temp"},
@@ -171,6 +172,20 @@ static void test_auth_all_protects_device_state(void)
TEST_ASSERT_EQUAL_INT(200, resp.status);
}
static void test_auth_too_long_is_unauthenticated(void)
{
/* M4: even a token that starts with the real one but was truncated at NL_TOKEN_LEN - 1
* (i.e. the header carried more than that) must not authenticate. */
nl_http_request_t r;
memset(&r, 0, sizeof r);
r.method = NL_HTTP_GET;
strcpy(r.path, "/api/v1/system/settings");
strcpy(r.auth, TOKEN); /* would match on its own */
r.auth_too_long = true;
nl_api_handle(&fake_ctx, &r, &resp);
TEST_ASSERT_EQUAL_INT(401, resp.status);
}
/* --- system/info --- */
static void test_info_is_open(void)
{
@@ -240,6 +255,14 @@ static void test_settings_wifi(void)
TEST_ASSERT_EQUAL_STRING("invalid ssid", error_text());
call(NL_HTTP_PUT, "/api/v1/system/settings", TOKEN, "{\"type\":\"wifi\",\"ssid\":\"x\",\"password\":5}");
TEST_ASSERT_EQUAL_STRING("invalid password", error_text());
/* M2: password must be empty (open network) or 8..64 chars, never 1..7 */
call(NL_HTTP_PUT, "/api/v1/system/settings", TOKEN, "{\"type\":\"wifi\",\"ssid\":\"x\",\"password\":\"short\"}");
TEST_ASSERT_EQUAL_INT(400, resp.status);
TEST_ASSERT_EQUAL_STRING("invalid password", error_text());
call(NL_HTTP_PUT, "/api/v1/system/settings", TOKEN, "{\"type\":\"wifi\",\"ssid\":\"x\",\"password\":\"\"}");
TEST_ASSERT_EQUAL_INT(202, resp.status);
call(NL_HTTP_PUT, "/api/v1/system/settings", TOKEN, "{\"type\":\"wifi\",\"ssid\":\"x\",\"password\":\"12345678\"}");
TEST_ASSERT_EQUAL_INT(202, resp.status);
fake_wifi_result = -1;
call(NL_HTTP_PUT, "/api/v1/system/settings", TOKEN, "{\"type\":\"wifi\",\"ssid\":\"x\"}");
TEST_ASSERT_EQUAL_INT(503, resp.status);
@@ -329,6 +352,7 @@ int main(void)
RUN_TEST(test_post_settings_fields);
RUN_TEST(test_post_save_failure);
RUN_TEST(test_auth_all_protects_device_state);
RUN_TEST(test_auth_too_long_is_unauthenticated);
RUN_TEST(test_info_is_open);
RUN_TEST(test_info_with_sta_ip_and_error);
RUN_TEST(test_settings_auth);