diff options
| author | frosty <frostinator33@airmail.cc> | 2026-10-27 20:31:28 -0400 |
|---|---|---|
| committer | frosty <frostinator33@airmail.cc> | 2026-10-27 20:33:50 -0400 |
| commit | 250898c638f07ace6bc7167faede93113c8fd0db (patch) | |
| tree | 310addbedcaaf76da98df22434a4f4711c8ba963 /src/Routes/ImageProxy.c | |
| parent | bd3da2232540ea4c78e7ac35e2c90c38c8a3c410 (diff) | |
refactor: replace potentially unsafe string functions with snprintf
Diffstat (limited to 'src/Routes/ImageProxy.c')
| -rw-r--r-- | src/Routes/ImageProxy.c | 23 |
1 files changed, 11 insertions, 12 deletions
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"); |
