Additional checks for ACLK proxy setting (#20639)
Fix memory allocation and handling for proxy credentials in MQTT WSS client (CID 451575)
Stelios Fragkakis committed
Jul 8, 2025 at 18:58 UTC
10b3af3b8d35825c09a2ce83713dd8e1628b6674
1 file changed
+8
-15
src/aclk/mqtt_websockets/mqtt_wss_client.c
+8
-15
@@ -356,29 +356,22 @@ static int http_proxy_connect(mqtt_wss_client client)
356
if(write(client->sockfd, r_buf_ptr, strlen(r_buf_ptr)) <= 0) { ; }
357
358
if (client->proxy_uname) {
359
- size_t creds_plain_len = strlen(client->proxy_uname) + strlen(client->proxy_passwd) + 2;
359
+ size_t pass_len = client->proxy_passwd ? strlen(client->proxy_passwd) : 0;
360
+ size_t creds_plain_len = strlen(client->proxy_uname) + pass_len + 2;
361
+
362
char *creds_plain = mallocz(creds_plain_len);
361
- if (!creds_plain) {
362
- nd_log(NDLS_DAEMON, NDLP_ERR, "OOM creds_plain");
363
- rc = 6;
364
- goto cleanup;
365
- }
366
- int creds_base64_len = (((4 * creds_plain_len / 3) + 3) & ~3);
363
+ size_t creds_base64_len = (((4 * creds_plain_len / 3) + 3) & ~3);
364
// OpenSSL encoder puts newline every 64 output bytes
365
// we remove those but during encoding we need that space in the buffer
369
- creds_base64_len += (1+(creds_base64_len/64)) * strlen("\n");
366
+ creds_base64_len += (1 + (creds_base64_len / 64)) * strlen("\n");
367
+
368
char *creds_base64 = mallocz(creds_base64_len + 1);
371
- if (!creds_base64) {
372
- freez(creds_plain);
373
- nd_log(NDLS_DAEMON, NDLP_ERR, "OOM creds_base64");
374
- rc = 6;
375
- goto cleanup;
376
- }
369
char *ptr = creds_plain;
370
strcpy(ptr, client->proxy_uname);
371
ptr += strlen(client->proxy_uname);
372
*ptr++ = ':';
381
- strcpy(ptr, client->proxy_passwd);
373
+ if (pass_len)
374
+ strcpy(ptr, client->proxy_passwd);
375
376
(void) netdata_base64_encode((unsigned char*)creds_base64, (unsigned char*)creds_plain, strlen(creds_plain));
377
freez(creds_plain);