Skip to content

Commit 0f061fe

Browse files
committed
wip
1 parent 74293dd commit 0f061fe

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/spx_utils.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,52 @@
2121
#include <string.h>
2222
#include "spx_utils.h"
2323

24+
char * spx_utils_resolve_confined_file_absolute_path(
25+
const char * root_dir,
26+
const char * relative_path,
27+
const char * suffix,
28+
char * dst,
29+
size_t size
30+
) {
31+
if (size < PATH_MAX) {
32+
spx_utils_die("size < PATH_MAX");
33+
}
34+
35+
char absolute_file_path[PATH_MAX];
36+
37+
snprintf(
38+
absolute_file_path,
39+
sizeof(absolute_file_path),
40+
"%s%s%s",
41+
root_dir,
42+
relative_path,
43+
suffix == NULL ? "" : suffix
44+
);
45+
46+
if (realpath(absolute_file_path, dst) == NULL) {
47+
return NULL;
48+
}
49+
50+
char root_dir_real_path[PATH_MAX];
51+
if (realpath(root_dir, root_dir_real_path) == NULL) {
52+
return NULL;
53+
}
54+
55+
char expected_path_prefix[PATH_MAX + 1];
56+
snprintf(
57+
expected_path_prefix,
58+
sizeof(expected_path_prefix),
59+
"%s/",
60+
root_dir_real_path
61+
);
62+
63+
if (! spx_utils_str_starts_with(dst, expected_path_prefix)) {
64+
return NULL;
65+
}
66+
67+
return dst;
68+
}
69+
2470
char * spx_utils_json_escape(char * dst, const char * src, size_t limit)
2571
{
2672
size_t i = 0;

src/spx_utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ do { \
5050
} \
5151
} while (0)
5252

53+
char * spx_utils_resolve_confined_file_absolute_path(
54+
const char * root_dir,
55+
const char * relative_path,
56+
const char * suffix,
57+
char * dst,
58+
size_t size
59+
);
60+
5361
char * spx_utils_json_escape(char * dst, const char * src, size_t limit);
5462
int spx_utils_str_starts_with(const char * str, const char * prefix);
5563
int spx_utils_str_ends_with(const char * str, const char * suffix);

0 commit comments

Comments
 (0)