From 5e85060605f2f01369fa0bfefac3b00635e71c87 Mon Sep 17 00:00:00 2001 From: Michael Stegeman Date: Tue, 5 Jan 2021 21:27:03 -0900 Subject: [PATCH] Add option to disable host validation. --- ESPWebThingAdapter.h | 11 +++++++++-- EthernetWebThingAdapter.h | 11 +++++++++-- WiFi101WebThingAdapter.h | 11 +++++++++-- library.json | 2 +- library.properties | 2 +- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index 166def6..6d96fcf 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -43,8 +43,10 @@ class WebThingAdapter { public: - WebThingAdapter(String _name, IPAddress _ip, uint16_t _port = 80) - : server(_port), name(_name), ip(_ip.toString()), port(_port) {} + WebThingAdapter(String _name, IPAddress _ip, uint16_t _port = 80, + bool _disableHostValidation = false) + : server(_port), name(_name), ip(_ip.toString()), port(_port), + disableHostValidation(_disableHostValidation) {} void begin() { name.toLowerCase(); @@ -197,12 +199,17 @@ class WebThingAdapter { String name; String ip; uint16_t port; + bool disableHostValidation; ThingDevice *firstDevice = nullptr; ThingDevice *lastDevice = nullptr; char body_data[ESP_MAX_PUT_BODY_SIZE]; bool b_has_body_data = false; bool verifyHost(AsyncWebServerRequest *request) { + if (disableHostValidation) { + return true; + } + AsyncWebHeader *header = request->getHeader("Host"); if (header == nullptr) { request->send(403); diff --git a/EthernetWebThingAdapter.h b/EthernetWebThingAdapter.h index d9c80d6..5b679a8 100644 --- a/EthernetWebThingAdapter.h +++ b/EthernetWebThingAdapter.h @@ -73,8 +73,10 @@ enum ReadState { class WebThingAdapter { public: - WebThingAdapter(String _name, uint32_t _ip, uint16_t _port = 80) - : name(_name), port(_port), server(_port) + WebThingAdapter(String _name, uint32_t _ip, uint16_t _port = 80, + bool _disableHostValidation = false) + : name(_name), port(_port), server(_port), + disableHostValidation(_disableHostValidation) #ifdef CONFIG_MDNS , mdns(udp) @@ -240,6 +242,7 @@ class WebThingAdapter { private: String name, ip; uint16_t port; + bool disableHostValidation; EthernetServer server; EthernetClient client; #ifdef CONFIG_MDNS @@ -260,6 +263,10 @@ class WebThingAdapter { ThingDevice *firstDevice = nullptr, *lastDevice = nullptr; bool verifyHost() { + if (disableHostValidation) { + return true; + } + int colonIndex = host.indexOf(':'); if (colonIndex >= 0) { host.remove(colonIndex); diff --git a/WiFi101WebThingAdapter.h b/WiFi101WebThingAdapter.h index 16497c4..e0af337 100644 --- a/WiFi101WebThingAdapter.h +++ b/WiFi101WebThingAdapter.h @@ -67,8 +67,10 @@ enum ReadState { class WebThingAdapter { public: - WebThingAdapter(String _name, uint32_t _ip, uint16_t _port = 80) - : name(_name), port(_port), server(_port), mdns(udp) { + WebThingAdapter(String _name, uint32_t _ip, uint16_t _port = 80, + bool _disableHostValidation) + : name(_name), port(_port), server(_port), + disableHostValidation(_disableHostValidation), mdns(udp) { ip = ""; for (int i = 0; i < 4; i++) { ip += _ip & 0xff; @@ -228,6 +230,7 @@ class WebThingAdapter { private: String name, ip; uint16_t port; + bool disableHostValidation; WiFiServer server; WiFiClient client; WiFiUDP udp; @@ -246,6 +249,10 @@ class WebThingAdapter { ThingDevice *firstDevice = nullptr, *lastDevice = nullptr; bool verifyHost() { + if (disableHostValidation) { + return true; + } + int colonIndex = host.indexOf(':'); if (colonIndex >= 0) { host.remove(colonIndex); diff --git a/library.json b/library.json index fc0aabb..6495a6c 100644 --- a/library.json +++ b/library.json @@ -2,7 +2,7 @@ "name": "webthing-arduino", "description": "A library for creating Web Things using the Web of Things API. Runs on ESP8266, ESP32, Ethernet, and WiFi101-compatible boards. Compatible with the WebThings Gateway.", "keywords": "Communication", - "version": "0.11.7", + "version": "0.12.0", "authors": { "name": "WebThingsIO", "email": "team@webthings.io", diff --git a/library.properties b/library.properties index 0e73e63..b0d9fd2 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=webthing-arduino -version=0.11.7 +version=0.12.0 author=WebThingsIO maintainer=WebThingsIO sentence=A library for creating Web Things using the Web of Things API