Fix room browser stack overflow and bound response bodies

This commit is contained in:
mikecarper committed 2026-10-17 23:31:24 -07:00
1 parent a4a0b97c3f
commit 68f45e5cf5
6 files changed
+464 -218

No files matched your search

+239 -211
View File
@@ -413,6 +413,241 @@ static const char* roomWebString(JsonVariantConst value, size_t maximum) {
return text.c_str();
}
// Keep operation-specific arrays and store call chains out of the common
// callback's frame. The ESP32 loop task has an 8 KB stack, including printf
// and filesystem frames; these private handlers must remain separate calls.
bool MyMesh::processRoomWebMail(const mesh::Identity& author, uint8_t role,
uint32_t sequence, JsonObjectConst input,
mesh::RoomJsonWriter& json) {
return mesh::handleRoomMailWeb(_fs, author.pub_key,
role == PERM_ACL_READ_WRITE || role == PERM_ACL_ADMIN, role == PERM_ACL_ADMIN,
sequence, getRTCClock()->getCurrentTime(), input, json,
[this](const uint8_t* identity) { return room_access.allowsIdentity(identity); });
}
void MyMesh::processRoomWebAdmin(const char* operation, JsonObjectConst input,
mesh::RoomJsonWriter& json) {
auto error = [&json](const char* message) {
json.reset();
json.raw("{\"error\":"); json.string(message); json.raw("}"); json.finish();
};
if (strcmp(operation, "admin.settings") == 0) {
json.raw("{\"topic\":"); json.string(room_topic);
json.raw(",\"persistent_history\":"); json.raw(room_history_enabled ? "true" : "false");
json.raw(",\"post_rate\":"); json.number(room_access.postRate());
json.raw(",\"poll_rate\":"); json.number(room_access.pollRate());
json.raw(",\"boot\":"); json.number(room_web_boot_id);
json.raw("}"); json.finish(); return;
}
if (strcmp(operation, "admin.users") == 0) {
if (!input["cursor"].is<uint16_t>() || input["cursor"].as<uint16_t>() > acl.getNumClients()) {
error("invalid user cursor; reload users"); return;
}
const uint16_t cursor = input["cursor"].as<uint16_t>();
uint16_t active = 0, pending = 0, failed = 0;
uint32_t backlog = 0;
for (int i = 0; i < acl.getNumClients(); ++i) {
ClientInfo* client = acl.getClientByIdx(i);
active += client->last_activity != 0 && room_access.allowsIdentity(client->id.pub_key);
backlog += getUnsyncedCount(client);
pending += client->extra.room.pending_ack != 0;
failed += client->extra.room.push_failures != 0 || client->extra.room.topic_failures != 0;
}
json.raw("{\"users\":[");
uint16_t next = cursor;
for (; next < acl.getNumClients() && next < cursor + 2; ++next) {
if (next != cursor) json.raw(",");
writeRoomClientJson(json, acl.getClientByIdx(next), false);
}
json.raw("],\"next\":");
if (next < acl.getNumClients()) json.number(next); else json.raw("null");
json.raw(",\"total\":"); json.number(acl.getNumClients());
json.raw(",\"active\":"); json.number(active);
json.raw(",\"backlog\":"); json.number(backlog);
json.raw(",\"pending\":"); json.number(pending);
json.raw(",\"failed\":"); json.number(failed);
json.raw(",\"boot\":"); json.number(room_web_boot_id);
json.raw("}"); json.finish(); return;
}
char reply[157] = {};
if (strcmp(operation, "admin.topic") == 0) {
const char* value = roomWebString(input["value"], MAX_POST_TEXT_LEN);
const char* expected = roomWebString(input["expected"], MAX_POST_TEXT_LEN);
if (!value || !expected) { error("topic and expected topic required"); return; }
if (strcmp(expected, room_topic) != 0) { error("topic changed; refresh before editing"); return; }
char command[MAX_POST_TEXT_LEN + 11];
snprintf(command, sizeof(command), "set topic %s", value);
if (strcmp(value, room_topic) != 0) handleRoomTopicCommand(command, reply);
} else if (strcmp(operation, "admin.history") == 0) {
if (!input["enabled"].is<bool>() || !input["expected"].is<bool>()) {
error("history setting and expected setting required"); return;
}
if (input["expected"].as<bool>() != room_history_enabled) {
error("history setting changed; refresh before editing"); return;
}
if (input["enabled"].as<bool>() != room_history_enabled) {
handleRoomHistoryCommand(input["enabled"].as<bool>() ? "set room.history on"
: "set room.history off", reply);
}
} else if (strcmp(operation, "admin.rates") == 0) {
if (!input["post_rate"].is<uint16_t>() || !input["poll_rate"].is<uint16_t>()) {
error("rates must be 0-65535"); return;
}
if (!room_access.setRates(_fs, input["post_rate"].as<uint16_t>(), input["poll_rate"].as<uint16_t>())) {
error("rate save failed; unchanged"); return;
}
} else {
const char* target = roomWebString(input["key"], PUB_KEY_SIZE * 2);
uint8_t decoded[PUB_KEY_SIZE];
if (!target || strlen(target) != PUB_KEY_SIZE * 2
|| !mesh::Utils::fromHex(decoded, sizeof(decoded), target)) {
error("complete 64-hex public key required"); return;
}
ClientInfo* client = acl.getClient(decoded, sizeof(decoded));
if (strcmp(operation, "admin.ban") == 0) {
if (!input["banned"].is<bool>()) { error("ban setting required"); return; }
const bool banned = input["banned"].as<bool>();
const auto result = banned ? room_access.addBan(_fs, decoded) : room_access.removeBan(_fs, decoded);
if (result != mesh::RoomAccessPolicy::BanResult::Saved
&& result != mesh::RoomAccessPolicy::BanResult::Unchanged) {
error("ban save failed or ban list full; unchanged"); return;
}
} else {
if (!client) { error("unknown room user; reload users"); return; }
if (strcmp(operation, "admin.user") == 0) {
json.raw("{\"user\":"); writeRoomClientJson(json, client, true);
json.raw(",\"boot\":"); json.number(room_web_boot_id);
json.raw("}"); json.finish(); return;
}
if (strcmp(operation, "admin.route") == 0) {
const char* which = roomWebString(input["which"], 7);
const char* value = roomWebString(input["value"], 191);
const char* expected = roomWebString(input["expected"], 154);
if (!which || !value || !expected
|| (strcmp(which, "outpath") != 0 && strcmp(which, "altpath") != 0)) {
error("path selection, value and expected path required"); return;
}
const bool alternate = strcmp(which, "altpath") == 0;
char current[157];
mesh::formatRoomClientPathReply(alternate ? client->alt_path : client->out_path,
alternate ? client->alt_path_len : client->out_path_len, current, sizeof(current));
if (strcmp(expected, current + 2) != 0) { error("path changed; refresh before editing"); return; }
setRoomClientPath(client, which, value, reply);
} else if (strcmp(operation, "admin.catchup") == 0) {
const char* mode = roomWebString(input["mode"], 6);
if (!input["expected_sync"].is<uint32_t>()
|| input["expected_sync"].as<uint32_t>() != client->extra.room.sync_since) {
error("catch-up position changed; refresh before editing"); return;
}
if (!mode || (strcmp(mode, "before") != 0 && strcmp(mode, "keep") != 0)
|| (strcmp(mode, "before") == 0 ? !input["before"].is<uint32_t>()
: !input["count"].is<uint16_t>())) {
error("use before timestamp or keep count"); return;
}
applyRoomCatchUpCommand(client, mode, strcmp(mode, "before") == 0
? input["before"].as<uint32_t>() : input["count"].as<uint16_t>(), reply);
} else if (strcmp(operation, "admin.access") == 0) {
if (!input["role"].is<uint8_t>() || input["role"].as<uint8_t>() > PERM_ACL_ADMIN
|| !input["expected_role"].is<uint8_t>()
|| input["expected_role"].as<uint8_t>() != (client->permissions & PERM_ACL_ROLE_MASK)) {
error("invalid role or access changed; refresh before editing"); return;
}
const uint8_t selected = input["role"].as<uint8_t>();
if (selected != (client->permissions & PERM_ACL_ROLE_MASK)
|| !client->permissions_are_explicit || selected == PERM_ACL_GUEST) {
const uint8_t permissions = selected == PERM_ACL_GUEST ? 0
: (client->permissions & ~PERM_ACL_ROLE_MASK) | selected;
if (!acl.applyPermissions(self_id, decoded, sizeof(decoded), permissions)) {
error("access change failed; unchanged"); return;
}
mesh::scheduleLazyPersistenceMutation(dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
}
json.raw("{\"ok\":true,\"pending_save\":");
json.raw(dirty_contacts_expiry != 0 ? "true" : "false");
json.raw("}"); json.finish(); return;
}
}
}
if (strncmp(reply, "Err", 3) == 0) { error(reply); return; }
json.raw("{\"ok\":true}"); json.finish(); return;
}
void MyMesh::processRoomWebBoard(const char* operation, JsonObjectConst input,
mesh::RoomJsonWriter& json) {
auto error = [&json](const char* message) {
json.reset();
json.raw("{\"error\":"); json.string(message); json.raw("}"); json.finish();
};
mesh::RoomBoardIndex board;
if (!mesh::loadRoomBoard(_fs, board)) { error("information board storage unavailable"); return; }
if (strcmp(operation, "board.index") == 0) {
if (!input["revision"].is<uint32_t>() || !input["cursor"].is<uint8_t>()) { error("invalid board cursor"); return; }
const uint32_t revision = input["revision"].as<uint32_t>();
const uint8_t cursor = input["cursor"].as<uint8_t>();
if (cursor > board.count || (revision != board.revision && (revision != 0 || cursor != 0))) {
error("board changed; reload index"); return;
}
json.raw("{\"revision\":"); json.number(board.revision);
json.raw(",\"articles\":[");
uint8_t next = cursor;
for (; next < board.count && next < cursor + 2; ++next) {
if (next != cursor) json.raw(",");
const auto& article = board.articles[next];
json.raw("{\"id\":"); json.number(article.id);
json.raw(",\"version\":"); json.number(article.version);
json.raw(",\"length\":"); json.number(article.body_length);
json.raw(",\"title\":"); json.string(article.title); json.raw("}");
}
json.raw("],\"next\":"); json.number(next < board.count ? next : 255);
json.raw("}"); json.finish(); return;
}
if (!input["id"].is<uint8_t>() || !input["version"].is<uint32_t>()) { error("invalid article identity/version"); return; }
const uint8_t id = input["id"].as<uint8_t>();
const uint32_t version = input["version"].as<uint32_t>();
const mesh::RoomBoardArticle* article = nullptr;
for (uint8_t i = 0; i < board.count; ++i) if (board.articles[i].id == id) article = &board.articles[i];
if (strcmp(operation, "board.read") == 0) {
if (!input["offset"].is<uint16_t>()) { error("invalid article offset"); return; }
const uint16_t offset = input["offset"].as<uint16_t>();
uint8_t chunk[mesh::ROOM_BOARD_READ_MAX_CHUNK];
size_t length = 0;
const auto result = mesh::readRoomBoardArticle(_fs, id, version, offset, chunk, sizeof(chunk), length);
if (result != mesh::RoomBoardResult::Success || !article) { error("article changed or unavailable; reload index"); return; }
char encoded[4 * ((sizeof(chunk) + 2) / 3) + 1]; mesh::roomEncodeBase64(chunk, length, encoded);
json.raw("{\"version\":"); json.number(version);
json.raw(",\"offset\":"); json.number(offset);
json.raw(",\"next\":"); json.number(uint32_t(offset) + length);
json.raw(",\"total\":"); json.number(article->body_length);
json.raw(",\"data64\":"); json.string(encoded); json.raw("}"); json.finish(); return;
}
mesh::RoomBoardResult result;
if (strcmp(operation, "board.delete") == 0) {
if (version == 0) { error("deletion requires the current article version"); return; }
result = mesh::deleteRoomBoardArticle(_fs, id, version);
} else {
const char* title = roomWebString(input["title"], 63);
const char* encoded = roomWebString(input["body64"], 2732);
if (!title || !encoded || (article ? article->version != version : version != 0)) {
error("article changed; reload index before editing"); return;
}
auto* body = new (std::nothrow) uint8_t[mesh::ROOM_BOARD_MAX_BODY_LENGTH];
if (!body) { error("insufficient memory; article retained"); return; }
size_t length = 0;
if (!mesh::roomDecodeBase64(encoded, body, mesh::ROOM_BOARD_MAX_BODY_LENGTH, length)) {
delete[] body; error("invalid article encoding"); return;
}
result = mesh::saveRoomBoardArticle(_fs, id, title, length,
[body](size_t offset, uint8_t* out, size_t count) {
memcpy(out, body + offset, count); return count;
}, version);
delete[] body;
}
if (result != mesh::RoomBoardResult::Success) { error("article save failed or version changed; previous article retained"); return; }
json.raw("{\"ok\":true,\"version\":"); json.number(board.revision + 1);
json.raw("}"); json.finish();
}
void MyMesh::processRoomRequest(const uint8_t* token, uint32_t sequence, char* request,
char* response, size_t capacity) {
room_web_request_authenticated = false;
@@ -527,151 +762,10 @@ void MyMesh::processRoomRequest(const uint8_t* token, uint32_t sequence, char* r
}
char key[65]; mesh::Utils::toHex(key, author.pub_key, PUB_KEY_SIZE);
mesh::RoomJsonWriter json(response, capacity);
if (mesh::handleRoomMailWeb(_fs, author.pub_key,
role == PERM_ACL_READ_WRITE || role == PERM_ACL_ADMIN, role == PERM_ACL_ADMIN,
sequence, getRTCClock()->getCurrentTime(), input.as<JsonObjectConst>(), json,
[this](const uint8_t* identity) { return room_access.allowsIdentity(identity); })) return;
if ((mail_read || mail_write || strncmp(operation, "admin.mail.", 11) == 0)
&& processRoomWebMail(author, role, sequence, input.as<JsonObjectConst>(), json)) return;
if (admin_operation) {
if (strcmp(operation, "admin.settings") == 0) {
json.raw("{\"topic\":"); json.string(room_topic);
json.raw(",\"persistent_history\":"); json.raw(room_history_enabled ? "true" : "false");
json.raw(",\"post_rate\":"); json.number(room_access.postRate());
json.raw(",\"poll_rate\":"); json.number(room_access.pollRate());
json.raw(",\"boot\":"); json.number(room_web_boot_id);
json.raw("}"); json.finish(); return;
}
if (strcmp(operation, "admin.users") == 0) {
if (!input["cursor"].is<uint16_t>() || input["cursor"].as<uint16_t>() > acl.getNumClients()) {
error("invalid user cursor; reload users"); return;
}
const uint16_t cursor = input["cursor"].as<uint16_t>();
uint16_t active = 0, pending = 0, failed = 0;
uint32_t backlog = 0;
for (int i = 0; i < acl.getNumClients(); ++i) {
ClientInfo* client = acl.getClientByIdx(i);
active += client->last_activity != 0 && room_access.allowsIdentity(client->id.pub_key);
backlog += getUnsyncedCount(client);
pending += client->extra.room.pending_ack != 0;
failed += client->extra.room.push_failures != 0 || client->extra.room.topic_failures != 0;
}
json.raw("{\"users\":[");
uint16_t next = cursor;
for (; next < acl.getNumClients() && next < cursor + 2; ++next) {
if (next != cursor) json.raw(",");
writeRoomClientJson(json, acl.getClientByIdx(next), false);
}
json.raw("],\"next\":");
if (next < acl.getNumClients()) json.number(next); else json.raw("null");
json.raw(",\"total\":"); json.number(acl.getNumClients());
json.raw(",\"active\":"); json.number(active);
json.raw(",\"backlog\":"); json.number(backlog);
json.raw(",\"pending\":"); json.number(pending);
json.raw(",\"failed\":"); json.number(failed);
json.raw(",\"boot\":"); json.number(room_web_boot_id);
json.raw("}"); json.finish(); return;
}
char reply[157] = {};
if (strcmp(operation, "admin.topic") == 0) {
const char* value = roomWebString(input["value"], MAX_POST_TEXT_LEN);
const char* expected = roomWebString(input["expected"], MAX_POST_TEXT_LEN);
if (!value || !expected) { error("topic and expected topic required"); return; }
if (strcmp(expected, room_topic) != 0) { error("topic changed; refresh before editing"); return; }
char command[MAX_POST_TEXT_LEN + 11];
snprintf(command, sizeof(command), "set topic %s", value);
if (strcmp(value, room_topic) != 0) handleRoomTopicCommand(command, reply);
} else if (strcmp(operation, "admin.history") == 0) {
if (!input["enabled"].is<bool>() || !input["expected"].is<bool>()) {
error("history setting and expected setting required"); return;
}
if (input["expected"].as<bool>() != room_history_enabled) {
error("history setting changed; refresh before editing"); return;
}
if (input["enabled"].as<bool>() != room_history_enabled) {
handleRoomHistoryCommand(input["enabled"].as<bool>() ? "set room.history on"
: "set room.history off", reply);
}
} else if (strcmp(operation, "admin.rates") == 0) {
if (!input["post_rate"].is<uint16_t>() || !input["poll_rate"].is<uint16_t>()) {
error("rates must be 0-65535"); return;
}
if (!room_access.setRates(_fs, input["post_rate"].as<uint16_t>(), input["poll_rate"].as<uint16_t>())) {
error("rate save failed; unchanged"); return;
}
} else {
const char* target = roomWebString(input["key"], PUB_KEY_SIZE * 2);
uint8_t decoded[PUB_KEY_SIZE];
if (!target || strlen(target) != PUB_KEY_SIZE * 2
|| !mesh::Utils::fromHex(decoded, sizeof(decoded), target)) {
error("complete 64-hex public key required"); return;
}
ClientInfo* client = acl.getClient(decoded, sizeof(decoded));
if (strcmp(operation, "admin.ban") == 0) {
if (!input["banned"].is<bool>()) { error("ban setting required"); return; }
const bool banned = input["banned"].as<bool>();
const auto result = banned ? room_access.addBan(_fs, decoded) : room_access.removeBan(_fs, decoded);
if (result != mesh::RoomAccessPolicy::BanResult::Saved
&& result != mesh::RoomAccessPolicy::BanResult::Unchanged) {
error("ban save failed or ban list full; unchanged"); return;
}
} else {
if (!client) { error("unknown room user; reload users"); return; }
if (strcmp(operation, "admin.user") == 0) {
json.raw("{\"user\":"); writeRoomClientJson(json, client, true);
json.raw(",\"boot\":"); json.number(room_web_boot_id);
json.raw("}"); json.finish(); return;
}
if (strcmp(operation, "admin.route") == 0) {
const char* which = roomWebString(input["which"], 7);
const char* value = roomWebString(input["value"], 191);
const char* expected = roomWebString(input["expected"], 154);
if (!which || !value || !expected
|| (strcmp(which, "outpath") != 0 && strcmp(which, "altpath") != 0)) {
error("path selection, value and expected path required"); return;
}
const bool alternate = strcmp(which, "altpath") == 0;
char current[157];
mesh::formatRoomClientPathReply(alternate ? client->alt_path : client->out_path,
alternate ? client->alt_path_len : client->out_path_len, current, sizeof(current));
if (strcmp(expected, current + 2) != 0) { error("path changed; refresh before editing"); return; }
setRoomClientPath(client, which, value, reply);
} else if (strcmp(operation, "admin.catchup") == 0) {
const char* mode = roomWebString(input["mode"], 6);
if (!input["expected_sync"].is<uint32_t>()
|| input["expected_sync"].as<uint32_t>() != client->extra.room.sync_since) {
error("catch-up position changed; refresh before editing"); return;
}
if (!mode || (strcmp(mode, "before") != 0 && strcmp(mode, "keep") != 0)
|| (strcmp(mode, "before") == 0 ? !input["before"].is<uint32_t>()
: !input["count"].is<uint16_t>())) {
error("use before timestamp or keep count"); return;
}
applyRoomCatchUpCommand(client, mode, strcmp(mode, "before") == 0
? input["before"].as<uint32_t>() : input["count"].as<uint16_t>(), reply);
} else if (strcmp(operation, "admin.access") == 0) {
if (!input["role"].is<uint8_t>() || input["role"].as<uint8_t>() > PERM_ACL_ADMIN
|| !input["expected_role"].is<uint8_t>()
|| input["expected_role"].as<uint8_t>() != (client->permissions & PERM_ACL_ROLE_MASK)) {
error("invalid role or access changed; refresh before editing"); return;
}
const uint8_t selected = input["role"].as<uint8_t>();
if (selected != (client->permissions & PERM_ACL_ROLE_MASK)
|| !client->permissions_are_explicit || selected == PERM_ACL_GUEST) {
const uint8_t permissions = selected == PERM_ACL_GUEST ? 0
: (client->permissions & ~PERM_ACL_ROLE_MASK) | selected;
if (!acl.applyPermissions(self_id, decoded, sizeof(decoded), permissions)) {
error("access change failed; unchanged"); return;
}
mesh::scheduleLazyPersistenceMutation(dirty_contacts_expiry, contacts_save_failures,
futureMillis(LAZY_CONTACTS_WRITE_DELAY));
}
json.raw("{\"ok\":true,\"pending_save\":");
json.raw(dirty_contacts_expiry != 0 ? "true" : "false");
json.raw("}"); json.finish(); return;
}
}
}
if (strncmp(reply, "Err", 3) == 0) { error(reply); return; }
json.raw("{\"ok\":true}"); json.finish(); return;
processRoomWebAdmin(operation, input.as<JsonObjectConst>(), json); return;
}
if (strcmp(operation, "status") == 0) {
bool mailbox_only = false;
@@ -719,73 +813,7 @@ void MyMesh::processRoomRequest(const uint8_t* token, uint32_t sequence, char* r
json.raw("}"); json.finish(); return;
}
mesh::RoomBoardIndex board;
if (!mesh::loadRoomBoard(_fs, board)) { error("information board storage unavailable"); return; }
if (strcmp(operation, "board.index") == 0) {
if (!input["revision"].is<uint32_t>() || !input["cursor"].is<uint8_t>()) { error("invalid board cursor"); return; }
const uint32_t revision = input["revision"].as<uint32_t>();
const uint8_t cursor = input["cursor"].as<uint8_t>();
if (cursor > board.count || (revision != board.revision && (revision != 0 || cursor != 0))) {
error("board changed; reload index"); return;
}
json.raw("{\"revision\":"); json.number(board.revision);
json.raw(",\"articles\":[");
uint8_t next = cursor;
for (; next < board.count && next < cursor + 2; ++next) {
if (next != cursor) json.raw(",");
const auto& article = board.articles[next];
json.raw("{\"id\":"); json.number(article.id);
json.raw(",\"version\":"); json.number(article.version);
json.raw(",\"length\":"); json.number(article.body_length);
json.raw(",\"title\":"); json.string(article.title); json.raw("}");
}
json.raw("],\"next\":"); json.number(next < board.count ? next : 255);
json.raw("}"); json.finish(); return;
}
if (!input["id"].is<uint8_t>() || !input["version"].is<uint32_t>()) { error("invalid article identity/version"); return; }
const uint8_t id = input["id"].as<uint8_t>();
const uint32_t version = input["version"].as<uint32_t>();
const mesh::RoomBoardArticle* article = nullptr;
for (uint8_t i = 0; i < board.count; ++i) if (board.articles[i].id == id) article = &board.articles[i];
if (strcmp(operation, "board.read") == 0) {
if (!input["offset"].is<uint16_t>()) { error("invalid article offset"); return; }
const uint16_t offset = input["offset"].as<uint16_t>();
uint8_t chunk[mesh::ROOM_BOARD_READ_MAX_CHUNK];
size_t length = 0;
const auto result = mesh::readRoomBoardArticle(_fs, id, version, offset, chunk, sizeof(chunk), length);
if (result != mesh::RoomBoardResult::Success || !article) { error("article changed or unavailable; reload index"); return; }
char encoded[4 * ((sizeof(chunk) + 2) / 3) + 1]; mesh::roomEncodeBase64(chunk, length, encoded);
json.raw("{\"version\":"); json.number(version);
json.raw(",\"offset\":"); json.number(offset);
json.raw(",\"next\":"); json.number(uint32_t(offset) + length);
json.raw(",\"total\":"); json.number(article->body_length);
json.raw(",\"data64\":"); json.string(encoded); json.raw("}"); json.finish(); return;
}
mesh::RoomBoardResult result;
if (strcmp(operation, "board.delete") == 0) {
if (version == 0) { error("deletion requires the current article version"); return; }
result = mesh::deleteRoomBoardArticle(_fs, id, version);
} else {
const char* title = roomWebString(input["title"], 63);
const char* encoded = roomWebString(input["body64"], 2732);
if (!title || !encoded || (article ? article->version != version : version != 0)) {
error("article changed; reload index before editing"); return;
}
auto* body = new (std::nothrow) uint8_t[mesh::ROOM_BOARD_MAX_BODY_LENGTH];
if (!body) { error("insufficient memory; article retained"); return; }
size_t length = 0;
if (!mesh::roomDecodeBase64(encoded, body, mesh::ROOM_BOARD_MAX_BODY_LENGTH, length)) {
delete[] body; error("invalid article encoding"); return;
}
result = mesh::saveRoomBoardArticle(_fs, id, title, length,
[body](size_t offset, uint8_t* out, size_t count) {
memcpy(out, body + offset, count); return count;
}, version);
delete[] body;
}
if (result != mesh::RoomBoardResult::Success) { error("article save failed or version changed; previous article retained"); return; }
json.raw("{\"ok\":true,\"version\":"); json.number(board.revision + 1);
json.raw("}"); json.finish();
processRoomWebBoard(operation, input.as<JsonObjectConst>(), json);
}
#endif
+9
View File
@@ -87,6 +87,9 @@ namespace mesh { class RoomJsonWriter; }
#if defined(ESP_PLATFORM) && defined(ADMIN_PASSWORD) && !defined(WEBCONFIG_DISABLED)
#include "helpers/esp32/WebConfigServer.h"
#endif
#ifdef WITH_WEBCONFIG
#include <ArduinoJson.h>
#endif
/* ------------------------------ Config -------------------------------- */
@@ -358,6 +361,12 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks,
uint8_t recent[4][8] = {};
} room_web_password_limit;
bool handleRoomWebCommand(const char* command, char* reply);
bool processRoomWebMail(const mesh::Identity& author, uint8_t role, uint32_t sequence,
JsonObjectConst input, mesh::RoomJsonWriter& json) __attribute__((noinline));
void processRoomWebAdmin(const char* operation, JsonObjectConst input,
mesh::RoomJsonWriter& json) __attribute__((noinline));
void processRoomWebBoard(const char* operation, JsonObjectConst input,
mesh::RoomJsonWriter& json) __attribute__((noinline));
bool _unconfigured_setup_espnow_suspended = false;
void suspendUnconfiguredSetupBridges();
bool startWebConfigImpl(bool force_ap, char* reply, bool automatic_setup);
+18 -7
View File
@@ -119,9 +119,20 @@ function makeIdentity(){
}
function message(error){$('error').textContent=error?String(error.message||error):''}
const pause=ms=>new Promise(resolve=>setTimeout(resolve,ms));
async function fetchBounded(url,options){
const controller=new AbortController(), timer=setTimeout(()=>controller.abort(),5000);
try{return await fetch(url,Object.assign({},options,{signal:controller.signal,cache:'no-store'}))}
async function fetchBounded(url,options,received,timeout){
const controller=new AbortController(), timer=setTimeout(()=>controller.abort(),timeout);
try{
const response=await fetch(url,Object.assign({},options,{signal:controller.signal,cache:'no-store'}));
received(response);
// Drain admission, pending and busy replies too. The deadline covers the
// body, so stalled reads cannot keep a connection or the request chain open.
let result;
try{result=await response.json()}catch(e){
if(controller.signal.aborted)throw e;
const error=Error('Invalid response; refresh to check before retrying.');error.invalidResponse=true;throw error;
}
return {ok:response.ok,status:response.status,result:result};
}
finally{clearTimeout(timer)}
}
function api(op,values={}){
@@ -144,12 +155,12 @@ async function request(op,values,generation,secret){
if(generation!==connectionGeneration)throw Error('Room connection changed. Reconnect before trying again.');
let response;
try{response=await fetchBounded(accepted?'/api/room/result':'/api/room',
{method:'POST',headers:headers,body:accepted?undefined:body})}
catch(e){await pause(400);continue}
{method:'POST',headers:headers,body:accepted?undefined:body},
response=>{if(response.status===202)accepted=true},Math.min(5000,deadline-Date.now()))}
catch(e){if(e.invalidResponse)throw e;await pause(400);continue}
if(!accepted&&response.status===429){await pause(500);continue}
if(response.status===202){accepted=true;await pause(250);continue}
let result;
try{result=await response.json()}catch(e){throw Error('Invalid response; refresh to check before retrying.')}
const result=response.result;
if(generation!==connectionGeneration)throw Error('Room connection changed. Reconnect before trying again.');
if(!response.ok||result.error)throw Error(result.error||('Room request failed ('+response.status+').'));
return result;
+17
View File
@@ -168,6 +168,10 @@ struct MyMesh {
}
Clock* getRTCClock() { return &clock; }
bool handleRoomWebCommand(const char*, char*);
bool processRoomWebMail(const mesh::Identity&, uint8_t, uint32_t, JsonObjectConst,
mesh::RoomJsonWriter&) __attribute__((noinline));
void processRoomWebAdmin(const char*, JsonObjectConst, mesh::RoomJsonWriter&) __attribute__((noinline));
void processRoomWebBoard(const char*, JsonObjectConst, mesh::RoomJsonWriter&) __attribute__((noinline));
void processRoomRequest(const uint8_t*, uint32_t, char*, char*, size_t);
void serviceRoomQuotas();
bool storePost(const mesh::Identity&, const char*);
@@ -928,6 +932,19 @@ static void mailGlobalQueueCases() {
}
int main() {
unsigned scenarios = 0;
{
// The actual page connects, refreshes status/chat, then loads an empty
// board. On ESP32 that last read used to overflow the loop task stack.
MyMesh m; enable(m);
assert(result(m, request("status", "admin-secret"), 1)["role"] == PERM_ACL_ADMIN);
assert(result(m, request("status", "admin-secret"), 2)["role"] == PERM_ACL_ADMIN);
assert(result(m, request("posts", "admin-secret", ",\"after\":0"), 3)["post"].isNull());
const auto board = result(m, request("board.index", "admin-secret", ",\"revision\":0,\"cursor\":0"), 4);
assert(board["revision"] == 0 && board["articles"].size() == 0 && board["next"] == 255);
assert(m.room_web_request_authenticated && m._num_posted == 0);
printf("WEB_OPERATION_DISPATCH: page startup reaches an empty information board without posting passed\n");
++scenarios;
}
{
MyMesh m; assert(!m.room_web_enabled);
error(m, request("status"), 1, "disabled"); assert(floors(m) == 0);
+25
View File
@@ -29,6 +29,8 @@ class RoomWebServiceTest(unittest.TestCase):
header = (ROOT / "examples/simple_room_server/MyMesh.h").read_text()
production = "\n".join(extract_braced(source, signature) for signature in (
"bool MyMesh::handleRoomWebCommand(", "static const char* roomWebString(",
"bool MyMesh::processRoomWebMail(", "void MyMesh::processRoomWebAdmin(",
"void MyMesh::processRoomWebBoard(",
"void MyMesh::processRoomRequest(", "void MyMesh::serviceRoomQuotas(",
"bool MyMesh::storePost(", "bool MyMesh::snapshotRoomHistory(",
"bool MyMesh::executeClientPathCommand(", "bool MyMesh::setRoomClientPath(",
@@ -82,6 +84,7 @@ class RoomWebServiceTest(unittest.TestCase):
checked = subprocess.run([str(binary)], capture_output=True, text=True, timeout=60)
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
self.assertIn("actual room web service", checked.stdout)
self.assertIn("WEB_OPERATION_DISPATCH:", checked.stdout)
for group in ("ADMIN_AUTH", "PASSWORD_LIMIT", "ADMIN_ROUTES", "ADMIN_CATCHUP", "ADMIN_USERS",
"ADMIN_SETTINGS", "ADMIN_INVALID", "MAIL_AUTH", "MAIL_DELIVERY",
"MAIL_ADMIN", "MAIL_PAGING", "MAIL_STORAGE", "MAIL_GLOBAL"):
@@ -93,6 +96,28 @@ class RoomWebServiceTest(unittest.TestCase):
expected = json.loads((FIXTURES / "boundary.json").read_text())[kind.lower()]
self.assertEqual(actual, expected)
print(checked.stdout.splitlines()[-1])
# A native frame budget catches accidental reinlining of every
# operation's scratch arrays into the common callback. This is
# separate from sanitizer instrumentation, which inflates frames.
# ESP32 ELF frames and the real filesystem call chain must still
# be checked against its unchanged 8192-byte loop task stack.
stack_object = work / "room-web-stack.o"
stack_args = [argument for argument in built.args[:-2]
if argument not in sanitizers]
stack_built = subprocess.run([*stack_args, "-Os", "-fstack-usage", "-c",
"-o", str(stack_object)],
capture_output=True, text=True, timeout=60)
self.assertEqual(stack_built.returncode, 0, stack_built.stdout + stack_built.stderr)
usage = "\n".join(path.read_text() for path in work.glob("*.su"))
frames = {}
for method, budget in (("processRoomRequest", 1024), ("processRoomWebAdmin", 1536),
("processRoomWebBoard", 2048), ("processRoomWebMail", 3072)):
sizes = re.findall(r"(?:void|bool) MyMesh::" + method
+ r"\([^\t\n]*\)\s*\t(\d+)\t[^\n]*", usage)
self.assertEqual(len(sizes), 1, "missing or ambiguous stack frame: " + method)
frames[method] = int(sizes[0])
self.assertLessEqual(frames[method], budget, method + " frame exceeds its host regression budget")
print("room web optimized host stack bytes: " + json.dumps(frames, sort_keys=True))
if __name__ == "__main__":
+156
View File
@@ -152,6 +152,162 @@ window.addEventListener('load',()=>{(async()=>{
self.assertEqual(self.attribute(dom, "attempts"), "3")
self.assertEqual(self.attribute(dom, "draft"), "keep this draft")
def test_admission_pending_and_busy_bodies_drain_before_the_next_request(self):
dom = self.run_page(r'''
let attempts=0,polls=0,unread=0,overlap=0,drained=[],first=null;
const drainResponse=(value,status)=>{
unread++;
return Promise.resolve({ok:status>=200&&status<300,status:status,json:()=>new Promise(resolve=>{
setTimeout(()=>{unread--;drained.push(status);resolve(value)},20);
})});
};
window.fetch=(url,options)=>{
if(unread){overlap++;throw Error('previous response body was not drained')}
const p=record(url,options),current=calls[calls.length-1];
if(url==='/api/room/result'){
polls++;mark('result-sequence',current.seq===first.seq&&current.token===first.token);
return polls===1?drainResponse({pending:true},202):drainResponse({error:'post not retained'},200);
}
if(p.op==='status')return response(boundary.status);
if(p.op==='posts')return response({post:null});
if(p.op==='board.index')return response({revision:0,articles:[],next:255});
if(p.op==='post'){
attempts++;
if(attempts===1){first=current;return drainResponse({error:'room service busy'},429)}
mark('same-intent',current.seq===first.seq&&current.body===first.body&&current.token===first.token);
return drainResponse({accepted:true},202);
}
throw Error('unexpected operation');
};
window.addEventListener('load',()=>{(async()=>{
submit('join');await until(()=>$('notices').textContent==='No notices published.');
$('message').value='keep this unsent draft';$('message').dispatchEvent(new Event('input'));submit('compose');
await until(()=>$('error').textContent==='post not retained');
mark('overlap',overlap);mark('unread',unread);mark('drained',JSON.stringify(drained));
mark('attempts',attempts);mark('polls',polls);mark('draft',$('message').value);mark('done',true);
})().catch(e=>mark('js-error',e.message))});
''')
self.assertEqual(self.attribute(dom, "overlap"), "0")
self.assertEqual(self.attribute(dom, "unread"), "0")
self.assertEqual(json.loads(self.attribute(dom, "drained")), [429, 202, 202, 200])
self.assertEqual(self.attribute(dom, "same-intent"), "true")
self.assertEqual(self.attribute(dom, "result-sequence"), "true")
self.assertEqual(self.attribute(dom, "attempts"), "2")
self.assertEqual(self.attribute(dom, "polls"), "2")
self.assertEqual(self.attribute(dom, "draft"), "keep this unsent draft")
def test_stalled_admission_body_aborts_and_only_polls_the_accepted_sequence(self):
dom = self.run_page(r'''
let attempts=0,polls=0,aborts=0,abortElapsed=0,first=null;
window.fetch=(url,options)=>{
const p=record(url,options),current=calls[calls.length-1];
if(url==='/api/room/result'){
polls++;mark('result-sequence',current.seq===first.seq&&current.token===first.token);
return response({error:'post not retained'});
}
if(p.op==='status')return response(boundary.status);
if(p.op==='posts')return response({post:null});
if(p.op==='board.index')return response({revision:0,articles:[],next:255});
if(p.op==='post'){
attempts++;first=current;const started=Date.now();
return Promise.resolve({ok:true,status:202,json:()=>new Promise((resolve,reject)=>{
options.signal.addEventListener('abort',()=>{aborts++;abortElapsed=Date.now()-started;reject(new DOMException('body stalled','AbortError'))},{once:true});
})});
}
throw Error('unexpected operation');
};
window.addEventListener('load',()=>{(async()=>{
submit('join');await until(()=>$('notices').textContent==='No notices published.');
$('message').value='draft across lost admission body';$('message').dispatchEvent(new Event('input'));submit('compose');
await new Promise(resolve=>setTimeout(resolve,1000));mark('during-stall-draft',$('message').value);
for(let i=0;i<700&&$('error').textContent!=='post not retained';i++)await new Promise(resolve=>setTimeout(resolve,10));
mark('error',$('error').textContent);mark('aborts',aborts);mark('abort-ms',abortElapsed);
mark('attempts',attempts);mark('polls',polls);mark('draft',$('message').value);mark('done',true);
})().catch(e=>mark('js-error',e.message))});
''', virtual_time=9500)
self.assertEqual(self.attribute(dom, "error"), "post not retained")
self.assertEqual(self.attribute(dom, "aborts"), "1")
self.assertGreaterEqual(int(self.attribute(dom, "abort-ms")), 4900)
self.assertLessEqual(int(self.attribute(dom, "abort-ms")), 5100)
self.assertEqual(self.attribute(dom, "attempts"), "1")
self.assertEqual(self.attribute(dom, "polls"), "1")
self.assertEqual(self.attribute(dom, "result-sequence"), "true")
self.assertEqual(self.attribute(dom, "during-stall-draft"), "draft across lost admission body")
self.assertEqual(self.attribute(dom, "draft"), "draft across lost admission body")
def test_stalled_busy_and_pending_bodies_keep_the_original_write_intent(self):
dom = self.run_page(r'''
let attempts=0,polls=0,aborts=0,first=null;
const stalled=(options,status)=>Promise.resolve({ok:status>=200&&status<300,status:status,json:()=>new Promise((resolve,reject)=>{
options.signal.addEventListener('abort',()=>{aborts++;reject(new DOMException('body stalled','AbortError'))},{once:true});
})});
window.fetch=(url,options)=>{
const p=record(url,options),current=calls[calls.length-1];
if(url==='/api/room/result'){
polls++;mark('result-sequence',current.seq===first.seq&&current.token===first.token);
return polls===1?stalled(options,202):response({error:'post not retained'});
}
if(p.op==='status')return response(boundary.status);
if(p.op==='posts')return response({post:null});
if(p.op==='board.index')return response({revision:0,articles:[],next:255});
if(p.op==='post'){
attempts++;
if(attempts===1){first=current;return stalled(options,429)}
mark('same-intent',current.seq===first.seq&&current.body===first.body&&current.token===first.token);
return response({accepted:true},202);
}
throw Error('unexpected operation');
};
window.addEventListener('load',()=>{(async()=>{
submit('join');await until(()=>$('notices').textContent==='No notices published.');
$('message').value='draft across busy and pending bodies';$('message').dispatchEvent(new Event('input'));submit('compose');
for(let i=0;i<1300&&$('error').textContent!=='post not retained';i++)await new Promise(resolve=>setTimeout(resolve,10));
mark('error',$('error').textContent);mark('aborts',aborts);mark('attempts',attempts);mark('polls',polls);
mark('draft',$('message').value);mark('done',true);
})().catch(e=>mark('js-error',e.message))});
''', virtual_time=14500)
self.assertEqual(self.attribute(dom, "error"), "post not retained")
self.assertEqual(self.attribute(dom, "aborts"), "2")
self.assertEqual(self.attribute(dom, "same-intent"), "true")
self.assertEqual(self.attribute(dom, "result-sequence"), "true")
self.assertEqual(self.attribute(dom, "attempts"), "2")
self.assertEqual(self.attribute(dom, "polls"), "2")
self.assertEqual(self.attribute(dom, "draft"), "draft across busy and pending bodies")
def test_stalled_result_bodies_reach_confirmation_deadline_without_losing_draft(self):
dom = self.run_page(r'''
let attempts=0,polls=0,aborts=0,started=0,first=null;
window.fetch=(url,options)=>{
const p=record(url,options),current=calls[calls.length-1];
if(url==='/api/room/result'){
polls++;mark('result-sequence',current.seq===first.seq&&current.token===first.token);
return Promise.resolve({ok:true,status:200,json:()=>new Promise((resolve,reject)=>{
options.signal.addEventListener('abort',()=>{aborts++;reject(new DOMException('body stalled','AbortError'))},{once:true});
})});
}
if(p.op==='status')return response(boundary.status);
if(p.op==='posts')return response({post:null});
if(p.op==='board.index')return response({revision:0,articles:[],next:255});
if(p.op==='post'){attempts++;first=current;started=Date.now();return response({accepted:true},202)}
throw Error('unexpected operation');
};
window.addEventListener('load',()=>{(async()=>{
submit('join');await until(()=>$('notices').textContent==='No notices published.');
$('message').value='keep draft until confirmation';$('message').dispatchEvent(new Event('input'));submit('compose');
for(let i=0;i<2200&&!$('error').textContent.includes('No confirmation');i++)await new Promise(resolve=>setTimeout(resolve,10));
mark('elapsed-ms',Date.now()-started);mark('error',$('error').textContent);mark('aborts',aborts);
mark('attempts',attempts);mark('polls',polls);mark('draft',$('message').value);mark('done',true);
})().catch(e=>mark('js-error',e.message))});
''', virtual_time=24500)
self.assertIn("No confirmation received", self.attribute(dom, "error"))
self.assertGreaterEqual(int(self.attribute(dom, "elapsed-ms")), 20000)
self.assertLess(int(self.attribute(dom, "elapsed-ms")), 21000)
self.assertEqual(self.attribute(dom, "aborts"), "4")
self.assertEqual(self.attribute(dom, "attempts"), "1")
self.assertEqual(self.attribute(dom, "polls"), "4")
self.assertEqual(self.attribute(dom, "result-sequence"), "true")
self.assertEqual(self.attribute(dom, "draft"), "keep draft until confirmation")
def test_chunk_resume_version_restart_and_full_utf8_publication(self):
dom = self.run_page(r'''
let currentVersion=1,lostChunk=true,oldOffsets=[],newOffsets=[],publications=[],indexCount=0;