refactor: replace potentially unsafe string functions with snprintf
This commit is contained in:
@@ -36,7 +36,7 @@ static void md5_hash(const char *str, char *output) {
|
|||||||
EVP_DigestFinal_ex(ctx, hash, &hash_len);
|
EVP_DigestFinal_ex(ctx, hash, &hash_len);
|
||||||
EVP_MD_CTX_free(ctx);
|
EVP_MD_CTX_free(ctx);
|
||||||
for (unsigned int i = 0; i < hash_len; i++) {
|
for (unsigned int i = 0; i < hash_len; i++) {
|
||||||
sprintf(output + (i * 2), "%02x", hash[i]);
|
snprintf(output + (i * 2), 3, "%02x", hash[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,11 +50,9 @@ static time_t get_file_mtime(const char *filepath) {
|
|||||||
|
|
||||||
int cache_init(const char *dir) {
|
int cache_init(const char *dir) {
|
||||||
if (!dir || strlen(dir) == 0) {
|
if (!dir || strlen(dir) == 0) {
|
||||||
strncpy(cache_dir, DEFAULT_CACHE_DIR, sizeof(cache_dir) - 1);
|
snprintf(cache_dir, sizeof(cache_dir), "%s", DEFAULT_CACHE_DIR);
|
||||||
cache_dir[sizeof(cache_dir) - 1] = '\0';
|
|
||||||
} else {
|
} else {
|
||||||
strncpy(cache_dir, dir, sizeof(cache_dir) - 1);
|
snprintf(cache_dir, sizeof(cache_dir), "%s", dir);
|
||||||
cache_dir[sizeof(cache_dir) - 1] = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|||||||
@@ -207,8 +207,7 @@ char *construct_dictionary_url(const char *query) {
|
|||||||
const char *base = "https://dictionary.cambridge.org/dictionary/english/";
|
const char *base = "https://dictionary.cambridge.org/dictionary/english/";
|
||||||
char *url = malloc(strlen(base) + strlen(escaped) + 1);
|
char *url = malloc(strlen(base) + strlen(escaped) + 1);
|
||||||
if (url) {
|
if (url) {
|
||||||
strcpy(url, base);
|
snprintf(url, strlen(base) + strlen(escaped) + 1, "%s%s", base, escaped);
|
||||||
strcat(url, escaped);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_free(escaped);
|
curl_free(escaped);
|
||||||
|
|||||||
@@ -360,7 +360,7 @@ static int parse_conversion_query(const char *query, double *value,
|
|||||||
while (try_len > 0) {
|
while (try_len > 0) {
|
||||||
*to_unit = find_unit(try_buf);
|
*to_unit = find_unit(try_buf);
|
||||||
if (*to_unit) {
|
if (*to_unit) {
|
||||||
strcpy(to_part, try_buf);
|
snprintf(to_part, sizeof(to_part), "%s", try_buf);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
char *last_space = strrchr(try_buf, ' ');
|
char *last_space = strrchr(try_buf, ' ');
|
||||||
@@ -443,34 +443,34 @@ static const char *pluralize(const char *unit, double value, char *buf,
|
|||||||
if (len == 0 || bufsize == 0)
|
if (len == 0 || bufsize == 0)
|
||||||
return unit;
|
return unit;
|
||||||
|
|
||||||
strncpy(buf, unit, bufsize - 1);
|
snprintf(buf, bufsize, "%s", unit);
|
||||||
buf[bufsize - 1] = '\0';
|
|
||||||
|
|
||||||
if (strcmp(unit, "foot") == 0 || strcmp(unit, "square foot") == 0) {
|
if (strcmp(unit, "foot") == 0 || strcmp(unit, "square foot") == 0) {
|
||||||
if (is_one)
|
if (is_one)
|
||||||
strcpy(buf, unit);
|
snprintf(buf, bufsize, "%s", unit);
|
||||||
else
|
else
|
||||||
strcpy(buf, strcmp(unit, "square foot") == 0 ? "square feet" : "feet");
|
snprintf(buf, bufsize, "%s",
|
||||||
|
strcmp(unit, "square foot") == 0 ? "square feet" : "feet");
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
if (strcmp(unit, "inch") == 0 || strcmp(unit, "square inch") == 0) {
|
if (strcmp(unit, "inch") == 0 || strcmp(unit, "square inch") == 0) {
|
||||||
if (is_one)
|
if (is_one)
|
||||||
strcpy(buf, unit);
|
snprintf(buf, bufsize, "%s", unit);
|
||||||
else
|
else
|
||||||
strcpy(buf,
|
snprintf(buf, bufsize, "%s",
|
||||||
strcmp(unit, "square inch") == 0 ? "square inches" : "inches");
|
strcmp(unit, "square inch") == 0 ? "square inches" : "inches");
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
if (strcmp(unit, "stone") == 0) {
|
if (strcmp(unit, "stone") == 0) {
|
||||||
if (is_one)
|
if (is_one)
|
||||||
strcpy(buf, "stone");
|
snprintf(buf, bufsize, "%s", "stone");
|
||||||
else
|
else
|
||||||
strcpy(buf, "stones");
|
snprintf(buf, bufsize, "%s", "stones");
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
if (strcmp(unit, "celsius") == 0 || strcmp(unit, "fahrenheit") == 0 ||
|
if (strcmp(unit, "celsius") == 0 || strcmp(unit, "fahrenheit") == 0 ||
|
||||||
strcmp(unit, "kelvin") == 0) {
|
strcmp(unit, "kelvin") == 0) {
|
||||||
strcpy(buf, unit);
|
snprintf(buf, bufsize, "%s", unit);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ static void shorten_summary(char **extract_ptr, int max_chars) {
|
|||||||
if (new_text) {
|
if (new_text) {
|
||||||
strncpy(new_text, text, end_pos);
|
strncpy(new_text, text, end_pos);
|
||||||
new_text[end_pos] = '\0';
|
new_text[end_pos] = '\0';
|
||||||
strcat(new_text, "...");
|
strncat(new_text, "...", 3);
|
||||||
free(*extract_ptr);
|
free(*extract_ptr);
|
||||||
*extract_ptr = new_text;
|
*extract_ptr = new_text;
|
||||||
}
|
}
|
||||||
@@ -65,8 +65,8 @@ static void extract_wiki_info(xmlNode *node, InfoBox *info) {
|
|||||||
info->url =
|
info->url =
|
||||||
malloc(strlen(base_article_url) + strlen(formatted_title) + 1);
|
malloc(strlen(base_article_url) + strlen(formatted_title) + 1);
|
||||||
if (info->url) {
|
if (info->url) {
|
||||||
strcpy(info->url, base_article_url);
|
snprintf(info->url, strlen(base_article_url) + strlen(formatted_title) + 1,
|
||||||
strcat(info->url, formatted_title);
|
"%s%s", base_article_url, formatted_title);
|
||||||
}
|
}
|
||||||
free(formatted_title);
|
free(formatted_title);
|
||||||
xmlFree(title);
|
xmlFree(title);
|
||||||
@@ -177,9 +177,9 @@ static char *get_first_search_result(const char *search_term) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(search_url, search_base);
|
snprintf(search_url, strlen(search_base) + strlen(escaped_term) +
|
||||||
strcat(search_url, escaped_term);
|
strlen(search_suffix) + 1, "%s%s%s", search_base,
|
||||||
strcat(search_url, search_suffix);
|
escaped_term, search_suffix);
|
||||||
|
|
||||||
curl_free(escaped_term);
|
curl_free(escaped_term);
|
||||||
|
|
||||||
@@ -226,8 +226,8 @@ char *construct_wiki_url(const char *search_term) {
|
|||||||
|
|
||||||
char *full_url = malloc(strlen(base) + strlen(escaped_title) + 1);
|
char *full_url = malloc(strlen(base) + strlen(escaped_title) + 1);
|
||||||
if (full_url) {
|
if (full_url) {
|
||||||
strcpy(full_url, base);
|
snprintf(full_url, strlen(base) + strlen(escaped_title) + 1, "%s%s", base,
|
||||||
strcat(full_url, escaped_title);
|
escaped_title);
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_free(escaped_title);
|
curl_free(escaped_title);
|
||||||
|
|||||||
@@ -25,8 +25,7 @@ static void generate_random_string(char *buf, size_t len) {
|
|||||||
|
|
||||||
void set_proxy_config(const char *proxy_str, int rand_user, int rand_pass) {
|
void set_proxy_config(const char *proxy_str, int rand_user, int rand_pass) {
|
||||||
if (proxy_str && proxy_str[0]) {
|
if (proxy_str && proxy_str[0]) {
|
||||||
strncpy(proxy_url, proxy_str, sizeof(proxy_url) - 1);
|
snprintf(proxy_url, sizeof(proxy_url), "%s", proxy_str);
|
||||||
proxy_url[sizeof(proxy_url) - 1] = '\0';
|
|
||||||
}
|
}
|
||||||
randomize_username = rand_user;
|
randomize_username = rand_user;
|
||||||
randomize_password = rand_pass;
|
randomize_password = rand_pass;
|
||||||
@@ -144,7 +143,8 @@ int load_proxy_list(const char *filename) {
|
|||||||
|
|
||||||
if (temp_count >= temp_capacity) {
|
if (temp_count >= temp_capacity) {
|
||||||
temp_capacity *= 2;
|
temp_capacity *= 2;
|
||||||
Proxy *new_list = (Proxy *)realloc(temp_list, temp_capacity * sizeof(Proxy));
|
Proxy *new_list =
|
||||||
|
(Proxy *)realloc(temp_list, temp_capacity * sizeof(Proxy));
|
||||||
if (!new_list) {
|
if (!new_list) {
|
||||||
free(temp_list);
|
free(temp_list);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
typedef struct DnsCacheEntry {
|
typedef struct DnsCacheEntry {
|
||||||
char hostname[256];
|
char hostname[256];
|
||||||
char ip_str[INET_ADDRSTRLEN];
|
char ip_str[INET6_ADDRSTRLEN];
|
||||||
time_t resolved_at;
|
time_t resolved_at;
|
||||||
struct DnsCacheEntry *next;
|
struct DnsCacheEntry *next;
|
||||||
} DnsCacheEntry;
|
} 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) {
|
for (DnsCacheEntry *e = dns_cache; e; e = e->next) {
|
||||||
if (strcmp(e->hostname, hostname) == 0) {
|
if (strcmp(e->hostname, hostname) == 0) {
|
||||||
if ((now - e->resolved_at) < DNS_CACHE_TTL) {
|
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);
|
pthread_mutex_unlock(&dns_cache_mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ static void dns_cache_insert(const char *hostname, const char *ip_str) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (strcmp(entry->hostname, hostname) == 0) {
|
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;
|
entry->resolved_at = now;
|
||||||
pthread_mutex_unlock(&dns_cache_mutex);
|
pthread_mutex_unlock(&dns_cache_mutex);
|
||||||
return;
|
return;
|
||||||
@@ -73,9 +73,8 @@ static void dns_cache_insert(const char *hostname, const char *ip_str) {
|
|||||||
|
|
||||||
DnsCacheEntry *new_entry = malloc(sizeof(DnsCacheEntry));
|
DnsCacheEntry *new_entry = malloc(sizeof(DnsCacheEntry));
|
||||||
if (new_entry) {
|
if (new_entry) {
|
||||||
strncpy(new_entry->hostname, hostname, sizeof(new_entry->hostname) - 1);
|
snprintf(new_entry->hostname, sizeof(new_entry->hostname), "%s", hostname);
|
||||||
new_entry->hostname[sizeof(new_entry->hostname) - 1] = '\0';
|
snprintf(new_entry->ip_str, sizeof(new_entry->ip_str), "%s", ip_str);
|
||||||
strcpy(new_entry->ip_str, ip_str);
|
|
||||||
new_entry->resolved_at = now;
|
new_entry->resolved_at = now;
|
||||||
new_entry->next = dns_cache;
|
new_entry->next = dns_cache;
|
||||||
dns_cache = new_entry;
|
dns_cache = new_entry;
|
||||||
@@ -150,7 +149,7 @@ static const char *is_private_hostname(const char *hostname, char *out_ip) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
strcpy(out_ip, ip_str);
|
snprintf(out_ip, INET6_ADDRSTRLEN, "%s", ip_str);
|
||||||
dns_cache_insert(hostname, ip_str);
|
dns_cache_insert(hostname, ip_str);
|
||||||
return out_ip;
|
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);
|
curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
|
||||||
|
|
||||||
int valid_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;
|
valid_scheme = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +258,7 @@ static char *url_encode_key(const char *url) {
|
|||||||
EVP_MD_CTX_free(ctx);
|
EVP_MD_CTX_free(ctx);
|
||||||
|
|
||||||
for (int i = 0; i < 16; i++) {
|
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';
|
hash[32] = '\0';
|
||||||
|
|
||||||
@@ -279,7 +279,7 @@ int image_proxy_handler(UrlParams *params) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char resolved_ip[INET_ADDRSTRLEN] = {0};
|
char resolved_ip[INET6_ADDRSTRLEN] = {0};
|
||||||
int domain_check = is_allowed_domain(url, resolved_ip);
|
int domain_check = is_allowed_domain(url, resolved_ip);
|
||||||
if (domain_check == -1) {
|
if (domain_check == -1) {
|
||||||
send_response("Invalid URL scheme");
|
send_response("Invalid URL scheme");
|
||||||
@@ -420,8 +420,7 @@ int image_proxy_handler(UrlParams *params) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(content_type) == 0 ||
|
if (strlen(content_type) == 0 || strncmp(content_type, "image/", 6) != 0) {
|
||||||
strncmp(content_type, "image/", 6) != 0) {
|
|
||||||
free(buf.data);
|
free(buf.data);
|
||||||
free(cache_key);
|
free(cache_key);
|
||||||
send_response("Invalid content type");
|
send_response("Invalid content type");
|
||||||
|
|||||||
@@ -27,12 +27,12 @@ static char *build_images_href(const char *query, int page) {
|
|||||||
if (!href)
|
if (!href)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
snprintf(href, needed, "/images?q=%s", safe_query);
|
|
||||||
if (page > 1) {
|
if (page > 1) {
|
||||||
char page_buf[16];
|
char page_buf[16];
|
||||||
snprintf(page_buf, sizeof(page_buf), "%d", page);
|
snprintf(page_buf, sizeof(page_buf), "%d", page);
|
||||||
strcat(href, "&p=");
|
snprintf(href, needed, "/images?q=%s&p=%s", safe_query, page_buf);
|
||||||
strcat(href, page_buf);
|
} else {
|
||||||
|
snprintf(href, needed, "/images?q=%s", safe_query);
|
||||||
}
|
}
|
||||||
return href;
|
return href;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -376,18 +376,19 @@ static char *build_search_href(const char *query, const char *engine_id,
|
|||||||
if (!href)
|
if (!href)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
snprintf(href, needed, "/search?q=%s", safe_query);
|
if (use_engine && page > 1) {
|
||||||
|
|
||||||
if (use_engine) {
|
|
||||||
strcat(href, "&engine=");
|
|
||||||
strcat(href, engine_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (page > 1) {
|
|
||||||
char page_buf[16];
|
char page_buf[16];
|
||||||
snprintf(page_buf, sizeof(page_buf), "%d", page);
|
snprintf(page_buf, sizeof(page_buf), "%d", page);
|
||||||
strcat(href, "&p=");
|
snprintf(href, needed, "/search?q=%s&engine=%s&p=%s", safe_query, engine_id,
|
||||||
strcat(href, page_buf);
|
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;
|
return href;
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ int settings_save_handler(UrlParams *params) {
|
|||||||
selected_count < ENGINE_COUNT) {
|
selected_count < ENGINE_COUNT) {
|
||||||
strncpy(selected_ids[selected_count], engine_id,
|
strncpy(selected_ids[selected_count], engine_id,
|
||||||
sizeof(selected_ids[selected_count]) - 1);
|
sizeof(selected_ids[selected_count]) - 1);
|
||||||
selected_ids[selected_count][sizeof(selected_ids[selected_count]) - 1] =
|
selected_ids[selected_count]
|
||||||
'\0';
|
[sizeof(selected_ids[selected_count]) - 1] = '\0';
|
||||||
selected_count++;
|
selected_count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,19 +45,37 @@ int settings_save_handler(UrlParams *params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(theme) > 0) {
|
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) {
|
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) {
|
if (engines_present) {
|
||||||
char cookie_value[512];
|
char cookie_value[512];
|
||||||
cookie_value[0] = '\0';
|
cookie_value[0] = '\0';
|
||||||
|
size_t used = 0;
|
||||||
|
cookie_value[0] = '\0';
|
||||||
for (int i = 0; i < selected_count; i++) {
|
for (int i = 0; i < selected_count; i++) {
|
||||||
if (i > 0)
|
const char *id = selected_ids[i];
|
||||||
strcat(cookie_value, ",");
|
size_t idlen = strlen(id);
|
||||||
strcat(cookie_value, selected_ids[i]);
|
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", "/",
|
set_cookie("engines", cookie_value, "Fri, 31 Dec 2038 23:59:59 GMT", "/",
|
||||||
false, false);
|
false, false);
|
||||||
|
|||||||
@@ -89,8 +89,8 @@ void get_available_themes(char ***out_themes, int *out_count) {
|
|||||||
|
|
||||||
void set_default_locale(const char *locale) {
|
void set_default_locale(const char *locale) {
|
||||||
if (locale && strlen(locale) > 0) {
|
if (locale && strlen(locale) > 0) {
|
||||||
strncpy(global_default_locale, locale, sizeof(global_default_locale) - 1);
|
snprintf(global_default_locale, sizeof(global_default_locale), "%s",
|
||||||
global_default_locale[sizeof(global_default_locale) - 1] = '\0';
|
locale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user