--- parser3/src/main/pa_file.C 2026/09/28 00:23:20 1.3 +++ parser3/src/main/pa_file.C 2026/10/01 11:48:37 1.8 @@ -16,7 +16,10 @@ #include #endif -volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.3 2026/09/28 00:23:20 moko Exp $" IDENT_PA_FILE_H; +volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.8 2026/10/01 11:48:37 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,10 +48,49 @@ 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 + +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; +} + +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"); +#ifndef IGNORE_FILE_PREFIX_LANGUAGES + if(!path.check_lang(String::L_AS_IS, 0, scheme==PA_SCHEME_PARSER ? 9 /* "parser://" */ : 7 /* "file://" or "http://" */)) + throw Exception(PARSER_RUNTIME, &path, "tainted path scheme"); +#endif } + +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; +} // externs @@ -294,7 +336,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++; } @@ -410,7 +452,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; @@ -435,7 +477,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; @@ -516,7 +558,7 @@ bool dir_exists(const String& file_spec) 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;