mirror of
https://github.com/esphome/esphome.git
synced 2026-05-28 13:37:24 +08:00
[http_request.ota] Percent-encode credentials in URL (#14257)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
Jesse Hills
parent
efa39ae591
commit
29d890bb0f
@@ -1,5 +1,7 @@
|
|||||||
#include "ota_http_request.h"
|
#include "ota_http_request.h"
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
#include "esphome/core/application.h"
|
#include "esphome/core/application.h"
|
||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
@@ -210,6 +212,26 @@ uint8_t OtaHttpRequestComponent::do_ota_() {
|
|||||||
return ota::OTA_RESPONSE_OK;
|
return ota::OTA_RESPONSE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URL-encode characters that are not unreserved per RFC 3986 section 2.3.
|
||||||
|
// This is needed for embedding userinfo (username/password) in URLs safely.
|
||||||
|
static std::string url_encode(const std::string &str) {
|
||||||
|
std::string result;
|
||||||
|
result.reserve(str.size());
|
||||||
|
for (char c : str) {
|
||||||
|
if (std::isalnum(static_cast<unsigned char>(c)) || c == '-' || c == '_' || c == '.' || c == '~') {
|
||||||
|
result += c;
|
||||||
|
} else {
|
||||||
|
result += '%';
|
||||||
|
result += format_hex_pretty_char((static_cast<uint8_t>(c) >> 4) & 0x0F);
|
||||||
|
result += format_hex_pretty_char(static_cast<uint8_t>(c) & 0x0F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OtaHttpRequestComponent::set_password(const std::string &password) { this->password_ = url_encode(password); }
|
||||||
|
void OtaHttpRequestComponent::set_username(const std::string &username) { this->username_ = url_encode(username); }
|
||||||
|
|
||||||
std::string OtaHttpRequestComponent::get_url_with_auth_(const std::string &url) {
|
std::string OtaHttpRequestComponent::get_url_with_auth_(const std::string &url) {
|
||||||
if (this->username_.empty() || this->password_.empty()) {
|
if (this->username_.empty() || this->password_.empty()) {
|
||||||
return url;
|
return url;
|
||||||
|
|||||||
@@ -29,9 +29,9 @@ class OtaHttpRequestComponent : public ota::OTAComponent, public Parented<HttpRe
|
|||||||
|
|
||||||
void set_md5_url(const std::string &md5_url);
|
void set_md5_url(const std::string &md5_url);
|
||||||
void set_md5(const std::string &md5) { this->md5_expected_ = md5; }
|
void set_md5(const std::string &md5) { this->md5_expected_ = md5; }
|
||||||
void set_password(const std::string &password) { this->password_ = password; }
|
void set_password(const std::string &password);
|
||||||
void set_url(const std::string &url);
|
void set_url(const std::string &url);
|
||||||
void set_username(const std::string &username) { this->username_ = username; }
|
void set_username(const std::string &username);
|
||||||
|
|
||||||
std::string md5_computed() { return this->md5_computed_; }
|
std::string md5_computed() { return this->md5_computed_; }
|
||||||
std::string md5_expected() { return this->md5_expected_; }
|
std::string md5_expected() { return this->md5_expected_; }
|
||||||
|
|||||||
Reference in New Issue
Block a user