internal static string AlwaysSafeFilename(string filename) { if (!string.IsNullOrEmpty(filename)) { if (StringUtil.StartsWithIgnoreCase(filename, "http:")) { return(filename); } try { if (!Path.IsPathRooted(filename)) { return(filename); } } catch { return(null); } try { filename = Path.GetFileName(FullPathWithAssert(filename)); } catch { filename = null; } } return(filename); }
// // Internal Helper to strip a full path to just filename.ext when caller // does not have path discovery to the path (used for sane error handling). // internal static string SafeFilename(string filename) { if (string.IsNullOrEmpty(filename)) { return(filename); } // configuration file can be an http URL in IE if (StringUtil.StartsWithIgnoreCase(filename, HTTP_PREFIX)) { return(filename); } // // If it is a relative path, return it as is. // This could happen if the exception was constructed from the serialization constructor, // and the caller did not have PathDiscoveryPermission for the file. // try { if (!Path.IsPathRooted(filename)) { return(filename); } } catch { return(null); } try { // Confirm that it is a full path. // GetFullPath will also Demand PathDiscovery for the resulting path string fullPath = Path.GetFullPath(filename); } catch (SecurityException) { // Get just the name of the file without the directory part. try { string fullPath = FullPathWithAssert(filename); filename = Path.GetFileName(fullPath); } catch { filename = null; } } catch { filename = null; } return(filename); }
// // Internal Helper to always strip a full path to just filename.ext. // internal static string AlwaysSafeFilename(string filename) { if (string.IsNullOrEmpty(filename)) { return(filename); } // configuration file can be an http URL in IE if (StringUtil.StartsWithIgnoreCase(filename, HTTP_PREFIX)) { return(filename); } // // If it is a relative path, return it as is. // This could happen if the exception was constructed from the serialization constructor, // and the caller did not have PathDiscoveryPermission for the file. // try { if (!Path.IsPathRooted(filename)) { return(filename); } } catch { return(null); } // Get just the name of the file without the directory part. try { filename = ExtractFileNameWithAssert(filename); } catch { filename = null; } return(filename); }
private ClientConfigPaths(string exePath, bool includeUserConfig) { this._includesUserConfig = includeUserConfig; Assembly exeAssembly = null; string codeBase = null; string applicationFilename = null; if (exePath == null) { AppDomainSetup setupInformation = AppDomain.CurrentDomain.SetupInformation; this._applicationConfigUri = setupInformation.ConfigurationFile; exeAssembly = Assembly.GetEntryAssembly(); if (exeAssembly != null) { this._hasEntryAssembly = true; codeBase = exeAssembly.CodeBase; bool flag = false; if (StringUtil.StartsWithIgnoreCase(codeBase, "file:///")) { flag = true; codeBase = codeBase.Substring("file:///".Length); } else if (StringUtil.StartsWithIgnoreCase(codeBase, "file://")) { flag = true; codeBase = codeBase.Substring("file:".Length); } if (flag) { codeBase = codeBase.Replace('/', '\\'); applicationFilename = codeBase; } else { codeBase = exeAssembly.EscapedCodeBase; } } else { StringBuilder buffer = new StringBuilder(260); Microsoft.Win32.UnsafeNativeMethods.GetModuleFileName(new HandleRef(null, IntPtr.Zero), buffer, buffer.Capacity); codeBase = Path.GetFullPath(buffer.ToString()); applicationFilename = codeBase; } } else { codeBase = Path.GetFullPath(exePath); if (!FileUtil.FileExists(codeBase, false)) { throw ExceptionUtil.ParameterInvalid("exePath"); } applicationFilename = codeBase; } if (this._applicationConfigUri == null) { this._applicationConfigUri = codeBase + ".config"; } this._applicationUri = codeBase; if ((exePath == null) && this._includesUserConfig) { bool isHttp = StringUtil.StartsWithIgnoreCase(this._applicationConfigUri, "http://"); this.SetNamesAndVersion(applicationFilename, exeAssembly, isHttp); if (this.IsClickOnceDeployed(AppDomain.CurrentDomain)) { string data = AppDomain.CurrentDomain.GetData("DataDirectory") as string; string str4 = this.Validate(this._productVersion, false); if (Path.IsPathRooted(data)) { this._localConfigDirectory = this.CombineIfValid(data, str4); this._localConfigFilename = this.CombineIfValid(this._localConfigDirectory, "user.config"); } } else if (!isHttp) { string str5 = this.Validate(this._companyName, true); string str6 = this.Validate(AppDomain.CurrentDomain.FriendlyName, true); string str7 = !string.IsNullOrEmpty(this._applicationUri) ? this._applicationUri.ToLower(CultureInfo.InvariantCulture) : null; string str8 = !string.IsNullOrEmpty(str6) ? str6 : this.Validate(this._productName, true); string typeAndHashSuffix = this.GetTypeAndHashSuffix(AppDomain.CurrentDomain, str7); string str10 = (!string.IsNullOrEmpty(str8) && !string.IsNullOrEmpty(typeAndHashSuffix)) ? (str8 + typeAndHashSuffix) : null; string str11 = this.Validate(this._productVersion, false); string str12 = this.CombineIfValid(this.CombineIfValid(str5, str10), str11); string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); if (Path.IsPathRooted(folderPath)) { this._roamingConfigDirectory = this.CombineIfValid(folderPath, str12); this._roamingConfigFilename = this.CombineIfValid(this._roamingConfigDirectory, "user.config"); } string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); if (Path.IsPathRooted(path)) { this._localConfigDirectory = this.CombineIfValid(path, str12); this._localConfigFilename = this.CombineIfValid(this._localConfigDirectory, "user.config"); } } } }
private ClientConfigPaths(string exePath, bool includeUserConfig) { _includesUserConfig = includeUserConfig; Assembly exeAssembly = null; string applicationUri = null; string applicationFilename = null; // get the assembly and applicationUri for the file if (exePath == null) { // First check if a configuration file has been set for this app domain. If so, we will use that. // The CLR would already have normalized this, so no further processing necessary. AppDomain domain = AppDomain.CurrentDomain; AppDomainSetup setup = domain.SetupInformation; _applicationConfigUri = setup.ConfigurationFile; // Now figure out the application path. exeAssembly = Assembly.GetEntryAssembly(); if (exeAssembly != null) { _hasEntryAssembly = true; applicationUri = exeAssembly.CodeBase; bool isFile = false; // If it is a local file URI, convert it to its filename, without invoking Uri class. // example: "file:///C:/WINNT/Microsoft.NET/Framework/v2.0.x86fre/csc.exe" if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_LOCAL)) { isFile = true; applicationUri = applicationUri.Substring(FILE_URI_LOCAL.Length); } // If it is a UNC file URI, convert it to its filename, without invoking Uri class. // example: "file://server/share/csc.exe" else if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_UNC)) { isFile = true; applicationUri = applicationUri.Substring(FILE_URI.Length); } if (isFile) { applicationUri = applicationUri.Replace('/', '\\'); applicationFilename = applicationUri; } else { applicationUri = exeAssembly.EscapedCodeBase; } } else { StringBuilder sb = new StringBuilder(MAX_PATH); UnsafeNativeMethods.GetModuleFileName(new HandleRef(null, IntPtr.Zero), sb, sb.Capacity); applicationUri = Path.GetFullPath(sb.ToString()); applicationFilename = applicationUri; } } else { applicationUri = Path.GetFullPath(exePath); if (!FileUtil.FileExists(applicationUri, false)) { throw ExceptionUtil.ParameterInvalid("exePath"); } applicationFilename = applicationUri; } // Fallback if we haven't set the app config file path yet. if (_applicationConfigUri == null) { _applicationConfigUri = applicationUri + ConfigExtension; } // Set application path _applicationUri = applicationUri; // In the case when exePath was explicitly supplied, we will not be able to // construct user.config paths, so quit here. if (exePath != null) { return; } // Skip expensive initialization of user config file information if requested. if (!_includesUserConfig) { return; } bool isHttp = StringUtil.StartsWithIgnoreCase(_applicationConfigUri, HTTP_URI); SetNamesAndVersion(applicationFilename, exeAssembly, isHttp); // Check if this is a clickonce deployed application. If so, point the user config // files to the clickonce data directory. if (this.IsClickOnceDeployed(AppDomain.CurrentDomain)) { string dataPath = AppDomain.CurrentDomain.GetData(ClickOnceDataDirectory) as string; string versionSuffix = Validate(_productVersion, false); // NOTE: No roaming config for clickonce - not supported. if (Path.IsPathRooted(dataPath)) { _localConfigDirectory = CombineIfValid(dataPath, versionSuffix); _localConfigFilename = CombineIfValid(_localConfigDirectory, UserConfigFilename); } } else if (!isHttp) { // If we get the config from http, we do not have a roaming or local config directory, // as it cannot be edited by the app in those cases because it does not have Full Trust. // suffix for user config paths string part1 = Validate(_companyName, true); string validAppDomainName = Validate(AppDomain.CurrentDomain.FriendlyName, true); string applicationUriLower = !String.IsNullOrEmpty(_applicationUri) ? _applicationUri.ToLower(CultureInfo.InvariantCulture) : null; string namePrefix = !String.IsNullOrEmpty(validAppDomainName) ? validAppDomainName : Validate(_productName, true); string hashSuffix = GetTypeAndHashSuffix(AppDomain.CurrentDomain, applicationUriLower); string part2 = (!String.IsNullOrEmpty(namePrefix) && !String.IsNullOrEmpty(hashSuffix)) ? namePrefix + hashSuffix : null; string part3 = Validate(_productVersion, false); string dirSuffix = CombineIfValid(CombineIfValid(part1, part2), part3); string roamingFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); if (Path.IsPathRooted(roamingFolderPath)) { _roamingConfigDirectory = CombineIfValid(roamingFolderPath, dirSuffix); _roamingConfigFilename = CombineIfValid(_roamingConfigDirectory, UserConfigFilename); } string localFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); if (Path.IsPathRooted(localFolderPath)) { _localConfigDirectory = CombineIfValid(localFolderPath, dirSuffix); _localConfigFilename = CombineIfValid(_localConfigDirectory, UserConfigFilename); } } }