tcp_transport: Fix Websocket transport initialization

In case of a failure in alocation of WS transport handle, parent leaks.
This commit is contained in:
Euripedes Rocha 2021-12-06 13:15:55 -03:00
parent 120b0ba2af
commit ae8eeaade9
2 changed files with 39 additions and 41 deletions

View File

@ -1,16 +1,8 @@
// Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <string.h>
@ -109,7 +101,9 @@ static char *trimwhitespace(const char *str)
char *end;
// Trim leading space
while (isspace((unsigned char)*str)) str++;
while (isspace((unsigned char)*str)) {
str++;
}
if (*str == 0) {
return (char *)str;
@ -117,7 +111,9 @@ static char *trimwhitespace(const char *str)
// Trim trailing space
end = (char *)(str + strlen(str) - 1);
while (end > str && isspace((unsigned char)*end)) end--;
while (end > str && isspace((unsigned char)*end)) {
end--;
}
// Write new null terminator
*(end + 1) = 0;
@ -154,7 +150,7 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
// Size of base64 coded string is equal '((input_size * 4) / 3) + (input_size / 96) + 6' including Z-term
unsigned char client_key[28] = {0};
const char *user_agent_ptr = (ws->user_agent)?(ws->user_agent):"ESP32 Websocket Client";
const char *user_agent_ptr = (ws->user_agent) ? (ws->user_agent) : "ESP32 Websocket Client";
size_t outlen = 0;
esp_crypto_base64_encode(client_key, sizeof(client_key), &outlen, random_key, sizeof(random_key));
@ -229,15 +225,15 @@ static int ws_connect(esp_transport_handle_t t, const char *host, int port, int
// If you are interested, see https://tools.ietf.org/html/rfc6455
const char expected_server_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
unsigned char expected_server_text[sizeof(client_key) + sizeof(expected_server_magic) + 1];
strcpy((char*)expected_server_text, (char*)client_key);
strcat((char*)expected_server_text, expected_server_magic);
strcpy((char *)expected_server_text, (char *)client_key);
strcat((char *)expected_server_text, expected_server_magic);
size_t key_len = strlen((char*)expected_server_text);
size_t key_len = strlen((char *)expected_server_text);
esp_crypto_sha1(expected_server_text, key_len, expected_server_sha1);
esp_crypto_base64_encode(expected_server_key, sizeof(expected_server_key), &outlen, expected_server_sha1, sizeof(expected_server_sha1));
expected_server_key[ (outlen < sizeof(expected_server_key)) ? outlen : (sizeof(expected_server_key) - 1) ] = 0;
ESP_LOGD(TAG, "server key=%s, send_key=%s, expected_server_key=%s", (char *)server_key, (char*)client_key, expected_server_key);
if (strcmp((char*)expected_server_key, (char*)server_key) != 0) {
ESP_LOGD(TAG, "server key=%s, send_key=%s, expected_server_key=%s", (char *)server_key, (char *)client_key, expected_server_key);
if (strcmp((char *)expected_server_key, (char *)server_key) != 0) {
ESP_LOGE(TAG, "Invalid websocket key");
return -1;
}
@ -300,7 +296,7 @@ static int _ws_write(esp_transport_handle_t t, int opcode, int mask_flag, const
// in case of masked transport we have to revert back to the original data, as ws layer
// does not create its own copy of data to be sent
if (mask_flag) {
mask = &ws_header[header_len-4];
mask = &ws_header[header_len - 4];
for (i = 0; i < len; ++i) {
buffer[i] = (buffer[i] ^ mask[i % 4]);
}
@ -598,7 +594,10 @@ esp_transport_handle_t esp_transport_ws_init(esp_transport_handle_t parent_handl
return NULL;
}
transport_ws_t *ws = calloc(1, sizeof(transport_ws_t));
ESP_TRANSPORT_MEM_CHECK(TAG, ws, return NULL);
ESP_TRANSPORT_MEM_CHECK(TAG, ws, {
esp_transport_destroy(t);
return NULL;
});
ws->parent = parent_handle;
ws->path = strdup("/");
@ -771,7 +770,7 @@ static int esp_transport_ws_handle_control_frames(esp_transport_handle_t t, char
if (client_closed == false) {
// Only echo the closing frame if not initiated by the client
if (_ws_write(t, WS_OPCODE_CLOSE | WS_FIN, WS_MASK, NULL,0, timeout_ms) < 0) {
if (_ws_write(t, WS_OPCODE_CLOSE | WS_FIN, WS_MASK, NULL, 0, timeout_ms) < 0) {
ESP_LOGE(TAG, "Sending CLOSE frame with 0 payload failed");
return -1;
}

View File

@ -2294,7 +2294,6 @@ components/tcp_transport/test/test_transport_fixtures.c
components/tcp_transport/transport.c
components/tcp_transport/transport_ssl.c
components/tcp_transport/transport_utils.c
components/tcp_transport/transport_ws.c
components/tcpip_adapter/include/tcpip_adapter.h
components/tcpip_adapter/include/tcpip_adapter_compatible/tcpip_adapter_compat.h
components/tcpip_adapter/include/tcpip_adapter_types.h