Use the node's admin password for the migration bridge AP
The Wi-Fi, LoRa first-stage and Partition Expander bridges serve an unauthenticated /update behind an AP whose password was fixed and published, so anyone in range during a migration could upload firmware while the private key is staged. Use the old node's saved admin password as the WPA2 passphrase, read from /com_prefs or /node_prefs at offset 56 as the firmware loads it. The operator already used that password to start the old updater. Fall back to the published password, with a serial warning, when the saved admin password is missing, unreadable, shorter than 8 characters or not printable ASCII, so no node becomes unreachable. The legacy filesystem is mounted without formatting and the password is never printed. Migration admission, flash sequencing, /update gating and the USB bridge are unchanged. Update both guides and the package README. Add a sanitizer-backed host test that compiles the bridge's actual Wi-Fi block against fake SPIFFS and WiFi: both boots, file precedence, WPA2 length and terminator bounds, printable characters, stock 1.10/1.11 file sizes, truncated fields, and mount, seek and partial-read failures. It fails 8 of 8 on the previous code and catches 27 of 27 code mutations. Verified 13 related host test files and the esp32_s3_4mb_partition_migrator build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0e72474f41
commit
09435b049d
6 files changed
+444
-10
No files matched your search
@@ -33,7 +33,9 @@ namespace migration = mesh::esp32_partition_migration;
|
||||
namespace {
|
||||
|
||||
constexpr char kApSsid[] = "MeshCore-Migrate";
|
||||
constexpr char kApPassword[] = "meshcore-migrate";
|
||||
// Published in the docs, so it protects nothing. Used only when the node's own
|
||||
// admin password cannot serve as the WPA2 passphrase.
|
||||
constexpr char kFallbackApPassword[] = "meshcore-migrate";
|
||||
#ifndef MESHCORE_MIGRATION_DELAY_MS
|
||||
#define MESHCORE_MIGRATION_DELAY_MS 4500
|
||||
#endif
|
||||
@@ -1193,11 +1195,46 @@ void sendHome(AsyncWebServerRequest* request) {
|
||||
request->send(200, "text/html", page);
|
||||
}
|
||||
|
||||
bool ap_password_is_admin = false;
|
||||
|
||||
// Anyone who joins the bridge AP can upload firmware while the private key is
|
||||
// staged. The operator started this migration from the node's authenticated
|
||||
// terminal, so its admin password is a secret they already hold. Stock and
|
||||
// keymindCascade /com_prefs (and older /node_prefs) images all keep its
|
||||
// 16-byte field at offset 56; stock 1.10/1.11 images end before byte 290.
|
||||
bool loadAdminApPassword(char (&password)[16]) {
|
||||
constexpr size_t kPasswordOffset = 56;
|
||||
memset(password, 0, sizeof(password));
|
||||
// Never format: before migration this is the legacy filesystem.
|
||||
if (!SPIFFS.begin(false)) return false;
|
||||
const char* path = SPIFFS.exists("/com_prefs") ? "/com_prefs"
|
||||
: (SPIFFS.exists("/node_prefs") ? "/node_prefs" : nullptr);
|
||||
File prefs = path ? SPIFFS.open(path, "r") : File();
|
||||
// Byte 15 stays zero, matching the firmware's own terminator.
|
||||
bool valid = prefs && prefs.size() >= kPasswordOffset + sizeof(password)
|
||||
&& prefs.seek(kPasswordOffset)
|
||||
&& prefs.read(reinterpret_cast<uint8_t*>(password), sizeof(password) - 1)
|
||||
== sizeof(password) - 1;
|
||||
if (prefs) prefs.close();
|
||||
SPIFFS.end();
|
||||
// WPA2 needs 8-63 printable ASCII characters. Anything else falls back
|
||||
// rather than leaving the bridge unreachable.
|
||||
size_t length = 0;
|
||||
while (valid && password[length] != 0) {
|
||||
valid = password[length] >= 0x20 && password[length] <= 0x7E;
|
||||
++length;
|
||||
}
|
||||
return valid && length >= 8;
|
||||
}
|
||||
|
||||
void startServer() {
|
||||
// A legacy ESP-NOW repeater can leave the AP protocol mask in proprietary
|
||||
// LR mode. SoftAP then reports success but ordinary phones and laptops
|
||||
// cannot discover it. Reuse the normal WebConfig recipe: AP+STA mode,
|
||||
// an explicit interoperable protocol mask, and the project AP channel.
|
||||
char admin_password[16];
|
||||
ap_password_is_admin = loadAdminApPassword(admin_password);
|
||||
const char* ap_password = ap_password_is_admin ? admin_password : kFallbackApPassword;
|
||||
WiFi.mode(WIFI_AP_STA);
|
||||
WiFi.setAutoReconnect(false);
|
||||
WiFi.disconnect(false, true);
|
||||
@@ -1206,7 +1243,7 @@ void startServer() {
|
||||
const IPAddress address(192, 168, 4, 1);
|
||||
const IPAddress netmask(255, 255, 255, 0);
|
||||
if (!WiFi.softAPConfig(address, address, netmask)
|
||||
|| !WiFi.softAP(kApSsid, kApPassword, mesh::wifi::accessPointChannel())
|
||||
|| !WiFi.softAP(kApSsid, ap_password, mesh::wifi::accessPointChannel())
|
||||
|| mesh::wifi::applyAccessPointProtocolMask() != ESP_OK
|
||||
|| esp_wifi_set_protocol(WIFI_IF_STA, mesh::wifi::kProtocolMask) != ESP_OK
|
||||
|| esp_wifi_set_max_tx_power(78) != ESP_OK) {
|
||||
@@ -1221,6 +1258,9 @@ void startServer() {
|
||||
WiFi.softAPSSID().c_str(), WiFi.softAPIP().toString().c_str(),
|
||||
(unsigned)ap_config.ap.channel, (unsigned)ap_config.ap.ssid_hidden,
|
||||
max_tx_power / 4.0, (int)config_result, (int)power_result);
|
||||
if (!ap_password_is_admin)
|
||||
Serial.println("Warning: no saved admin password of 8-15 printable characters; "
|
||||
"anyone with the published fallback password can upload firmware");
|
||||
server.on("/", HTTP_GET, sendHome);
|
||||
server.on("/reboot", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
if (!migration_complete) {
|
||||
@@ -1347,7 +1387,8 @@ void setup() {
|
||||
#else
|
||||
startServer();
|
||||
Serial.printf("%s. Join %s (password: %s), then open http://192.168.4.1/\n",
|
||||
status_text, kApSsid, kApPassword);
|
||||
status_text, kApSsid,
|
||||
ap_password_is_admin ? "this node's admin password" : kFallbackApPassword);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user