From 250898c638f07ace6bc7167faede93113c8fd0db Mon Sep 17 00:00:00 2001 From: frosty Date: Tue, 27 Oct 2026 20:31:28 -0400 Subject: refactor: replace potentially unsafe string functions with snprintf --- src/Routes/ImageProxy.c | 23 +++++++++++------------ src/Routes/Images.c | 6 +++--- src/Routes/Search.c | 21 +++++++++++---------- src/Routes/SettingsSave.c | 32 +++++++++++++++++++++++++------- 4 files changed, 50 insertions(+), 32 deletions(-) (limited to 'src/Routes') diff --git a/src/Routes/ImageProxy.c b/src/Routes/ImageProxy.c index 2d6d3a9..9ce79d9 100644 --- a/src/Routes/ImageProxy.c +++ b/src/Routes/ImageProxy.c @@ -19,7 +19,7 @@ typedef struct DnsCacheEntry { char hostname[256]; - char ip_str[INET_ADDRSTRLEN]; + char ip_str[INET6_ADDRSTRLEN]; time_t resolved_at; struct DnsCacheEntry *next; } DnsCacheEntry; @@ -39,7 +39,7 @@ static int dns_cache_lookup(const char *hostname, char *out_ip) { for (DnsCacheEntry *e = dns_cache; e; e = e->next) { if (strcmp(e->hostname, hostname) == 0) { if ((now - e->resolved_at) < DNS_CACHE_TTL) { - strcpy(out_ip, e->ip_str); + snprintf(out_ip, INET6_ADDRSTRLEN, "%s", e->ip_str); pthread_mutex_unlock(&dns_cache_mutex); return 0; } @@ -63,7 +63,7 @@ static void dns_cache_insert(const char *hostname, const char *ip_str) { continue; } if (strcmp(entry->hostname, hostname) == 0) { - strcpy(entry->ip_str, ip_str); + snprintf(entry->ip_str, sizeof(entry->ip_str), "%s", ip_str); entry->resolved_at = now; pthread_mutex_unlock(&dns_cache_mutex); return; @@ -73,9 +73,8 @@ static void dns_cache_insert(const char *hostname, const char *ip_str) { DnsCacheEntry *new_entry = malloc(sizeof(DnsCacheEntry)); if (new_entry) { - strncpy(new_entry->hostname, hostname, sizeof(new_entry->hostname) - 1); - new_entry->hostname[sizeof(new_entry->hostname) - 1] = '\0'; - strcpy(new_entry->ip_str, ip_str); + snprintf(new_entry->hostname, sizeof(new_entry->hostname), "%s", hostname); + snprintf(new_entry->ip_str, sizeof(new_entry->ip_str), "%s", ip_str); new_entry->resolved_at = now; new_entry->next = dns_cache; dns_cache = new_entry; @@ -150,7 +149,7 @@ static const char *is_private_hostname(const char *hostname, char *out_ip) { } freeaddrinfo(res); - strcpy(out_ip, ip_str); + snprintf(out_ip, INET6_ADDRSTRLEN, "%s", ip_str); dns_cache_insert(hostname, ip_str); return out_ip; } @@ -172,7 +171,8 @@ static int is_allowed_domain(const char *url, char *resolved_ip) { curl_url_get(h, CURLUPART_SCHEME, &scheme, 0); int valid_scheme = 0; - if (scheme && (strcasecmp(scheme, "http") == 0 || strcasecmp(scheme, "https") == 0)) { + if (scheme && + (strcasecmp(scheme, "http") == 0 || strcasecmp(scheme, "https") == 0)) { valid_scheme = 1; } @@ -258,7 +258,7 @@ static char *url_encode_key(const char *url) { EVP_MD_CTX_free(ctx); for (int i = 0; i < 16; i++) { - sprintf(hash + (i * 2), "%02x", md5hash[i]); + snprintf(hash + (i * 2), 3, "%02x", md5hash[i]); } hash[32] = '\0'; @@ -279,7 +279,7 @@ int image_proxy_handler(UrlParams *params) { return 0; } - char resolved_ip[INET_ADDRSTRLEN] = {0}; + char resolved_ip[INET6_ADDRSTRLEN] = {0}; int domain_check = is_allowed_domain(url, resolved_ip); if (domain_check == -1) { send_response("Invalid URL scheme"); @@ -420,8 +420,7 @@ int image_proxy_handler(UrlParams *params) { return 0; } - if (strlen(content_type) == 0 || - strncmp(content_type, "image/", 6) != 0) { + if (strlen(content_type) == 0 || strncmp(content_type, "image/", 6) != 0) { free(buf.data); free(cache_key); send_response("Invalid content type"); diff --git a/src/Routes/Images.c b/src/Routes/Images.c index 1ab875e..3840587 100644 --- a/src/Routes/Images.c +++ b/src/Routes/Images.c @@ -27,12 +27,12 @@ static char *build_images_href(const char *query, int page) { if (!href) return NULL; - snprintf(href, needed, "/images?q=%s", safe_query); if (page > 1) { char page_buf[16]; snprintf(page_buf, sizeof(page_buf), "%d", page); - strcat(href, "&p="); - strcat(href, page_buf); + snprintf(href, needed, "/images?q=%s&p=%s", safe_query, page_buf); + } else { + snprintf(href, needed, "/images?q=%s", safe_query); } return href; } diff --git a/src/Routes/Search.c b/src/Routes/Search.c index 0aa146a..8526cfa 100644 --- a/src/Routes/Search.c +++ b/src/Routes/Search.c @@ -376,18 +376,19 @@ static char *build_search_href(const char *query, const char *engine_id, if (!href) return NULL; - snprintf(href, needed, "/search?q=%s", safe_query); - - if (use_engine) { - strcat(href, "&engine="); - strcat(href, engine_id); - } - - if (page > 1) { + if (use_engine && page > 1) { char page_buf[16]; snprintf(page_buf, sizeof(page_buf), "%d", page); - strcat(href, "&p="); - strcat(href, page_buf); + snprintf(href, needed, "/search?q=%s&engine=%s&p=%s", safe_query, engine_id, + page_buf); + } else if (use_engine) { + snprintf(href, needed, "/search?q=%s&engine=%s", safe_query, engine_id); + } else if (page > 1) { + char page_buf[16]; + snprintf(page_buf, sizeof(page_buf), "%d", page); + snprintf(href, needed, "/search?q=%s&p=%s", safe_query, page_buf); + } else { + snprintf(href, needed, "/search?q=%s", safe_query); } return href; diff --git a/src/Routes/SettingsSave.c b/src/Routes/SettingsSave.c index 3cbb1d6..498f627 100644 --- a/src/Routes/SettingsSave.c +++ b/src/Routes/SettingsSave.c @@ -36,8 +36,8 @@ int settings_save_handler(UrlParams *params) { selected_count < ENGINE_COUNT) { strncpy(selected_ids[selected_count], engine_id, sizeof(selected_ids[selected_count]) - 1); - selected_ids[selected_count][sizeof(selected_ids[selected_count]) - 1] = - '\0'; + selected_ids[selected_count] + [sizeof(selected_ids[selected_count]) - 1] = '\0'; selected_count++; } } @@ -45,19 +45,37 @@ int settings_save_handler(UrlParams *params) { } if (strlen(theme) > 0) { - set_cookie("theme", theme, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, false); + set_cookie("theme", theme, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, + false); } if (strlen(locale) > 0) { - set_cookie("locale", locale, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, false); + set_cookie("locale", locale, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, + false); } if (engines_present) { char cookie_value[512]; cookie_value[0] = '\0'; + size_t used = 0; + cookie_value[0] = '\0'; for (int i = 0; i < selected_count; i++) { - if (i > 0) - strcat(cookie_value, ","); - strcat(cookie_value, selected_ids[i]); + const char *id = selected_ids[i]; + size_t idlen = strlen(id); + if (i > 0) { + if (used + 1 < sizeof(cookie_value)) { + cookie_value[used++] = ','; + cookie_value[used] = '\0'; + } else { + break; + } + } + if (used + idlen < sizeof(cookie_value)) { + memcpy(cookie_value + used, id, idlen); + used += idlen; + cookie_value[used] = '\0'; + } else { + break; + } } set_cookie("engines", cookie_value, "Fri, 31 Dec 2038 23:59:59 GMT", "/", false, false); -- cgit v1.3