Skip to content

Commit

Permalink
Added new wwwroot_get_simplified_abs_path() helper to FileCache. Also…
Browse files Browse the repository at this point in the history
… improved the other path helper methods.
  • Loading branch information
Relintai committed Mar 10, 2024
1 parent b96205f commit 6addb02
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 30 additions & 1 deletion modules/web/file_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ bool FileCache::wwwroot_has_file(const String &file_path) {

String fp = _wwwroot_abs + file_path;

fp = fp.simplify_path();

// Don't allow going outside wwwroot
if (!fp.begins_with(_wwwroot_abs)) {
return false;
}

if (!FileAccess::exists(fp)) {
return false;
}
Expand All @@ -92,7 +99,7 @@ bool FileCache::wwwroot_has_file(const String &file_path) {
String absp = f->get_path_absolute();
memdelete(f);

//likely a directory walking attempt. e.g. ../../../../../etc/passwd
// likely a directory walking attempt. e.g. ../../../../../etc/passwd
if (!absp.begins_with(_wwwroot_abs)) {
return false;
}
Expand All @@ -107,6 +114,13 @@ String FileCache::wwwroot_get_file_abspath(const String &file_path) {

String fp = _wwwroot_abs + file_path;

fp = fp.simplify_path();

// Don't allow going outside wwwroot
if (!fp.begins_with(_wwwroot_abs)) {
return String();
}

if (!FileAccess::exists(fp)) {
return String();
}
Expand Down Expand Up @@ -138,6 +152,19 @@ String FileCache::wwwroot_get_file_abspath(const String &file_path) {
return absp;
}

String FileCache::wwwroot_get_simplified_abs_path(const String &file_path) {
String fp = _wwwroot_abs + file_path;

fp = fp.simplify_path();

// Don't allow going outside wwwroot
if (!fp.begins_with(_wwwroot_abs)) {
return String();
}

return fp;
}

bool FileCache::get_cached_body(const String &path, String *body) {
//TODO ERROR MACRO body == null

Expand Down Expand Up @@ -264,6 +291,8 @@ void FileCache::_bind_methods() {
ClassDB::bind_method(D_METHOD("wwwroot_has_file", "file_path"), &FileCache::wwwroot_has_file);
ClassDB::bind_method(D_METHOD("wwwroot_get_file_abspath", "file_path"), &FileCache::wwwroot_get_file_abspath);

ClassDB::bind_method(D_METHOD("wwwroot_get_simplified_abs_path", "file_path"), &FileCache::wwwroot_get_simplified_abs_path);

ClassDB::bind_method(D_METHOD("get_cached_body", "path"), &FileCache::get_cached_body_bind);
ClassDB::bind_method(D_METHOD("has_cached_body", "path"), &FileCache::has_cached_body);
ClassDB::bind_method(D_METHOD("set_cached_body", "path", "body"), &FileCache::set_cached_body);
Expand Down
2 changes: 2 additions & 0 deletions modules/web/file_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class FileCache : public Reference {
//e.g. http://127.0.0.1/a/b/d.jpg -> /a/b/d.jpg
bool wwwroot_has_file(const String &file_path);
String wwwroot_get_file_abspath(const String &file_path);

String wwwroot_get_simplified_abs_path(const String &file_path);

bool get_cached_body(const String &path, String *body);
bool has_cached_body(const String &path);
Expand Down

0 comments on commit 6addb02

Please sign in to comment.