// Methods public bool Check (Evidence evidence) { if (evidence == null) return false; string codebase = Assembly.GetCallingAssembly ().CodeBase; Uri local = new Uri (codebase); Url ucode = new Url (codebase); // *both* ApplicationDirectory and Url must be in *Host* evidences bool adir = false; bool url = false; IEnumerator e = evidence.GetHostEnumerator (); while (e.MoveNext ()) { object o = e.Current; if (!adir && (o is ApplicationDirectory)) { ApplicationDirectory ad = (o as ApplicationDirectory); string s = ad.Directory; adir = (String.Compare (s, 0, local.ToString (), 0, s.Length, true, CultureInfo.InvariantCulture) == 0); } else if (!url && (o is Url)) { url = ucode.Equals (o); } // got both ? if (adir && url) return true; } return false; }
public static Zone CreateFromUrl (string url) { if (url == null) throw new ArgumentNullException ("url"); SecurityZone z = SecurityZone.NoZone; if (url.Length == 0) return new Zone (z); Uri uri = new Uri (url); // TODO: apply zone configuration // this is the only way to use the Trusted and Untrusted zones if (z == SecurityZone.NoZone) { // not part of configuration, the use default mapping if (uri.IsFile) { if (File.Exists (uri.LocalPath)) z = SecurityZone.MyComputer; else if (String.Compare ("FILE://", 0, url, 0, 7, true, CultureInfo.InvariantCulture) == 0) z = SecurityZone.Intranet; // non accessible file:// else z = SecurityZone.Internet; } else if (uri.IsLoopback) { // e.g. http://localhost/x z = SecurityZone.Intranet; } else { // all protocols, including unknown ones z = SecurityZone.Internet; } } return new Zone (z); }
// // Public Constructors // public ApplicationDirectory (string name) { if (null == name) throw new ArgumentNullException ("name"); if (name.Length < 1) throw new FormatException (Locale.GetText ("Empty")); #if NET_2_0 directory = name; #else ThrowOnInvalid (name); // swap directory separators (later it will be too late) name = name.Replace ('\\', '/'); // default to file:// scheme is none is specified if (name.IndexOf (':') < 0) { name = String.Format ("{0}{1}{2}", Uri.UriSchemeFile, ((name [0] == '/') ? ":/" : Uri.SchemeDelimiter), name); } try { Uri uri = new Uri (name); // FIXME - *maybe* this can be merged back in corlib's Uri if this // is also required for other security class (and not just this one) directory = uri.Unescape (uri.GetLeftPart (UriPartial.Path), false); if (uri.Scheme == Uri.UriSchemeFile) { string path = uri.Host; if (uri.AbsolutePath.Length > 0) { StringBuilder sb = new StringBuilder (path.ToUpperInvariant ()); string[] segs = uri.Segments; for (int i=0; i < segs.Length - 1; i++) { sb.Append (segs [i].ToUpperInvariant ()); } sb.Append (segs [segs.Length - 1]); path += uri.AbsolutePath; directory = directory.Replace (path, sb.ToString ()); } else directory = directory.Replace (path, path.ToUpperInvariant ()); } } catch (FormatException fe) { string msg = String.Format (Locale.GetText ("Invalid directory {0}"), name); throw new ArgumentException (msg, "name", fe); } #endif }
// internal stuff #if NET_2_0 internal void CheckUrl (string url) { // In .NET 1.x Url class checked the validity of the // URL but that's no more the case in 2.x - but we // still need the check done here int protocolPos = url.IndexOf (Uri.SchemeDelimiter); string u = (protocolPos < 0) ? "file://" + url : url; Uri uri = new Uri (u, false, false); // no * except for the "lone star" case if (uri.Host.IndexOf ('*') >= 1) { string msg = Locale.GetText ("Invalid * character in url"); throw new ArgumentException (msg, "name"); } }
// no exception - we return null if a site couldn't be created // this is useful for creating the default evidence as the majority of URL will be local (file://) // and throw an unrequired exception internal static string UrlToSite (string url) { if (url == null) return null; Uri uri = new Uri (url); if (uri.Scheme == Uri.UriSchemeFile) return null; string site = uri.Host; return IsValid (site) ? site : null; }
// no exception - we return null if a site couldn't be created // this is useful for creating the default evidence as the majority of URL will be local (file://) // and throw an unrequired exception internal static string UrlToSite (string url) { if (url == null) return null; Uri uri = new Uri (url); #if NET_2_0 if (uri.Scheme == Uri.UriSchemeFile) return null; string site = uri.Host; #else string site = uri.Host; if (site == null) site = uri.AbsoluteUri.ToUpper (); // strange but true #endif return IsValid (site) ? site : null; }
private string Prepare (string url) { if (url == null) throw new ArgumentNullException ("Url"); if (url == String.Empty) throw new FormatException (Locale.GetText ("Invalid (empty) Url")); int protocolPos = url.IndexOf (Uri.SchemeDelimiter); // '://' if (protocolPos > 0) { if (url.StartsWith ("file://")) { // convert file url into uppercase url = "file://" + url.Substring (7).ToUpperInvariant (); } else { // add a trailing slash if none (lonely one) is present if (url.LastIndexOf ("/") == protocolPos + 2) url += "/"; } } else { // add file scheme (default) and convert url to uppercase url = "file://" + url.ToUpperInvariant (); } // don't escape and don't reduce (e.g. '.' and '..') Uri uri = new Uri (url, false, false); if ((uri.Host.IndexOf ('*') < 0) || (uri.Host.Length < 2)) // lone star case url = uri.ToString (); else { string msg = Locale.GetText ("Invalid * character in url"); throw new ArgumentException (msg, "name"); } return url; }
// internal #if NET_2_0 private string Prepare (string url) { if (url == null) throw new ArgumentNullException ("Url"); if (url == String.Empty) throw new FormatException (Locale.GetText ("Invalid (empty) Url")); int protocolPos = url.IndexOf (Uri.SchemeDelimiter); // '://' if (protocolPos > 0) { if (url.StartsWith ("file://")) { // convert file url into uppercase url = "file://" + url.Substring (7); } // don't escape and don't reduce (e.g. '.' and '..') Uri uri = new Uri (url, false, false); url = uri.ToString (); } int lastpos = url.Length - 1; if (url [lastpos] == '/') url = url.Substring (0, lastpos); return url; }