--- parser3/src/main/pa_file.C 2026/09/27 17:37:51 1.2 +++ parser3/src/main/pa_file.C 2026/11/03 15:51:20 1.13 @@ -7,6 +7,7 @@ #include "pa_file.h" #include "pa_dir.h" +#include "pa_os.h" #include "pa_charsets.h" #include "pa_http.h" #include "pa_convert_utf.h" @@ -16,7 +17,10 @@ #include #endif -volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.2 2026/09/27 17:37:51 moko Exp $" IDENT_PA_FILE_H; +volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.13 2026/11/03 15:51:20 moko Exp $" IDENT_PA_FILE_H; + +// a tainted path scheme (file://, http:// or parser://) would be accepted +//#define IGNORE_FILE_PREFIX_LANGUAGES // some maybe-undefined constants @@ -45,47 +49,125 @@ bool is_os_absolute_path(const String& p bool is_os_absolute_path(const char* path) { return path[0] && path[0]!=':' && path[1]==':' // DRIVE: (including drive-relative paths, for compatibility) - || path[0]=='\\' && path[1]=='\\' // UNC - || path[0]=='/' && path[1]=='/'; + || path[0]=='\\' && path[1]=='\\'; // UNC, only '\\' is supported as '//' is a document root relative path } #endif -// externs +uint path_scheme(const String& path) { + if(path.starts_with("file://")) + return PA_SCHEME_FILE; + if(path.starts_with("http://")) + return PA_SCHEME_HTTP; + if(path.starts_with("parser://")) + return PA_SCHEME_PARSER; + return PA_SCHEME_NONE; +} -const UTF16* pa_utf16_encode(const char* in, Charset& source_charset); +bool clean_path_scheme(uint scheme, const String& path) { +#ifdef IGNORE_FILE_PREFIX_LANGUAGES + return true; +#else + return path.check_lang(String::L_AS_IS, 0, scheme==PA_SCHEME_PARSER ? 9 /* "parser://" */ : 7 /* "file://" or "http://" */); +#endif +} + +void check_path_scheme(uint scheme, const String& path, uint allowed) { + if(scheme!=PA_SCHEME_FILE && !(allowed&scheme)) + throw Exception(PARSER_RUNTIME, &path, "unsupported path scheme"); + if(!clean_path_scheme(scheme, path)) + throw Exception(PARSER_RUNTIME, &path, "tainted path scheme"); +} + +int path_cmp(const char* path, const char* dir, size_t length) { +#ifdef WIN32 + return strncasecmp(path, dir, length); // mostly for the drive letter +#else + return strncmp(path, dir, length); +#endif +} + +const char* actual_filename(const String& file_spec, const char* fname) { + return strcmp(file_spec.cstr(), fname) ? pa_strcat(", actual filename '", fname, "'") : ""; +} + +const String* file_uri_to_path(const String& uri) { + const String* rest=&uri.mid(7 /* "file://" */, uri.length()); + if(rest->starts_with("localhost/")) + rest=&rest->mid(9 /* "localhost", keeping the slash */, rest->length()); + if(rest->first_char()=='/') { + // file:///C:/path and file://localhost/C:/path: the slash before a drive name is not a part of the path + const String& disk=rest->mid(1, rest->length()); + return is_os_absolute_path(disk) ? &disk : rest; + } + if(is_os_absolute_path(*rest)) + return rest; // file://C:/path, not a standard form but in use +#ifdef WIN32 + if(rest->pos('/')!=STRING_NOT_FOUND) { // file://server/share/path is the UNC path \\server\share\path + String& result=*new String("//"); + result << *rest; + return &result; + } +#endif + return 0; +} // functions #ifdef _MSC_VER +// externs + +const UTF16* pa_utf16_encode(const char* in, Charset& source_charset); #define PA_UTF16_ENC(value) (const wchar_t *)pa_utf16_encode(value, pa_thread_request().charsets.source()) +// non-UTF-8 name should be converted to an error without throw +#define PA_UTF16_VAR(var, value, error, failed) const wchar_t* var=PA_UTF16_ENC(value); if(!var){ errno=error; return failed; } + int pa_stat(const char *pathname, struct stat *buffer){ - return _wstat64(PA_UTF16_ENC(pathname), buffer); + PA_UTF16_VAR(wpathname, pathname, ENOENT, -1) + return _wstat64(wpathname, buffer); } int pa_open(const char *pathname, int flags, int mode){ - return _wopen(PA_UTF16_ENC(pathname), flags, mode); + PA_UTF16_VAR(wpathname, pathname, (flags & _O_CREAT) ? EINVAL : ENOENT, -1) + return _wopen(wpathname, flags, mode); } FILE *pa_fopen(const char *pathname, const char *mode){ - return _wfopen(PA_UTF16_ENC(pathname), PA_UTF16_ENC(mode)); + PA_UTF16_VAR(wpathname, pathname, *mode=='r' ? ENOENT : EINVAL, 0) + PA_UTF16_VAR(wmode, mode, EINVAL, 0) + return _wfopen(wpathname, wmode); } int pa_mkdir(const char *pathname, int){ - return _wmkdir(PA_UTF16_ENC(pathname)); + PA_UTF16_VAR(wpathname, pathname, EINVAL, -1) + return _wmkdir(wpathname); } int pa_rmdir(const char *pathname){ - return _wrmdir(PA_UTF16_ENC(pathname)); + PA_UTF16_VAR(wpathname, pathname, ENOENT, -1) + return _wrmdir(wpathname); +} + +static bool pa_is_dir(const wchar_t* wpathname){ + DWORD attributes=GetFileAttributesW(wpathname); + return attributes!=INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY); } int pa_rename(const char *oldpath, const char *newpath){ - return _wrename(PA_UTF16_ENC(oldpath), PA_UTF16_ENC(newpath)); + PA_UTF16_VAR(woldpath, oldpath, ENOENT, -1) + PA_UTF16_VAR(wnewpath, newpath, ENOENT, -1) + if(pa_set_errno(MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING))) + return 0; + // like rename on unix, a directory replaces the empty one: windows does not replace directories at all + if(errno==EACCES && pa_is_dir(woldpath) && pa_is_dir(wnewpath) && _wrmdir(wnewpath)==0) + return pa_set_errno(MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING)) ? 0 : -1; + return -1; } int pa_unlink(const char *pathname){ - return _wunlink(PA_UTF16_ENC(pathname)); + PA_UTF16_VAR(wpathname, pathname, ENOENT, -1) + return _wunlink(wpathname); } #else @@ -117,11 +199,11 @@ int pa_get_valid_file_options_count(Hash struct File_read_action_info { char **data; size_t *data_size; char* buf; uint64_t offset; size_t limit; -}; +}; #endif static void file_read_action(struct stat& finfo, int f, const String& file_spec, void *context) { - File_read_action_info& info = *static_cast(context); + File_read_action_info& info = *static_cast(context); size_t to_read_size = check_file_size(info.limit && info.limit < (size_t)finfo.st_size ? info.limit : (size_t)finfo.st_size, &file_spec); if(to_read_size) { if(info.offset) @@ -148,7 +230,7 @@ File_read_result file_read_binary(const return result; } -File_read_result file_read(Request_charsets& charsets, const String& file_spec, +File_read_result file_read(Request_charsets& charsets, const String& file_spec, bool as_text, HashStringValue *options, bool fail_on_read_problem, size_t offset = 0, size_t limit = 0, bool transcode_text_result = true) { @@ -159,9 +241,9 @@ File_read_result file_read(Request_chars throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } - File_read_action_info info = {&result.str, &result.length, 0, offset, limit}; + File_read_action_info info = {&result.str, &result.length, 0, offset, limit}; - result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); + result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); if(as_text){ if(result.success){ @@ -187,7 +269,7 @@ File_read_result file_read(Request_chars return result; } -File_read_result file_load(Request& r, const String& file_spec, +File_read_result file_load(Request& r, const String& file_spec, bool as_text, HashStringValue *options, bool fail_on_read_problem, bool transcode_text_result) { @@ -252,10 +334,10 @@ bool file_read_action_under_lock(const S // first open, next stat: // directory update of NTFS hard links performed on open. - // ex: + // ex: // a.html:^test[] and b.html hardlink to a.html // user inserts ! before ^test in a.html - // directory entry of b.html in NTFS not updated at once, + // directory entry of b.html in NTFS not updated at once, // they delay update till open, so we would receive "!^test[" string // if would do stat, next open. // later: it seems, even this does not help sometimes @@ -263,29 +345,29 @@ bool file_read_action_under_lock(const S try { int pa_errno=pa_lock_shared_blocking(f); if(pa_errno!=0) - throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname); + throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d)%s", strerror(pa_errno), pa_errno, actual_filename(file_spec, fname)); struct stat finfo; if(pa_fstat(f, &finfo)!=0) throw Exception("file.missing", // hardly possible: we just opened it OK - &file_spec, "stat failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); + &file_spec, "stat failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname)); check_safe_mode(finfo, file_spec, fname); - action(finfo, f, file_spec, context); + action(finfo, f, file_spec, context); } catch(...) { - pa_unlock(f);close(f); + pa_unlock(f);close(f); if(fail_on_read_problem) rethrow; return false; - } + } - pa_unlock(f);close(f); + pa_unlock(f);close(f); return true; } else { if(fail_on_read_problem) throw Exception(errno==EACCES ? "file.access" : (errno==ENOENT || errno==ENOTDIR || errno==ENODEV) ? "file.missing" : 0, - &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname); + &file_spec, "%s failed: %s (%d)%s", action_name, strerror(errno), errno, actual_filename(file_spec, fname)); return false; } } @@ -294,7 +376,7 @@ void create_dir_for_file(const String& f const char *str=file_spec.taint_cstr(String::L_FILE_SPEC); if(str[0]){ const char *pos=str+1; - while((pos=strpbrk(pos, "/\\")) && pos[1]) { // to avoid trailing /, see #1166 + while((pos=strchr(pos, '/')) && pos[1]) { // to avoid trailing /, see #1166 pa_mkdir(pa_strdup(str,pos-str), 0775); pos++; } @@ -312,7 +394,7 @@ bool file_write_action_under_lock(const if((f=pa_open(fname, O_CREAT | O_RDWR | (as_text ? _O_TEXT : _O_BINARY) | (do_append ? O_APPEND : PA_O_TRUNC), 0664))>=0) { int pa_errno=do_block ? pa_lock_exclusive_blocking(f) : pa_lock_exclusive_nonblocking(f); if(pa_errno!=0) { - Exception e("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname); + Exception e("file.lock", &file_spec, "exclusive lock failed: %s (%d)%s", strerror(pa_errno), pa_errno, actual_filename(file_spec, fname)); close(f); if(fail_on_lock_problem) throw e; @@ -342,7 +424,7 @@ bool file_write_action_under_lock(const pa_unlock(f);close(f); return true; } else - throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname); + throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d)%s", action_name, strerror(errno), errno, actual_filename(file_spec, fname)); // here should be nothing, see rethrow above } @@ -382,8 +464,8 @@ void file_write( Request_charsets& charsets, const String& file_spec, const char* data, - size_t size, - bool as_text, + size_t size, + bool as_text, bool do_append, Charset* asked_charset) { @@ -410,7 +492,7 @@ static size_t get_dir(char* fname, size_ size_t pos=0; for(pos=helper_length; pos; pos--){ char c=fname[pos-1]; - if(c=='/' || c=='\\'){ + if(c=='/'){ fname[pos-1]=0; dir=true; } else if(dir) break; @@ -427,7 +509,7 @@ bool entry_exists(const char* fname, str } bool entry_exists(const String& file_spec) { - return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0); + return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0); } static bool entry_ifdir(char *fname, bool need_dir) { @@ -435,7 +517,7 @@ static bool entry_ifdir(char *fname, boo size_t size=strlen(fname); while(size) { char c=fname[size-1]; - if(c=='/' || c=='\\') + if(c=='/') fname[--size]=0; else break; @@ -476,47 +558,47 @@ static void rmdir(const String& file_spe } bool file_delete(const String& file_spec, bool fail_on_problem, bool keep_empty_dirs) { - const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); + const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); if(pa_unlink(fname)!=0) { if(fail_on_problem) throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, - &file_spec, "unlink failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); + &file_spec, "unlink failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname)); else return false; } if(!keep_empty_dirs) - rmdir(file_spec, 1); + rmdir(file_spec, 1); return true; } void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs) { - const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC); - const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC); + const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC); + const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC); - create_dir_for_file(new_spec); + create_dir_for_file(new_spec); if(pa_rename(old_spec_cstr, new_spec_cstr)!=0) throw Exception(errno==EACCES ? "file.access" : errno==ENOENT ? "file.missing" : 0, - &old_spec, "rename failed: %s (%d), actual filename '%s' to '%s'", strerror(errno), errno, old_spec_cstr, new_spec_cstr); + &old_spec, "rename to '%s' failed: %s (%d)%s", new_spec_cstr, strerror(errno), errno, actual_filename(old_spec, old_spec_cstr)); if(!keep_empty_dirs) - rmdir(old_spec, 1); + rmdir(old_spec, 1); } bool file_exist(const String& file_spec) { - return entry_ifdir(file_spec, false); + return entry_ifdir(file_spec, false); } bool dir_exists(const String& file_spec) { - return entry_ifdir(file_spec, true); + return entry_ifdir(file_spec, true); } const String* file_exist(const String& path, const String& name) { String& result=*new String(path); - if(path.last_char() != '/' && path.last_char() != '\\') + if(path.last_char() != '/') result << "/"; result << name; return file_exist(result)?&result:0; @@ -527,11 +609,11 @@ bool file_executable(const String& file_ } bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem) { - const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); + const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); struct stat finfo; if(pa_stat(fname, &finfo)!=0) { if(fail_on_read_problem) - throw Exception("file.missing", &file_spec, "getting file size failed: %s (%d), real filename '%s'", strerror(errno), errno, fname); + throw Exception("file.missing", &file_spec, "getting file size failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname)); else return false; } @@ -548,7 +630,7 @@ size_t check_file_size(uint64_t size, co return (size_t)size; } -/** +/** String related functions */