Skip to content

Commit

Permalink
Ensure we verify the output path before parsing the log.
Browse files Browse the repository at this point in the history
Closes #2790
  • Loading branch information
allinurl committed Feb 8, 2025
1 parent 43b9fc2 commit 4c4e3b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,8 @@ read_option_args (int argc, char **argv) {
case 'o':
if (!valid_output_type (optarg))
FATAL ("Invalid filename extension. It must be any of .csv, .json, or .html\n");
if (!is_writable_path(optarg))
FATAL("Invalid or unwritable path.");
if (conf.output_format_idx < MAX_OUTFORMATS)
conf.output_formats[conf.output_format_idx++] = optarg;
break;
Expand Down
23 changes: 23 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <inttypes.h>
#include <regex.h>
#include <pthread.h>
#include <unistd.h>

#include <netinet/in.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -1221,3 +1222,25 @@ unescape_str (const char *src) {

return dest;
}

int
is_writable_path (const char *path) {
if (access (path, W_OK) == 0) {
return 1; // Path is writable
} else {
switch (errno) {
case ENOENT:
fprintf (stderr, "Path does not exist: %s\n", path);
return 0;
case EACCES:
fprintf (stderr, "No write permission for path: %s\n", path);
return 0;
case EROFS:
fprintf (stderr, "Path is on a read-only file system: %s\n", path);
return 0;
default:
fprintf (stderr, "Unknown error (errno %d) for path: %s\n", errno, path);
return 0;
}
}
}
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ int intlen (uint64_t num);
int invalid_ipaddr (const char *str, int *ipvx);
int ip_in_range (const char *ip);
int is_valid_http_status (int code);
int is_writable_path (const char *path);
int ptr2int (char *ptr);
int str2int (const char *date);
int str_inarray (const char *s, const char *arr[], int size);
Expand Down

0 comments on commit 4c4e3b7

Please sign in to comment.