From ce871657192e4a310fc843a349b18f7fddcb783c Mon Sep 17 00:00:00 2001 From: akallabeth Date: Fri, 7 Nov 2025 11:17:08 +0100 Subject: [PATCH 1/3] [c,attributes] mark functions with attirbute malloc --- include/freerdp/settings.h | 1 + winpr/include/winpr/json.h | 2 ++ 2 files changed, 3 insertions(+) diff --git a/include/freerdp/settings.h b/include/freerdp/settings.h index 4ffce003b..ed694e300 100644 --- a/include/freerdp/settings.h +++ b/include/freerdp/settings.h @@ -783,6 +783,7 @@ extern "C" * in case of an error. * @since version 3.16.0 */ + WINPR_ATTR_MALLOC(free, 1) FREERDP_API char* freerdp_settings_serialize(const rdpSettings* settings, BOOL pretty, size_t* plength); diff --git a/winpr/include/winpr/json.h b/winpr/include/winpr/json.h index d4b2f0eb6..de48124fc 100644 --- a/winpr/include/winpr/json.h +++ b/winpr/include/winpr/json.h @@ -401,6 +401,7 @@ extern "C" * @return A string representation of the JSON instance or \b NULL * @since version 3.6.0 */ + WINPR_ATTR_MALLOC(free, 1) WINPR_API char* WINPR_JSON_Print(WINPR_JSON* item); /** @@ -411,6 +412,7 @@ extern "C" * @return A string representation of the JSON instance or \b NULL * @since version 3.6.0 */ + WINPR_ATTR_MALLOC(free, 1) WINPR_API char* WINPR_JSON_PrintUnformatted(WINPR_JSON* item); #ifdef __cplusplus From 021356cb08d58577f05a0d8bd848097b1057fba7 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Fri, 7 Nov 2025 11:11:38 +0100 Subject: [PATCH 2/3] [gateway,http] change return type of http_response_get_body the data is a string, so avoid unnecessary casts later on. --- libfreerdp/core/gateway/arm.c | 10 +++++++--- libfreerdp/core/gateway/http.c | 8 ++++---- libfreerdp/core/gateway/http.h | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/libfreerdp/core/gateway/arm.c b/libfreerdp/core/gateway/arm.c index 282d538f2..94d3206fb 100644 --- a/libfreerdp/core/gateway/arm.c +++ b/libfreerdp/core/gateway/arm.c @@ -1031,11 +1031,15 @@ fail: static BOOL arm_handle_request_ok(rdpArm* arm, const HttpResponse* response) { const size_t len = http_response_get_body_length(response); - const char* msg = (const char*)http_response_get_body(response); - if (strnlen(msg, len + 1) > len) + const char* msg = http_response_get_body(response); + const size_t alen = strnlen(msg, len + 1); + if (alen > len) + { + WLog_Print(arm->log, WLOG_ERROR, "Got HTTP Response data with invalid termination"); return FALSE; + } - WLog_Print(arm->log, WLOG_DEBUG, "Got HTTP Response data: %s", msg); + WLog_Print(arm->log, WLOG_DEBUG, "Got HTTP Response data: [%" PRIuz "] %s", len, msg); return arm_fill_gateway_parameters(arm, msg, len); } diff --git a/libfreerdp/core/gateway/http.c b/libfreerdp/core/gateway/http.c index 86c8eb14c..a9bdd057a 100644 --- a/libfreerdp/core/gateway/http.c +++ b/libfreerdp/core/gateway/http.c @@ -88,7 +88,7 @@ struct s_http_response const char* SecWebsocketAccept; size_t BodyLength; - BYTE* BodyContent; + char* BodyContent; wHashTable* Authenticates; wHashTable* SetCookie; @@ -1303,7 +1303,7 @@ static BOOL http_response_recv_body(rdpTls* tls, HttpResponse* response, BOOL re } while (ctx.state != ChunkStateEnd); response->BodyLength = WINPR_ASSERTING_INT_CAST(uint32_t, full_len); if (response->BodyLength > 0) - response->BodyContent = &(Stream_Buffer(response->data))[payloadOffset]; + response->BodyContent = &(Stream_BufferAs(response->data, char))[payloadOffset]; } else { @@ -1339,7 +1339,7 @@ static BOOL http_response_recv_body(rdpTls* tls, HttpResponse* response, BOOL re } if (response->BodyLength > 0) - response->BodyContent = &(Stream_Buffer(response->data))[payloadOffset]; + response->BodyContent = &(Stream_BufferAs(response->data, char))[payloadOffset]; if (bodyLength != response->BodyLength) { @@ -1467,7 +1467,7 @@ out_error: return NULL; } -const BYTE* http_response_get_body(const HttpResponse* response) +const char* http_response_get_body(const HttpResponse* response) { if (!response) return NULL; diff --git a/libfreerdp/core/gateway/http.h b/libfreerdp/core/gateway/http.h index eaa07a91c..92ce114e7 100644 --- a/libfreerdp/core/gateway/http.h +++ b/libfreerdp/core/gateway/http.h @@ -128,7 +128,7 @@ FREERDP_LOCAL HttpResponse* http_response_recv(rdpTls* tls, BOOL readContentLeng FREERDP_LOCAL UINT16 http_response_get_status_code(const HttpResponse* response); FREERDP_LOCAL size_t http_response_get_body_length(const HttpResponse* response); -FREERDP_LOCAL const BYTE* http_response_get_body(const HttpResponse* response); +FREERDP_LOCAL const char* http_response_get_body(const HttpResponse* response); FREERDP_LOCAL const char* http_response_get_auth_token(const HttpResponse* response, const char* method); FREERDP_LOCAL const char* http_response_get_setcookie(const HttpResponse* response, From a57950c576ed615b17dd0e0d4b0c547a3a853143 Mon Sep 17 00:00:00 2001 From: akallabeth Date: Fri, 7 Nov 2025 11:17:22 +0100 Subject: [PATCH 3/3] [gateway,arm] improve logging of arm responses --- libfreerdp/core/gateway/arm.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/libfreerdp/core/gateway/arm.c b/libfreerdp/core/gateway/arm.c index 94d3206fb..17e6aa2a1 100644 --- a/libfreerdp/core/gateway/arm.c +++ b/libfreerdp/core/gateway/arm.c @@ -953,7 +953,17 @@ static BOOL arm_fill_gateway_parameters(rdpArm* arm, const char* message, size_t WINPR_JSON* json = WINPR_JSON_ParseWithLength(message, len); BOOL status = FALSE; if (!json) + { + WLog_Print(arm->log, WLOG_ERROR, "Response data is not valid JSON"); return FALSE; + } + + if (WLog_IsLevelActive(arm->log, WLOG_DEBUG)) + { + char* str = WINPR_JSON_PrintUnformatted(json); + WLog_Print(arm->log, WLOG_DEBUG, "Got HTTP Response data: %s", str); + free(str); + } rdpSettings* settings = arm->context->settings; WINPR_JSON* gwurl = WINPR_JSON_GetObjectItemCaseSensitive(json, "gatewayLocationPreWebSocket"); @@ -1039,7 +1049,6 @@ static BOOL arm_handle_request_ok(rdpArm* arm, const HttpResponse* response) return FALSE; } - WLog_Print(arm->log, WLOG_DEBUG, "Got HTTP Response data: [%" PRIuz "] %s", len, msg); return arm_fill_gateway_parameters(arm, msg, len); }