Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Sep 11, 2024
1 parent 74293dd commit 0f061fe
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/spx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,52 @@
#include <string.h>
#include "spx_utils.h"

char * spx_utils_resolve_confined_file_absolute_path(
const char * root_dir,
const char * relative_path,
const char * suffix,
char * dst,
size_t size
) {
if (size < PATH_MAX) {
spx_utils_die("size < PATH_MAX");
}

char absolute_file_path[PATH_MAX];

snprintf(
absolute_file_path,
sizeof(absolute_file_path),
"%s%s%s",
root_dir,
relative_path,
suffix == NULL ? "" : suffix
);

if (realpath(absolute_file_path, dst) == NULL) {
return NULL;
}

char root_dir_real_path[PATH_MAX];
if (realpath(root_dir, root_dir_real_path) == NULL) {
return NULL;
}

char expected_path_prefix[PATH_MAX + 1];
snprintf(
expected_path_prefix,
sizeof(expected_path_prefix),
"%s/",
root_dir_real_path
);

if (! spx_utils_str_starts_with(dst, expected_path_prefix)) {
return NULL;
}

return dst;
}

char * spx_utils_json_escape(char * dst, const char * src, size_t limit)
{
size_t i = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/spx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ do { \
} \
} while (0)

char * spx_utils_resolve_confined_file_absolute_path(
const char * root_dir,
const char * relative_path,
const char * suffix,
char * dst,
size_t size
);

char * spx_utils_json_escape(char * dst, const char * src, size_t limit);
int spx_utils_str_starts_with(const char * str, const char * prefix);
int spx_utils_str_ends_with(const char * str, const char * suffix);
Expand Down

0 comments on commit 0f061fe

Please sign in to comment.