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:
@@ -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);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "net/nl_http.h"
|
||||
@@ -41,6 +42,21 @@ static void test_headers_case_insensitive_and_bearer(void)
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_PUT, r.method);
|
||||
TEST_ASSERT_EQUAL_STRING("abc123", r.auth);
|
||||
TEST_ASSERT_EQUAL_UINT(2, r.body_len);
|
||||
TEST_ASSERT_FALSE(r.auth_too_long);
|
||||
}
|
||||
|
||||
static void test_auth_too_long_sets_flag(void)
|
||||
{
|
||||
/* M4: token max is 64 chars (NL_TOKEN_LEN - 1); one char beyond that must be flagged
|
||||
* rather than silently truncated and possibly matching by accident. */
|
||||
char hdr[128];
|
||||
nl_http_request_t r;
|
||||
char token[66];
|
||||
memset(token, 'a', sizeof token - 1);
|
||||
token[sizeof token - 1] = '\0'; /* 65 chars: one more than NL_TOKEN_LEN - 1 */
|
||||
snprintf(hdr, sizeof hdr, "GET / HTTP/1.1\r\nAuthorization: %s\r\n\r\n", token);
|
||||
TEST_ASSERT_EQUAL(NL_HTTP_COMPLETE, parse(hdr, &r));
|
||||
TEST_ASSERT_TRUE(r.auth_too_long);
|
||||
}
|
||||
|
||||
static void test_plain_token_and_query_string(void)
|
||||
@@ -108,6 +124,7 @@ int main(void)
|
||||
RUN_TEST(test_simple_get);
|
||||
RUN_TEST(test_post_body_arrives_in_parts);
|
||||
RUN_TEST(test_headers_case_insensitive_and_bearer);
|
||||
RUN_TEST(test_auth_too_long_sets_flag);
|
||||
RUN_TEST(test_plain_token_and_query_string);
|
||||
RUN_TEST(test_other_method);
|
||||
RUN_TEST(test_body_too_large);
|
||||
|
||||
Reference in New Issue
Block a user