Skip to content

Commit 6cc1abf

Browse files
authored
Verify host (WebThingsIO#20)
* Verify Host header on ESP boards; Consolidate to use only AsyncWebServer * Verify host in WiFi101 adapter * Update examples
1 parent 5934c52 commit 6cc1abf

File tree

11 files changed

+161
-252
lines changed

11 files changed

+161
-252
lines changed

ESP8266WebThingAdapter.h

Lines changed: 0 additions & 205 deletions
This file was deleted.

ESP32WebThingAdapter.h renamed to ESPWebThingAdapter.h

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
/**
2-
* ESP32WebThingAdapter.h
2+
* ESPWebThingAdapter.h
33
*
44
* Exposes the Web Thing API based on provided ThingDevices.
5-
* Suitable for ESP32, ESPAsyncWebServer, ESPAsyncTCP
5+
* Suitable for ESP32 and ESP8266 using ESPAsyncWebServer and ESPAsyncTCP
66
*
77
* This Source Code Form is subject to the terms of the Mozilla Public
88
* License, v. 2.0. If a copy of the MPL was not distributed with this
99
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
1010
*/
1111

12-
#ifndef MOZILLA_IOT_ESP32WEBTHINGADAPTER_H
13-
#define MOZILLA_IOT_ESP32WEBTHINGADAPTER_H
12+
#ifndef MOZILLA_IOT_ESPWEBTHINGADAPTER_H
13+
#define MOZILLA_IOT_ESPWEBTHINGADAPTER_H
1414

15-
#ifdef ESP32
15+
#if defined(ESP32) || defined(ESP8266)
1616

17-
#include <WiFiClient.h>
1817
#include <ArduinoJson.h>
1918
#include <ESPAsyncWebServer.h>
19+
#ifdef ESP8266
20+
#include <ESP8266mDNS.h>
21+
#else
2022
#include <ESPmDNS.h>
23+
#endif
2124
#include "Thing.h"
2225

23-
#define ESP32_MAX_PUT_BODY_SIZE 256
26+
#define ESP_MAX_PUT_BODY_SIZE 512
2427

2528

2629
class WebThingAdapter {
2730
public:
28-
WebThingAdapter(String _name): name(_name), server(80) {
31+
WebThingAdapter(String _name, IPAddress _ip): name(_name), server(80), ip(_ip.toString()) {
2932
}
3033

3134
void begin() {
@@ -85,19 +88,44 @@ class WebThingAdapter {
8588
private:
8689
AsyncWebServer server;
8790
String name;
91+
String ip;
8892
ThingDevice* firstDevice = nullptr;
8993
ThingDevice* lastDevice = nullptr;
90-
char body_data[ESP32_MAX_PUT_BODY_SIZE];
94+
char body_data[ESP_MAX_PUT_BODY_SIZE];
9195
bool b_has_body_data = false;
9296

97+
bool verifyHost(AsyncWebServerRequest *request) {
98+
AsyncWebHeader* header = request->getHeader("Host");
99+
if (header == nullptr) {
100+
request->send(403);
101+
return false;
102+
}
103+
String value = header->value();
104+
int colonIndex = value.indexOf(':');
105+
if (colonIndex >= 0) {
106+
value.remove(colonIndex);
107+
}
108+
if (value == name + ".local" || value == ip) {
109+
return true;
110+
}
111+
request->send(403);
112+
return false;
113+
}
114+
93115
void handleUnknown(AsyncWebServerRequest *request) {
116+
if (!verifyHost(request)) {
117+
return;
118+
}
94119
request->send(404);
95120
}
96121

97122
void handleThings(AsyncWebServerRequest *request) {
123+
if (!verifyHost(request)) {
124+
return;
125+
}
98126
AsyncResponseStream *response = request->beginResponseStream("application/json");
99127

100-
StaticJsonBuffer<2048> buf;
128+
StaticJsonBuffer<4096> buf;
101129
JsonArray& things = buf.createArray();
102130
ThingDevice* device = this->firstDevice;
103131
while (device != nullptr) {
@@ -148,6 +176,9 @@ class WebThingAdapter {
148176
}
149177

150178
void handleThing(AsyncWebServerRequest *request, ThingDevice*& device) {
179+
if (!verifyHost(request)) {
180+
return;
181+
}
151182
AsyncResponseStream *response = request->beginResponseStream("application/json");
152183

153184
StaticJsonBuffer<1024> buf;
@@ -159,6 +190,9 @@ class WebThingAdapter {
159190
}
160191

161192
void handleThingPropertyGet(AsyncWebServerRequest *request, ThingProperty* property) {
193+
if (!verifyHost(request)) {
194+
return;
195+
}
162196
AsyncResponseStream *response = request->beginResponseStream("application/json");
163197

164198
StaticJsonBuffer<256> buf;
@@ -181,7 +215,7 @@ class WebThingAdapter {
181215

182216

183217
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
184-
if ( total >= ESP32_MAX_PUT_BODY_SIZE || index+len >= ESP32_MAX_PUT_BODY_SIZE) {
218+
if ( total >= ESP_MAX_PUT_BODY_SIZE || index+len >= ESP_MAX_PUT_BODY_SIZE) {
185219
return; // cannot store this size..
186220
}
187221
// copy to internal buffer
@@ -190,6 +224,9 @@ class WebThingAdapter {
190224
}
191225

192226
void handleThingPropertyPut(AsyncWebServerRequest *request, ThingProperty* property) {
227+
if (!verifyHost(request)) {
228+
return;
229+
}
193230
if (!b_has_body_data) {
194231
request->send(422); // unprocessable entity (b/c no body)
195232
return;
@@ -231,6 +268,6 @@ class WebThingAdapter {
231268

232269
};
233270

234-
#endif // ESP32
271+
#endif // ESP
235272

236273
#endif

WebThingAdapter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
#ifndef MOZILLA_IOT_WEBTHINGADAPTER_H
1212
#define MOZILLA_IOT_WEBTHINGADAPTER_H
1313

14-
#include "ESP8266WebThingAdapter.h"
15-
#include "ESP32WebThingAdapter.h"
14+
#include "ESPWebThingAdapter.h"
1615
#include "WiFi101WebThingAdapter.h"
1716

1817
#endif

0 commit comments

Comments
 (0)