File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 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+
2470char * spx_utils_json_escape (char * dst , const char * src , size_t limit )
2571{
2672 size_t i = 0 ;
Original file line number Diff line number Diff 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+
5361char * spx_utils_json_escape (char * dst , const char * src , size_t limit );
5462int spx_utils_str_starts_with (const char * str , const char * prefix );
5563int spx_utils_str_ends_with (const char * str , const char * suffix );
You can’t perform that action at this time.
0 commit comments