From c2f0e14e122f7469a9e0a8415d34af9d11fb0ed2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 Sep 2025 22:39:52 -0500 Subject: [PATCH] [api] Exclude ConnectRequest/Response when password is disabled --- esphome/components/api/api.proto | 2 ++ esphome/components/api/api_connection.cpp | 11 ++++------- esphome/components/api/api_connection.h | 2 ++ esphome/components/api/api_pb2.cpp | 2 ++ esphome/components/api/api_pb2.h | 2 ++ esphome/components/api/api_pb2_dump.cpp | 2 ++ esphome/components/api/api_pb2_service.cpp | 4 ++++ esphome/components/api/api_pb2_service.h | 6 ++++++ 8 files changed, 24 insertions(+), 7 deletions(-) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 471127e93a..f82e762345 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -133,6 +133,7 @@ message ConnectRequest { option (id) = 3; option (source) = SOURCE_CLIENT; option (no_delay) = true; + option (ifdef) = "USE_API_PASSWORD"; // The password to log in with string password = 1; @@ -144,6 +145,7 @@ message ConnectResponse { option (id) = 4; option (source) = SOURCE_SERVER; option (no_delay) = true; + option (ifdef) = "USE_API_PASSWORD"; bool invalid_password = 1; } diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 1fb65f3a2b..814f567a6d 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1385,20 +1385,17 @@ bool APIConnection::send_hello_response(const HelloRequest &msg) { return this->send_message(resp, HelloResponse::MESSAGE_TYPE); } -bool APIConnection::send_connect_response(const ConnectRequest &msg) { - bool correct = true; #ifdef USE_API_PASSWORD - correct = this->parent_->check_password(msg.password); -#endif - +bool APIConnection::send_connect_response(const ConnectRequest &msg) { ConnectResponse resp; // bool invalid_password = 1; - resp.invalid_password = !correct; - if (correct) { + resp.invalid_password = !this->parent_->check_password(msg.password); + if (!resp.invalid_password) { this->complete_authentication_(); } return this->send_message(resp, ConnectResponse::MESSAGE_TYPE); } +#endif // USE_API_PASSWORD bool APIConnection::send_ping_response(const PingRequest &msg) { PingResponse resp; diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 8f93f38203..70fc881a82 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -197,7 +197,9 @@ class APIConnection final : public APIServerConnection { void on_get_time_response(const GetTimeResponse &value) override; #endif bool send_hello_response(const HelloRequest &msg) override; +#ifdef USE_API_PASSWORD bool send_connect_response(const ConnectRequest &msg) override; +#endif bool send_disconnect_response(const DisconnectRequest &msg) override; bool send_ping_response(const PingRequest &msg) override; bool send_device_info_response(const DeviceInfoRequest &msg) override; diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index a92fca70d6..7b1601515e 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -42,6 +42,7 @@ void HelloResponse::calculate_size(ProtoSize &size) const { size.add_length(1, this->server_info_ref_.size()); size.add_length(1, this->name_ref_.size()); } +#ifdef USE_API_PASSWORD bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 1: @@ -54,6 +55,7 @@ bool ConnectRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value } void ConnectResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(1, this->invalid_password); } void ConnectResponse::calculate_size(ProtoSize &size) const { size.add_bool(1, this->invalid_password); } +#endif #ifdef USE_AREAS void AreaInfo::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(1, this->area_id); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 5b6d694e3b..421789a770 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -360,6 +360,7 @@ class HelloResponse final : public ProtoMessage { protected: }; +#ifdef USE_API_PASSWORD class ConnectRequest final : public ProtoDecodableMessage { public: static constexpr uint8_t MESSAGE_TYPE = 3; @@ -391,6 +392,7 @@ class ConnectResponse final : public ProtoMessage { protected: }; +#endif class DisconnectRequest final : public ProtoMessage { public: static constexpr uint8_t MESSAGE_TYPE = 5; diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index b5e98a9f28..53af7eefef 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -669,8 +669,10 @@ void HelloResponse::dump_to(std::string &out) const { dump_field(out, "server_info", this->server_info_ref_); dump_field(out, "name", this->name_ref_); } +#ifdef USE_API_PASSWORD void ConnectRequest::dump_to(std::string &out) const { dump_field(out, "password", this->password); } void ConnectResponse::dump_to(std::string &out) const { dump_field(out, "invalid_password", this->invalid_password); } +#endif void DisconnectRequest::dump_to(std::string &out) const { out.append("DisconnectRequest {}"); } void DisconnectResponse::dump_to(std::string &out) const { out.append("DisconnectResponse {}"); } void PingRequest::dump_to(std::string &out) const { out.append("PingRequest {}"); } diff --git a/esphome/components/api/api_pb2_service.cpp b/esphome/components/api/api_pb2_service.cpp index 2598e9a0fb..1bcbfe1ba2 100644 --- a/esphome/components/api/api_pb2_service.cpp +++ b/esphome/components/api/api_pb2_service.cpp @@ -24,6 +24,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, this->on_hello_request(msg); break; } +#ifdef USE_API_PASSWORD case ConnectRequest::MESSAGE_TYPE: { ConnectRequest msg; msg.decode(msg_data, msg_size); @@ -33,6 +34,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, this->on_connect_request(msg); break; } +#endif case DisconnectRequest::MESSAGE_TYPE: { DisconnectRequest msg; // Empty message: no decode needed @@ -597,11 +599,13 @@ void APIServerConnection::on_hello_request(const HelloRequest &msg) { this->on_fatal_error(); } } +#ifdef USE_API_PASSWORD void APIServerConnection::on_connect_request(const ConnectRequest &msg) { if (!this->send_connect_response(msg)) { this->on_fatal_error(); } } +#endif void APIServerConnection::on_disconnect_request(const DisconnectRequest &msg) { if (!this->send_disconnect_response(msg)) { this->on_fatal_error(); diff --git a/esphome/components/api/api_pb2_service.h b/esphome/components/api/api_pb2_service.h index 5b7508e786..cc7604c083 100644 --- a/esphome/components/api/api_pb2_service.h +++ b/esphome/components/api/api_pb2_service.h @@ -26,7 +26,9 @@ class APIServerConnectionBase : public ProtoService { virtual void on_hello_request(const HelloRequest &value){}; +#ifdef USE_API_PASSWORD virtual void on_connect_request(const ConnectRequest &value){}; +#endif virtual void on_disconnect_request(const DisconnectRequest &value){}; virtual void on_disconnect_response(const DisconnectResponse &value){}; @@ -213,7 +215,9 @@ class APIServerConnectionBase : public ProtoService { class APIServerConnection : public APIServerConnectionBase { public: virtual bool send_hello_response(const HelloRequest &msg) = 0; +#ifdef USE_API_PASSWORD virtual bool send_connect_response(const ConnectRequest &msg) = 0; +#endif virtual bool send_disconnect_response(const DisconnectRequest &msg) = 0; virtual bool send_ping_response(const PingRequest &msg) = 0; virtual bool send_device_info_response(const DeviceInfoRequest &msg) = 0; @@ -334,7 +338,9 @@ class APIServerConnection : public APIServerConnectionBase { #endif protected: void on_hello_request(const HelloRequest &msg) override; +#ifdef USE_API_PASSWORD void on_connect_request(const ConnectRequest &msg) override; +#endif void on_disconnect_request(const DisconnectRequest &msg) override; void on_ping_request(const PingRequest &msg) override; void on_device_info_request(const DeviceInfoRequest &msg) override;