diff --git a/src/helpers/MQTTTopicTemplate.h b/src/helpers/MQTTTopicTemplate.h index 7f50abf9c..e0af4d76c 100644 --- a/src/helpers/MQTTTopicTemplate.h +++ b/src/helpers/MQTTTopicTemplate.h @@ -48,5 +48,9 @@ static inline bool mqttSubstituteTopic(const char* tmpl, const char* iata, } } buf[out] = '\0'; + // The loop also stops when the output buffer is full. If input remains, + // report overflow just as we do for an oversized placeholder substitution; + // callers must never publish a silently truncated topic. + if (*p) return false; return out > 0; } diff --git a/src/helpers/esp32/WebConfigServer.cpp b/src/helpers/esp32/WebConfigServer.cpp index 592877a0c..04aaaebac 100644 --- a/src/helpers/esp32/WebConfigServer.cpp +++ b/src/helpers/esp32/WebConfigServer.cpp @@ -214,7 +214,7 @@ void WebConfigServer::finalizeTeardown() { // listener socket is released by _server->end() on stop) and route requests // through the currently attached session, so nothing a live request points at // is ever freed. - + _server = NULL; delete _dns; _dns = NULL; diff --git a/test/test_topic_template/test_topic_template.cpp b/test/test_topic_template/test_topic_template.cpp index b474dc9d1..5ebb553dc 100644 --- a/test/test_topic_template/test_topic_template.cpp +++ b/test/test_topic_template/test_topic_template.cpp @@ -63,14 +63,26 @@ TEST(TopicTemplate, OverflowReturnsFalseNoWrite) { EXPECT_FALSE(mqttSubstituteTopic("{device}", IATA, DEV, TOK, "status", buf, sizeof(buf))); } -TEST(TopicTemplate, LiteralOverflowTruncatesAndNulTerminates) { +TEST(TopicTemplate, LiteralOverflowReturnsFalseAndNulTerminates) { char buf[5]; // Literal longer than the buffer: fills up to buf_size-1 and NUL-terminates. - mqttSubstituteTopic("abcdefghij", IATA, DEV, TOK, "status", buf, sizeof(buf)); + EXPECT_FALSE(mqttSubstituteTopic("abcdefghij", IATA, DEV, TOK, "status", buf, sizeof(buf))); EXPECT_EQ('\0', buf[4]); EXPECT_EQ((size_t)4, strlen(buf)); } +TEST(TopicTemplate, LiteralSuffixOverflowAfterSubstitutionReturnsFalse) { + char buf[8]; + EXPECT_FALSE(mqttSubstituteTopic("{iata}/tail", IATA, DEV, TOK, "status", buf, sizeof(buf))); + EXPECT_STREQ("DEN/tai", buf); +} + +TEST(TopicTemplate, ExactFitLiteralSucceeds) { + char buf[5]; + EXPECT_TRUE(mqttSubstituteTopic("abcd", IATA, DEV, TOK, "status", buf, sizeof(buf))); + EXPECT_STREQ("abcd", buf); +} + TEST(TopicTemplate, AlwaysNulTerminatedAndBounded) { // Fuzz-ish: many buffer sizes never overrun and always NUL-terminate. const char* tmpl = "meshcore/{iata}/{device}/{token}/{type}/tail"; diff --git a/webui/index.html b/webui/index.html index 6334192f0..cb6e5e828 100644 --- a/webui/index.html +++ b/webui/index.html @@ -664,7 +664,7 @@ function saveChanges(extra,cb){ if(Object.keys(setmap).length===0&&!body.reboot){toast("Nothing to save");return} var reqid=mkReqId();body.reqid=reqid; $("#save-btn").disabled=true; - post("/api/config",body).then(function(){pollResult(cb,0,0,true,reqid)}) + post("/api/config",body).then(function(){pollResult(cb,0,0,true,reqid,setmap)}) .catch(function(e){ if(e.message==="auth"){$("#save-btn").disabled=false;if(cb)cb(null,false);return} // Definite HTTP rejection: the batch was NOT accepted, so there is no @@ -677,7 +677,7 @@ function saveChanges(extra,cb){ // 409 busy: a batch is in flight. If it's ours (same reqid — our POST // landed but its 202 was lost) poll it; otherwise another client owns it. if(e.status===409){ - if(e.reqid===reqid){pollResult(cb,0,0,true,reqid);return} + if(e.reqid===reqid){pollResult(cb,0,0,true,reqid,setmap);return} $("#save-btn").disabled=false; if(cb)cb(null,false);else toast("Another change is being applied — retry shortly"); return; @@ -685,7 +685,7 @@ function saveChanges(extra,cb){ // Ambiguous network failure: the POST may have landed even though its // response was lost (the save itself briefly stalls the AP's WiFi) — poll // for the result regardless, matching it by reqid. - pollResult(cb,0,0,false,reqid); + pollResult(cb,0,0,false,reqid,setmap); }); } // App "Save changes" button. A WiFi SSID/password change only takes effect @@ -718,7 +718,7 @@ function appSave(){ // acked: the node returned 202 for the POST, so the batch WILL be applied and // (for reboot saves) the node reboots on its own even if we never read the // result. Lets callers distinguish "confirmation lost" from "save never sent". -function pollResult(cb,errs,idles,acked,reqid){ +function pollResult(cb,errs,idles,acked,reqid,submitted){ errs=errs||0;idles=idles||0; function fail(){$("#save-btn").disabled=false; if(cb)cb(null,acked);else toast(acked?"Saved, but confirmation was lost — reload to verify":"Save failed — check connection and retry")} @@ -728,11 +728,11 @@ function pollResult(cb,errs,idles,acked,reqid){ // save (a concurrent client, or a stale DONE from before our POST) — never // consume it as our own success. if(r.reqid!==reqid){fail();return} - if(r.state==="pending"){setTimeout(function(){pollResult(cb,0,idles,true,reqid)},400);return} + if(r.state==="pending"){setTimeout(function(){pollResult(cb,0,idles,true,reqid,submitted)},400);return} if(r.state!=="done"){ // idle: the POST likely never arrived — but give it a moment in case // it's still in flight after a connection blip - if(idles<4){setTimeout(function(){pollResult(cb,errs,idles+1,acked,reqid)},700);return} + if(idles<4){setTimeout(function(){pollResult(cb,errs,idles+1,acked,reqid,submitted)},700);return} fail();return; } $("#save-btn").disabled=false; @@ -740,9 +740,12 @@ function pollResult(cb,errs,idles,acked,reqid){ (r.results||[]).forEach(function(it){ var ok=/^OK/i.test(it.reply); if(!ok)fails++; + // For an ordinary app save, clear only a value that was accepted and has + // not been edited again while the request was in flight. Rejected and + // newer values stay dirty so the operator can correct/retry them. + if(!cb&&ok&&submitted&&(it.key in submitted)&&st.dirty[it.key]===submitted[it.key])delete st.dirty[it.key]; if(it.key==="radio"){ if(/reboot/i.test(it.reply))radioReboot=true; - $$("#v-app [data-rg]").forEach(function(o){o.classList.remove("dirty")}); } var el=document.querySelector('#v-app [data-k="'+it.key+'"]'); if(el)chipFor(el,ok,ok?"OK":it.reply.replace(/^Err(or)?[:,]?\s*/i,"")); @@ -751,7 +754,6 @@ function pollResult(cb,errs,idles,acked,reqid){ if(cb){cb(r);return} if(fails)toast(fails+" setting(s) rejected — see fields"); else toast("Saved ✓"); - st.dirty={}; loadConfigSoft(); }).catch(function(e){ if(e.message==="auth"){fail();return} @@ -761,16 +763,30 @@ function pollResult(cb,errs,idles,acked,reqid){ // Transient network error: the save itself can knock the phone off the // AP for a moment (flash writes stall WiFi). Keep polling — the device // holds the result until we manage to read it. - if(errs<20){setTimeout(function(){pollResult(cb,errs+1,idles,acked,reqid)},700);return} + if(errs<20){setTimeout(function(){pollResult(cb,errs+1,idles,acked,reqid,submitted)},700);return} fail(); }); } -function loadConfigSoft(){ // re-sync origin values without clobbering result chips +function loadConfigSoft(){ // re-sync accepted values without clobbering chips/rejected edits api("/api/config").then(function(c){ st.cfg=c; - $$("[data-k]").forEach(function(el){var k=el.dataset.k;st.orig[k]=cfgVal(k);setEl(el,st.orig[k]);el.classList.remove("dirty")}); - $$("[data-rg]").forEach(function(el){el.value=String(c.radio[el.dataset.rg])}); - st.orig.radio=radioCombo(); + $$("[data-k]").forEach(function(el){ + var k=el.dataset.k,stored=cfgVal(k); + st.orig[k]=stored; + if((k in st.dirty)&&st.dirty[k]!==stored){setEl(el,st.dirty[k]);el.classList.add("dirty")} + else{delete st.dirty[k];setEl(el,stored);el.classList.remove("dirty")} + }); + var storedRadio=[c.radio.freq,c.radio.bw,c.radio.sf,c.radio.cr].join(","); + st.orig.radio=storedRadio; + var keepRadio=("radio" in st.dirty)&&!comboEq(st.dirty.radio,storedRadio); + if(!keepRadio)delete st.dirty.radio; + var radioParts=(keepRadio?st.dirty.radio:storedRadio).split(","); + $$("[data-rg]").forEach(function(el){ + var idx={freq:0,bw:1,sf:2,cr:3}[el.dataset.rg]; + el.value=radioParts[idx];el.classList.toggle("dirty",keepRadio); + }); + for(var i=1;i<=st.nslots;i++){refreshSlotFields($("#app-slots"),i);refreshSlotFields($("#wz-slots"),i)} + afHint(); updateSaveBar(); }).catch(function(){}); }