Skip to content

Commit a032f61

Browse files
committed
security audit on log forging
1 parent ba57db8 commit a032f61

File tree

5 files changed

+45
-76
lines changed

5 files changed

+45
-76
lines changed

library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "THiNX",
3-
"keywords": "json, rest, http, web",
3+
"keywords": "json, rest, http, web, thinx, ota, management, mqtt, esp8266",
44
"description": "THiNX Device Management for ESP8266",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/suculent/thinx-firmware-esp8266"
88
},
9-
"version": "2.1.171",
9+
"version": "2.1.172",
1010
"authors": {
1111
"name": "Matej Sychra",
1212
"url": "https://thinx.cloud"

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=THiNX
2-
version=2.1.171
2+
version=2.1.172
33
author=Matej Sychra
44
maintainer=Matej Sychra
55
sentence=A library to wrap THiNX device registration, MQTT and ESP8266HttpUpdate/ArduinoOTA

src/THiNXLib.cpp

Lines changed: 37 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C" {
1616
#endif
1717

1818
#ifndef THINX_COMMIT_ID
19-
#define THINX_COMMIT_ID "c680610df8457bb634cbaa80ce00b2b761208347"
19+
#define THINX_COMMIT_ID "0"
2020
#endif
2121

2222
char* THiNX::thinx_api_key;
@@ -177,7 +177,7 @@ THiNX::THiNX(const char * __apikey, const char * __owner_id) {
177177
void THiNX::initWithAPIKey(const char * __apikey) {
178178

179179
#ifdef __USE_SPIFFS__
180-
Serial.println(F("*TH: Checking FS..."));
180+
Serial.println(F("*TH: Checking filesystem, please don't turn off or reset the device now..."));
181181
if (!fsck()) {
182182
Serial.println(F("*TH: Filesystem check failed, disabling THiNX."));
183183
return;
@@ -456,13 +456,12 @@ void THiNX::senddata(String body) {
456456

457457
void THiNX::parse(String payload) {
458458

459-
// TODO: Should parse response only for this device_id (which must be internal and not a mac)
459+
// TODO: Should parse response only for this device
460460

461461
payload_type ptype = Unknown;
462462

463463
int start_index = 0;
464464
int endIndex = payload.length();
465-
466465
int reg_index = payload.indexOf("{\"registration\"");
467466
int upd_index = payload.indexOf("{\"FIRMWARE_UPDATE\"");
468467
int not_index = payload.indexOf("{\"notification\"");
@@ -525,33 +524,38 @@ void THiNX::parse(String payload) {
525524
Serial.println(String("mac: ") + mac);
526525

527526
if (!mac.equals(this_mac)) {
528-
Serial.println(F("*TH: Warning: firmware is dedicated to device with different MAC."));
527+
Serial.println(F("*TH: Firmware is dedicated to device with different MAC. Skipping update."));
528+
Serial.print("Local MAC: "); Serial.println(this_mac);
529+
Serial.print("Remote MAC: "); Serial.println(mac);
530+
return;
529531
}
530532

531533
String udid = root["udid"];
532534
if ( udid.length() > 4 ) {
533535
thinx_udid = strdup(udid.c_str());
534-
Serial.println(String("thinx_udid: ") + thinx_udid);
536+
Serial.println(String("Update for thinx_udid: ") + thinx_udid);
535537
}
536538

537539
// Check current firmware based on commit id and store Updated state...
538540
String commit = update["commit"];
539-
Serial.println(String("commit: ") + commit);
541+
Serial.println(String("Update commit: ") + commit);
540542

541543
// Check current firmware based on version and store Updated state...
542544
String version = update["version"];
543-
Serial.println(String("version: ") + version);
544-
545-
//if ((commit == thinx_commit_id) && (version == thinx_version_id)) { WHY?
546-
if (strlen(available_update_url) > 5) {
547-
Serial.println(F("*TH: firmware has same thx_commit_id as current and update availability is stored. Firmware has been installed."));
548-
available_update_url = strdup("");
549-
notify_on_successful_update();
550-
return;
551-
} else {
552-
Serial.println(F("*TH: Info: firmware has same thx_commit_id as current and no update is available."));
545+
Serial.println(String("Update version: ") + version);
546+
547+
// Well, the backend should not actually provide firmware when these two things are same,
548+
// following block may deprecate.
549+
if ((commit.equals(thinx_commit_id)) && (version.equals(thinx_version_id))) {
550+
if (strlen(available_update_url) > 5) {
551+
Serial.println(F("*TH: firmware has same thx_commit_id as current and update availability is stored. Firmware has been installed."));
552+
available_update_url = strdup("");
553+
notify_on_successful_update();
554+
return;
555+
} else {
556+
Serial.println(F("*TH: Info: firmware has same thx_commit_id as current and no update is available."));
557+
}
553558
}
554-
//}
555559

556560
save_device_info();
557561

@@ -852,11 +856,15 @@ String THiNX::date(const char* optional_format) {
852856
*/
853857

854858
const char * THiNX::thinx_mac() {
859+
855860
#if defined(ESP8266)
856-
sprintf(mac_string, "5CCF7F%6X", ESP.getChipId());
857-
#else
861+
sprintf(mac_string, "5CCF7F%6X", ESP.getChipId()); // ESP8266 only!
862+
#endif
863+
864+
#if defined(ESP32)
858865
sprintf(mac_string, "5CCF7F%6X", ESP.getEfuseMac());
859866
#endif
867+
860868
return mac_string;
861869
}
862870

@@ -873,7 +881,7 @@ void THiNX::notify_on_successful_update() {
873881
);
874882
mqtt_client->loop();
875883
} else {
876-
Serial.println(F("*TH: Device updated but MQTT not active to notify. TODO: Store."));
884+
Serial.println(F("*TH: Device updated but MQTT not active to notify.")); // TODO: Store as boot status
877885
}
878886
}
879887

@@ -1000,8 +1008,6 @@ bool THiNX::start_mqtt() {
10001008
}
10011009

10021010
} else {
1003-
Serial.println(F("*TH: MQTT Type: String or JSON..."));
1004-
Serial.println(pub.payload_string());
10051011
parse(pub.payload_string());
10061012
if (_mqtt_callback) {
10071013
_mqtt_callback(pub.payload_string());
@@ -1025,8 +1031,6 @@ bool THiNX::start_mqtt() {
10251031

10261032
void THiNX::restore_device_info() {
10271033

1028-
Serial.println(F("*TH: Restoring device info..."));
1029-
10301034
int json_end = 0;
10311035

10321036
#ifndef __USE_SPIFFS__
@@ -1067,21 +1071,18 @@ void THiNX::restore_device_info() {
10671071

10681072
// Validating bracket count
10691073
if (json_end != 0) {
1070-
Serial.println(F("*TH: JSON invalid... bailing out."));
1074+
// Serial.println(F("*TH: JSON invalid... bailing out."));
10711075
return;
10721076
}
10731077

1074-
Serial.println(F("*TH: Converting data to String..."));
1075-
10761078
#else
10771079
if (!SPIFFS.exists("/thx.cfg")) {
1078-
Serial.println(F("*TH: No saved configuration."));
1080+
// Serial.println(F("*TH: No saved configuration."));
10791081
return;
10801082
}
10811083
File f = SPIFFS.open("/thx.cfg", "r");
1082-
Serial.println(F("*TH: Found persistent data..."));
10831084
if (!f) {
1084-
Serial.println(F("*TH: No remote configuration found so far..."));
1085+
// Serial.println(F("*TH: No remote configuration found so far..."));
10851086
return;
10861087
}
10871088
if (f.size() == 0) {
@@ -1092,9 +1093,6 @@ void THiNX::restore_device_info() {
10921093
f.readBytesUntil('\n', json_info, 511);
10931094
#endif
10941095

1095-
Serial.println(json_info);
1096-
Serial.println(F("*TH: Parsing..."));
1097-
10981096
DynamicJsonBuffer jsonBuffer(512);
10991097
JsonObject& config = jsonBuffer.parseObject((char*)json_info); // must not be String!
11001098

@@ -1104,12 +1102,8 @@ void THiNX::restore_device_info() {
11041102

11051103
} else {
11061104

1107-
Serial.println(F("*TH: Reading JSON..."));
1108-
11091105
if (config["alias"]) {
11101106
thinx_alias = strdup(config["alias"]);
1111-
Serial.print("alias: ");
1112-
Serial.println(thinx_alias);
11131107
}
11141108

11151109
if (config["udid"]) {
@@ -1123,25 +1117,16 @@ void THiNX::restore_device_info() {
11231117
thinx_udid = strdup(THINX_UDID);
11241118
}
11251119

1126-
Serial.print("thinx_udid: ");
1127-
Serial.println(thinx_udid);
1128-
11291120
if (config["apikey"]) {
11301121
thinx_api_key = strdup(config["apikey"]);
1131-
Serial.print("apikey: ");
1132-
Serial.println(thinx_api_key);
11331122
}
11341123

11351124
if (config["owner"]) {
11361125
thinx_owner = strdup(config["owner"]);
1137-
Serial.print("owner: ");
1138-
Serial.println(thinx_owner);
11391126
}
11401127

11411128
if (config["ott"]) {
11421129
available_update_url = strdup(config["ott"]);
1143-
Serial.print("available_update_url: ");
1144-
Serial.println(available_update_url);
11451130
}
11461131

11471132
#ifdef __USE_SPIFFS__
@@ -1223,7 +1208,7 @@ void THiNX::deviceInfo() {
12231208

12241209
void THiNX::update_and_reboot(String url) {
12251210

1226-
Serial.print("*TH: Update with URL: ");
1211+
Serial.print("*TH: Update from URL: ");
12271212
Serial.println(url);
12281213

12291214
// #define __USE_STREAM_UPDATER__ ; // Warning, this is MQTT-based streamed update!
@@ -1343,13 +1328,11 @@ void THiNX::evt_save_api_key() {
13431328
if (should_save_config) {
13441329
if (strlen(thx_api_key) > 4) {
13451330
thinx_api_key = thx_api_key;
1346-
Serial.print(F("Saving thx_api_key from Captive Portal: "));
1347-
Serial.println(thinx_api_key);
1331+
Serial.print(F("Saving thx_api_key from Captive Portal."));
13481332
}
13491333
if (strlen(thx_owner_key) > 4) {
13501334
thinx_owner_key = thx_owner_key;
1351-
Serial.print(F("Saving thx_owner_key from Captive Portal: "));
1352-
Serial.println(thinx_owner_key);
1335+
Serial.print(F("Saving thx_owner_key from Captive Portal."));
13531336
}
13541337
save_device_info();
13551338
should_save_config = false;
@@ -1400,11 +1383,8 @@ void THiNX::loop() {
14001383
wifi_connected = false;
14011384
if (wifi_connection_in_progress != true) {
14021385
Serial.println(F("*TH: CONNECTING »"));
1403-
Serial.println(F("*TH: LOOP «÷»")); Serial.flush();
14041386
connect(); // blocking
1405-
Serial.println(F("*TH: Enabling connection state (1283)"));
14061387
wifi_connection_in_progress = true;
1407-
Serial.println(F("*TH: LOOP «"));
14081388
wifi_connection_in_progress = true;
14091389
return;
14101390
} else {
@@ -1418,18 +1398,11 @@ void THiNX::loop() {
14181398
Serial.println(F("*TH: Error setting up mDNS"));
14191399
} else {
14201400
// Query MDNS proxy
1421-
Serial.println(F("*TH: Searching for thinx-connect on local network..."));
1401+
Serial.println(F("*TH: Searching for thinx-connect proxy on local network..."));
14221402
int n = MDNS.queryService("thinx", "tcp"); // TODO: WARNING! may be _tcp!
14231403
if (n > 0) {
14241404
thinx_cloud_url = strdup(String(MDNS.hostname(0)).c_str());
14251405
thinx_mqtt_url = strdup(String(MDNS.hostname(0)).c_str());
1426-
1427-
Serial.println(F("*TH: Routing traffic through thinx-connect..."));
1428-
Serial.print(F(" host : ")); Serial.println(MDNS.hostname(0));
1429-
Serial.print(F(" IP : ")); Serial.println(MDNS.IP(0));
1430-
Serial.print(F(" port : ")); Serial.println(MDNS.port(0));
1431-
Serial.print(F(" thinx_cloud_url : ")); Serial.println(thinx_cloud_url);
1432-
Serial.print(F(" thinx_mqtt_url : ")); Serial.println(thinx_mqtt_url);
14331406
}
14341407
}
14351408

@@ -1442,10 +1415,7 @@ void THiNX::loop() {
14421415
thinx_mqtt_channel(); // initialize channel variable
14431416
if (strlen(mqtt_device_channel) > 5) {
14441417
if (mqtt_client->subscribe(mqtt_device_channel)) {
1445-
Serial.print(F("*TH: MQTT device topic: "));
1446-
Serial.print(mqtt_device_channel);
1447-
Serial.println(F(" successfully subscribed."));
1448-
Serial.println(F("*TH: Publishing device status over MQTT... "));
1418+
Serial.print(F("*TH: MQTT device topic subscribed."));
14491419
// Publish status on status channel
14501420
mqtt_client->publish(
14511421
mqtt_device_status_channel,
@@ -1483,7 +1453,6 @@ void THiNX::loop() {
14831453
// If connected, perform the MQTT loop and bail out ASAP
14841454
if (thinx_phase == CONNECT_API) {
14851455
if (WiFi.getMode() == WIFI_AP) {
1486-
Serial.println(F("*TH: LOOP « (AP_MODE)"));
14871456
return;
14881457
}
14891458
if (strlen(thinx_api_key) > 4) {

src/THiNXLib.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
// Provides placeholder for THINX_FIRMWARE_VERSION_SHORT
1111
#ifndef VERSION
12-
#define VERSION "2.1.171"
12+
#define VERSION "2.1.172"
1313
#endif
1414

1515
#ifndef THX_REVISION
1616
#ifdef THINX_FIRMWARE_VERSION_SHORT
1717
#define THX_REVISION THINX_FIRMWARE_VERSION_SHORT
1818
#else
19-
#define THX_REVISION "171"
19+
#define THX_REVISION "170"
2020
#endif
2121
#endif
2222

src/thinx.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
const char * THINX_ALIAS = "unnamed";
66
const char * THINX_API_KEY = "0";
7-
const char * THINX_APP_VERSION = "thinx-lib-esp8266-arduino:2.1.171:2018-03-19";
7+
const char * THINX_APP_VERSION = "thinx-lib-esp8266-arduino:2.1.172:2018-03-21";
88
const char * THINX_CLOUD_URL = "thinx.cloud";
99
const char * THINX_COMMIT_ID = "0";
10-
const char * THINX_FIRMWARE_VERSION = "thinx-lib-esp8266-arduino:2.1.171";
11-
const char * THINX_FIRMWARE_VERSION_SHORT = "2.1.171";
10+
const char * THINX_FIRMWARE_VERSION = "thinx-lib-esp8266-arduino:2.1.172";
11+
const char * THINX_FIRMWARE_VERSION_SHORT = "2.1.172";
1212
const char * THINX_MQTT_URL = "thinx.cloud";
1313
const char * THINX_OWNER = "0";
1414
const char * THINX_PLATFORM = "platformio";

0 commit comments

Comments
 (0)