Compare commits

...
Author SHA1 Message Date
J. Nick Koston 3f2d67600b Merge remote-tracking branch 'origin/dev' into HEAD
# Conflicts:
#	esphome/components/web_server_base/web_server_base.cpp
2026-11-14 08:43:46 -06:00
J. Nick Koston 88537650cc Merge remote-tracking branch 'origin/dev' into esp8266-reject-upload-early
# Conflicts:
#	esphome/components/web_server_base/web_server_base.h
2026-11-04 20:51:14 -06:00
J. Nick Koston 47c228c61d Merge branch 'dev' into esp8266-reject-upload-early 2026-10-30 16:47:44 -10:00
J. Nick Koston 868a7045df Merge remote-tracking branch 'origin/dev' into esp8266-reject-upload-early 2026-10-30 15:27:53 -10:00
J. Nick Koston e11ae733de [web_server_base] Answer a rejected upload at once on ESPAsyncWebServer platforms
ESPAsyncWebServer only sends a response after the whole request body has
arrived, so a firmware upload with wrong or missing credentials was read to
the end before the 401 went out. Pause the request before replying so the
response is sent immediately, and skip the repeated check on later chunks.
2026-10-30 15:08:19 -10:00
@@ -52,8 +52,6 @@ bool GuardMiddlewareHandler::check_auth_(AsyncWebServerRequest *request) {
// compiled out. On ESP32 our own server picks the scheme internally.
#if USE_ESP32 || defined(USE_WEBSERVER_AUTH_DIGEST)
bool success = request->authenticate(credentials.username, credentials.password);
if (!success)
request->requestAuthentication();
#else
// Checked here, not with the library's authenticate()/requestAuthentication(): those also link its
// Digest code, since the scheme is only known at run time there
@@ -62,13 +60,27 @@ bool GuardMiddlewareHandler::check_auth_(AsyncWebServerRequest *request) {
const bool success = request->authType() == AsyncAuthType::AUTH_BASIC && challenge.length() == hash_len &&
constant_time_equals(reinterpret_cast<const uint8_t *>(challenge.c_str()),
reinterpret_cast<const uint8_t *>(credentials.basic_auth_hash), hash_len);
#endif
if (!success) {
#if USE_ESP32
request->requestAuthentication();
#else
// Every upload or body chunk is checked; the first rejection already answered.
if (request->isSent())
return false;
// ESPAsyncWebServer holds a response until the whole body has arrived, so a rejected upload
// would be read to the end first. A paused request sends its response at once.
request->pause();
#ifdef USE_WEBSERVER_AUTH_DIGEST
request->requestAuthentication(nullptr, true);
#else
// The challenge the library sent, with the strings kept in flash on ESP8266
AsyncWebServerResponse *response = request->beginResponse(401);
response->addHeader(String(F("WWW-Authenticate")), String(F("Basic realm=\"Login Required\"")));
request->send(response);
}
#endif
#endif
}
return success;
#else
return true;