fix: stop sending stored passwords and PIN back to clients

The settings routes returned the WiFi passphrase, MQTT password and the
login password and PIN in plain text, and the debug log printed them and
the API key. Responses now carry hasPassphrase/hasPassword/hasPin flags,
logs mask the values, and an omitted secret keeps the stored one.

The UI loads secret fields empty and treats a blank field as "keep" while
the SSID or username is unchanged. Clearing a secret without changing the
SSID or username is listed in docs/KNOWN_ISSUES.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-18 14:34:18 +02:00
parent e963a5adb3
commit 1fb4c4cfcd
8 changed files with 189 additions and 29 deletions

View file

@ -0,0 +1,81 @@
#include <unity.h>
#include "Secrets.h"
void setUp() {}
void tearDown() {}
/*********************************************************************
* hasSecret / maskSecret
********************************************************************/
static void test_hasSecret_null_and_empty_are_unset() {
TEST_ASSERT_FALSE(hasSecret(nullptr));
TEST_ASSERT_FALSE(hasSecret(""));
}
static void test_hasSecret_any_value_is_set() {
TEST_ASSERT_TRUE(hasSecret("x"));
TEST_ASSERT_TRUE(hasSecret(" "));
}
static void test_maskSecret_never_returns_the_secret() {
TEST_ASSERT_EQUAL_STRING("<set>", maskSecret("hunter2"));
TEST_ASSERT_EQUAL_STRING("<empty>", maskSecret(""));
TEST_ASSERT_EQUAL_STRING("<empty>", maskSecret(nullptr));
}
/*********************************************************************
* applySecret
********************************************************************/
static void test_applySecret_absent_keeps_stored_value() {
char dest[16] = "stored";
TEST_ASSERT_FALSE(applySecret(nullptr, dest, sizeof(dest)));
TEST_ASSERT_EQUAL_STRING("stored", dest);
}
static void test_applySecret_new_value_replaces_stored() {
char dest[16] = "stored";
TEST_ASSERT_TRUE(applySecret("fresh", dest, sizeof(dest)));
TEST_ASSERT_EQUAL_STRING("fresh", dest);
}
static void test_applySecret_same_value_is_no_change() {
char dest[16] = "stored";
TEST_ASSERT_FALSE(applySecret("stored", dest, sizeof(dest)));
TEST_ASSERT_EQUAL_STRING("stored", dest);
}
static void test_applySecret_prefix_of_stored_is_a_change() {
char dest[16] = "stored";
TEST_ASSERT_TRUE(applySecret("sto", dest, sizeof(dest)));
TEST_ASSERT_EQUAL_STRING("sto", dest);
}
// Sending an empty value explicitly clears it, e.g. an open WiFi network.
static void test_applySecret_empty_value_clears_stored() {
char dest[16] = "stored";
TEST_ASSERT_TRUE(applySecret("", dest, sizeof(dest)));
TEST_ASSERT_EQUAL_STRING("", dest);
}
static void test_applySecret_truncates_and_terminates() {
char storage[8];
memset(storage, 'X', sizeof(storage));
storage[0] = '\0';
TEST_ASSERT_TRUE(applySecret("abcdefgh", storage, 5));
TEST_ASSERT_EQUAL_STRING("abcd", storage);
// Nothing past the buffer size is touched.
TEST_ASSERT_EQUAL_HEX8('X', storage[5]);
}
static void test_applySecret_truncated_same_value_is_no_change() {
char dest[5] = "abcd";
TEST_ASSERT_FALSE(applySecret("abcdefgh", dest, sizeof(dest)));
TEST_ASSERT_EQUAL_STRING("abcd", dest);
}
int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_hasSecret_null_and_empty_are_unset);
RUN_TEST(test_hasSecret_any_value_is_set);
RUN_TEST(test_maskSecret_never_returns_the_secret);
RUN_TEST(test_applySecret_absent_keeps_stored_value);
RUN_TEST(test_applySecret_new_value_replaces_stored);
RUN_TEST(test_applySecret_same_value_is_no_change);
RUN_TEST(test_applySecret_prefix_of_stored_is_a_change);
RUN_TEST(test_applySecret_empty_value_clears_stored);
RUN_TEST(test_applySecret_truncates_and_terminates);
RUN_TEST(test_applySecret_truncated_same_value_is_no_change);
return UNITY_END();
}