public void Constructor_PermissionSet_Unrestricted() { PermissionSet pset = new PermissionSet(PermissionState.Unrestricted); PolicyStatement ps = new PolicyStatement(pset); Assert.AreEqual(PolicyStatementAttribute.Nothing, ps.Attributes, "Attributes"); Assert.AreEqual(String.Empty, ps.AttributeString, "AttributeString"); Assert.AreEqual(Unrestricted.ToString(), ps.PermissionSet.ToString(), "PermissionSet"); Assert.AreEqual(ps.ToXml().ToString(), ps.Copy().ToXml().ToString(), "Copy"); }
protected override void DoCopy(IDataObject dataObject) { // Serialize the selected elements and add it to the data object. StringBuilder xmlData = new StringBuilder(); foreach (UIElement element in Elements) { string xml; xml = XamlWriter.Save(element); xmlData.Append(xml); } // Set the data object as XML format. dataObject.SetData(DataFormats.Xaml, xmlData.ToString()); // // we need to copy the permission set on the clipboard for // the Clipboard class methods. See security note for details. // PermissionSet permSet = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin(); string setString = permSet.ToString(); Debug.Assert(setString.Length > 0); dataObject.SetData(DataFormats.ApplicationTrust, setString); }
private static XmlElement GetCurrentCLRPermissions(string targetZone) { SecurityZone zone; switch (targetZone) { case LocalIntranet: zone = SecurityZone.Intranet; break; case Internet: zone = SecurityZone.Internet; break; default: throw new ArgumentException(String.Empty /* no message */, nameof(targetZone)); } var evidence = new Evidence(new EvidenceBase[] { new Zone(zone), new System.Runtime.Hosting.ActivationArguments(new System.ApplicationIdentity("")) }, null); PermissionSet sandbox = SecurityManager.GetStandardSandbox(evidence); string resultInString = sandbox.ToString(); if (!string.IsNullOrEmpty(resultInString)) { var doc = new XmlDocument(); // CA3057: DoNotUseLoadXml. Suppressed since the xml being loaded is a string representation of the PermissionSet. doc.LoadXml(resultInString); return(doc.DocumentElement); } return(null); }
public void Constructor_PermissionSet_Null() { PolicyStatement ps = new PolicyStatement(null); Assert.AreEqual(PolicyStatementAttribute.Nothing, ps.Attributes, "Attributes"); Assert.AreEqual(String.Empty, ps.AttributeString, "AttributeString"); Assert.AreEqual(Empty.ToString(), ps.PermissionSet.ToString(), "PermissionSet"); Assert.AreEqual(ps.ToXml().ToString(), ps.Copy().ToXml().ToString(), "Copy"); }
public static string[] PermissionSetToIdentityList(PermissionSet permissionSet) { string xml = (permissionSet != null) ? permissionSet.ToString() : "<PermissionSet/>"; XmlDocument document = new XmlDocument(); document.LoadXml(xml); return(XmlToIdentityList(document.DocumentElement)); }
public static string[] PermissionSetToIdentityList(PermissionSet permissionSet) { string psXml = permissionSet?.ToString() ?? "<PermissionSet/>"; var psDocument = new XmlDocument(); // CA3057: DoNotUseLoadXml. Suppressed since 'psXml' is a trusted or a constant string. psDocument.LoadXml(psXml); return(XmlToIdentityList(psDocument.DocumentElement)); }
public static void PermissionRequestEvidenceCallMethods() { PermissionSet ps = new PermissionSet(new PermissionState()); PermissionRequestEvidence pre = new PermissionRequestEvidence(ps, ps, ps); PermissionRequestEvidence obj = pre.Copy(); string str = ps.ToString(); SecurityElement se = new SecurityElement(""); ps.FromXml(se); se = ps.ToXml(); }
public void ResolvePolicy_CurrentAssemblyEvidence() { HostSecurityManager hsm = new HostSecurityManager(); Assembly a = Assembly.GetExecutingAssembly(); PermissionSet ps = hsm.ResolvePolicy(a.Evidence); PermissionSet expected = SecurityManager.ResolvePolicy(a.Evidence); Assert.AreEqual(expected.ToString(), ps.ToString(), "PermissionSet"); }
public void GetSet() { PermissionSetCollection psc = new PermissionSetCollection(); PermissionSet unr = new PermissionSet(PermissionState.Unrestricted); psc.Add(unr); PermissionSet ps = psc.GetSet(0); Assert.AreEqual(unr.ToString(), ps.ToString(), "Same XML"); Assert.IsTrue(Object.ReferenceEquals(unr, ps), "Same Object Reference"); }
internal static XmlDocument PermissionSetToXml(PermissionSet ps) { XmlDocument document = new XmlDocument(); string xml = (ps != null) ? ps.ToString() : "<PermissionSet/>"; document.LoadXml(xml); XmlDocument document2 = new XmlDocument(); XmlElement newChild = XmlUtil.CloneElementToDocument(document.DocumentElement, document2, "urn:schemas-microsoft-com:asm.v2"); document2.AppendChild(newChild); return(document2); }
internal static XmlDocument PermissionSetToXml(PermissionSet ps) { XmlDocument inputDocument = new XmlDocument(); string xml = (ps != null) ? ps.ToString() : "<PermissionSet/>"; // CA3057: DoNotUseLoadXml. Suppressed since 'xml' is a trusted or a constant string. inputDocument.LoadXml(xml); XmlDocument outputDocument = new XmlDocument(); XmlElement psElement = XmlUtil.CloneElementToDocument(inputDocument.DocumentElement, outputDocument, XmlNamespaces.asmv2); outputDocument.AppendChild(psElement); return(outputDocument); }
public void ToXml() { PermissionSetCollection psc = new PermissionSetCollection(); SecurityElement se = psc.ToXml(); Assert.IsNull(se.Children, "Children==null for 0"); PermissionSet unr = new PermissionSet(PermissionState.Unrestricted); psc.Add(unr); se = psc.ToXml(); Assert.AreEqual(1, se.Children.Count, "Children==1"); Assert.AreEqual(unr.ToString(), se.Children[0].ToString(), "XML"); }
protected override void DoCopy(IDataObject dataObject) { StringBuilder stringBuilder = new StringBuilder(); foreach (UIElement obj in base.Elements) { string value = XamlWriter.Save(obj); stringBuilder.Append(value); } dataObject.SetData(DataFormats.Xaml, stringBuilder.ToString()); PermissionSet permissionSet = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin(); string data = permissionSet.ToString(); dataObject.SetData(DataFormats.ApplicationTrust, data); }
public void Copy_References() { PermissionSet none = new PermissionSet(PermissionState.None); PermissionSetCollection psc = new PermissionSetCollection(); psc.Add(none); PermissionSetCollection copy = psc.Copy(); Assert.AreEqual(1, copy.PermissionSets.Count, "Count-1"); string before = psc.ToString(); none.AddPermission(new SecurityPermission(SecurityPermissionFlag.Assertion)); Assert.AreEqual(none.ToString(), psc.PermissionSets[0].ToString(), "psc"); Assert.AreEqual(before, copy.ToString(), "copy"); }
public static void PermissionSetCallMethods() { PermissionSet ps = new PermissionSet(new PermissionState()); ps.Assert(); bool containspermissions = ps.ContainsNonCodeAccessPermissions(); PermissionSet ps2 = ps.Copy(); ps.CopyTo(new int[1], 0); ps.Demand(); ps.Equals(ps2); System.Collections.IEnumerator ie = ps.GetEnumerator(); int hash = ps.GetHashCode(); PermissionSet ps3 = ps.Intersect(ps2); bool isempty = ps.IsEmpty(); bool issubsetof = ps.IsSubsetOf(ps2); bool isunrestricted = ps.IsUnrestricted(); string s = ps.ToString(); PermissionSet ps4 = ps.Union(ps2); SecurityElement se = new SecurityElement(""); ps.FromXml(se); se = ps.ToXml(); }
public static void PermissionSetDemo() { Console.WriteLine("Executing PermissionSetDemo"); try { //<Snippet2> // Open a new PermissionSet. PermissionSet ps1 = new PermissionSet(PermissionState.None); Console.WriteLine("Adding permission to open a file from a file dialog box."); //<Snippet3> // Add a permission to the permission set. ps1.AddPermission( new FileDialogPermission(FileDialogPermissionAccess.Open)); //</Snippet3> Console.WriteLine("Demanding permission to open a file."); ps1.Demand(); Console.WriteLine("Demand succeeded."); //</Snippet2> Console.WriteLine("Adding permission to save a file from a file dialog box."); ps1.AddPermission( new FileDialogPermission(FileDialogPermissionAccess.Save)); Console.WriteLine("Demanding permission to open and save a file."); ps1.Demand(); Console.WriteLine("Demand succeeded."); Console.WriteLine("Adding permission to read environment variable USERNAME."); ps1.AddPermission( new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME")); ps1.Demand(); Console.WriteLine("Demand succeeded."); Console.WriteLine("Adding permission to read environment variable COMPUTERNAME."); ps1.AddPermission( new EnvironmentPermission(EnvironmentPermissionAccess.Read, "COMPUTERNAME")); //<Snippet4> // Demand all the permissions in the set. Console.WriteLine("Demand all permissions."); ps1.Demand(); //</Snippet4> Console.WriteLine("Demand succeeded."); //<Snippet5> // Display the number of permissions in the set. Console.WriteLine("Number of permissions = " + ps1.Count); //</Snippet5> //<Snippet6> // Display the value of the IsSynchronized property. Console.WriteLine("IsSynchronized property = " + ps1.IsSynchronized); //</Snippet6> //<Snippet7> // Display the value of the IsReadOnly property. Console.WriteLine("IsReadOnly property = " + ps1.IsReadOnly); //</Snippet7> //<Snippet8> // Display the value of the SyncRoot property. Console.WriteLine("SyncRoot property = " + ps1.SyncRoot); //</Snippet8> //<Snippet9> // Display the result of a call to the ContainsNonCodeAccessPermissions method. // Gets a value indicating whether the PermissionSet contains permissions // that are not derived from CodeAccessPermission. // Returns true if the PermissionSet contains permissions that are not // derived from CodeAccessPermission; otherwise, false. Console.WriteLine("ContainsNonCodeAccessPermissions method returned " + ps1.ContainsNonCodeAccessPermissions()); //</Snippet9> //<Snippet10> Console.WriteLine("Value of the permission set ToString = \n" + ps1.ToString()); //</Snippet10> PermissionSet ps2 = new PermissionSet(PermissionState.None); //<Snippet11> // Create a second permission set and compare it to the first permission set. ps2.AddPermission( new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME")); ps2.AddPermission( new EnvironmentPermission(EnvironmentPermissionAccess.Write, "COMPUTERNAME")); IEnumerator list = ps1.GetEnumerator(); Console.WriteLine("Permissions in first permission set:"); while (list.MoveNext()) { Console.WriteLine(list.Current.ToString()); } Console.WriteLine("Second permission IsSubsetOf first permission = " + ps2.IsSubsetOf(ps1)); //</Snippet11> //<Snippet12> // Display the intersection of two permission sets. PermissionSet ps3 = ps2.Intersect(ps1); Console.WriteLine("The intersection of the first permission set and " + "the second permission set = " + ps3.ToString()); //</Snippet12> // Create a new permission set. PermissionSet ps4 = new PermissionSet(PermissionState.None); ps4.AddPermission( new FileIOPermission(FileIOPermissionAccess.Read, "C:\\Temp\\Testfile.txt")); ps4.AddPermission( new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write | FileIOPermissionAccess.Append, "C:\\Temp\\Testfile.txt")); //<Snippet13> // Display the union of two permission sets. PermissionSet ps5 = ps3.Union(ps4); Console.WriteLine("The union of permission set 3 and permission set 4 = " + ps5.ToString()); //</Snippet13> //<Snippet15> // Remove FileIOPermission from the permission set. ps5.RemovePermission(typeof(FileIOPermission)); Console.WriteLine("The last permission set after removing FileIOPermission = " + ps5.ToString()); //</Snippet15> //<Snippet16> // Change the permission set using SetPermission. ps5.SetPermission(new EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "USERNAME")); Console.WriteLine("Permission set after SetPermission = " + ps5.ToString()); //</Snippet16> //<Snippet17> // Display result of ToXml and FromXml operations. PermissionSet ps6 = new PermissionSet(PermissionState.None); ps6.FromXml(ps5.ToXml()); Console.WriteLine("Result of ToFromXml = " + ps6.ToString() + "\n"); //</Snippet17> //<Snippet18> // Display results of PermissionSet.GetEnumerator. IEnumerator psEnumerator = ps1.GetEnumerator(); while (psEnumerator.MoveNext()) { Console.WriteLine(psEnumerator.Current); } //</Snippet18> //<Snippet19> // Check for an unrestricted permission set. PermissionSet ps7 = new PermissionSet(PermissionState.Unrestricted); Console.WriteLine("Permission set is unrestricted = " + ps7.IsUnrestricted()); //</Snippet19> //<Snippet20> // Create and display a copy of a permission set. ps7 = ps5.Copy(); Console.WriteLine("Result of copy = " + ps7.ToString()); //</Snippet20> } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } }
/// <summary> /// Creates DataObject for Copy and Drag operations /// </summary> internal static DataObject _CreateDataObject(TextEditor This, bool isDragDrop) { DataObject dataObject; // Create the data object for drag and drop. // We could provide more extensibility here - // by allowing application to create its own DataObject. // Without it our extensibility looks inconsistent: // the interface IDataObject suggests that you can // create your own implementation of it, but you // really cannot, because there is no way of // using it in our TextEditor.Copy/Drag. (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert();//BlessedAssert try { dataObject = new DataObject(); } finally { UIPermission.RevertAssert(); } // Get plain text and copy it into the data object. string textString = This.Selection.Text; if (textString != String.Empty) { // Copy plain text into data object. // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception. if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Text)) { CriticalSetDataWrapper(dataObject, DataFormats.Text, textString); } // Copy unicode text into data object. // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception. if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.UnicodeText)) { CriticalSetDataWrapper(dataObject, DataFormats.UnicodeText, textString); } } // Get the rtf and xaml text and then copy it into the data object after confirm data format. // We do this only if our content is rich if (This.AcceptsRichContent) { // This ensures that in the confines of partial trust RTF is not enabled. // We use unmanaged code permission over clipboard permission since // the latter is available in intranet zone and this is something that will // fail in intranet too. if (SecurityHelper.CheckUnmanagedCodePermission()) { // In FullTrust we allow all rich formats on the clipboard Stream wpfContainerMemory = null; // null wpfContainerMemory on entry means that container is optional // and will be not created when there is no images in the range. // Create in-memory wpf package, and serialize the content of selection into it string xamlTextWithImages = WpfPayload.SaveRange(This.Selection, ref wpfContainerMemory, /*useFlowDocumentAsRoot:*/ false); if (xamlTextWithImages.Length > 0) { // ConfirmDataFormatSetting raises a public event - could throw recoverable exception. if (wpfContainerMemory != null && ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.XamlPackage)) { dataObject.SetData(DataFormats.XamlPackage, wpfContainerMemory); } // ConfirmDataFormatSetting raises a public event - could throw recoverable exception. if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Rtf)) { // Convert xaml to rtf text to set rtf data into data object. string rtfText = ConvertXamlToRtf(xamlTextWithImages, wpfContainerMemory); if (rtfText != String.Empty) { dataObject.SetData(DataFormats.Rtf, rtfText, true); } } } // Add a CF_BITMAP if we have only one image selected. Image image = This.Selection.GetUIElementSelected() as Image; if (image != null && image.Source is System.Windows.Media.Imaging.BitmapSource) { dataObject.SetImage((System.Windows.Media.Imaging.BitmapSource)image.Source); } } // Xaml format is availabe both in Full Trust and in Partial Trust // Need to re-serialize xaml to avoid image references within a container: StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); TextRangeSerialization.WriteXaml(xmlWriter, This.Selection, /*useFlowDocumentAsRoot:*/ false, /*wpfPayload:*/ null); string xamlText = stringWriter.ToString(); // Use WpfPayload.SaveRangeAsXaml method to produce correct image.Source properties. if (xamlText.Length > 0) { // ConfirmDataFormatSetting rasies a public event - could throw recoverable exception. if (ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Xaml)) { // Place Xaml data onto the dataobject using safe setter CriticalSetDataWrapper(dataObject, DataFormats.Xaml, xamlText); // The dataobject itself must hold an information about permission set // of the source appdomain. Set it there: // Package permission set for the current appdomain PermissionSet psCurrentAppDomain = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin(); string permissionSetCurrentAppDomain = psCurrentAppDomain.ToString(); CriticalSetDataWrapper(dataObject, DataFormats.ApplicationTrust, permissionSetCurrentAppDomain); } } } // Notify application about our data object preparation completion DataObjectCopyingEventArgs dataObjectCopyingEventArgs = new DataObjectCopyingEventArgs(dataObject, /*isDragDrop:*/ isDragDrop); This.UiScope.RaiseEvent(dataObjectCopyingEventArgs); if (dataObjectCopyingEventArgs.CommandCancelled) { dataObject = null; } return(dataObject); }
public static string ToPermissionString(this PermissionSet perms) { // TODO: clean string return(perms.ToString()); }
internal static DataObject _CreateDataObject(TextEditor This, bool isDragDrop) { new UIPermission(UIPermissionClipboard.AllClipboard).Assert(); DataObject dataObject; try { dataObject = new DataObject(); } finally { CodeAccessPermission.RevertAssert(); } string text = This.Selection.Text; if (text != string.Empty) { if (TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Text)) { TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.Text, text); } if (TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.UnicodeText)) { TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.UnicodeText, text); } } if (This.AcceptsRichContent) { if (SecurityHelper.CheckUnmanagedCodePermission()) { Stream stream = null; string text2 = WpfPayload.SaveRange(This.Selection, ref stream, false); if (text2.Length > 0) { if (stream != null && TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.XamlPackage)) { dataObject.SetData(DataFormats.XamlPackage, stream); } if (TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Rtf)) { string text3 = TextEditorCopyPaste.ConvertXamlToRtf(text2, stream); if (text3 != string.Empty) { dataObject.SetData(DataFormats.Rtf, text3, true); } } } Image image = This.Selection.GetUIElementSelected() as Image; if (image != null && image.Source is BitmapSource) { dataObject.SetImage((BitmapSource)image.Source); } } StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter); TextRangeSerialization.WriteXaml(xmlWriter, This.Selection, false, null); string text4 = stringWriter.ToString(); if (text4.Length > 0 && TextEditorCopyPaste.ConfirmDataFormatSetting(This.UiScope, dataObject, DataFormats.Xaml)) { TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.Xaml, text4); PermissionSet permissionSet = SecurityHelper.ExtractAppDomainPermissionSetMinusSiteOfOrigin(); string content = permissionSet.ToString(); TextEditorCopyPaste.CriticalSetDataWrapper(dataObject, DataFormats.ApplicationTrust, content); } } DataObjectCopyingEventArgs dataObjectCopyingEventArgs = new DataObjectCopyingEventArgs(dataObject, isDragDrop); This.UiScope.RaiseEvent(dataObjectCopyingEventArgs); if (dataObjectCopyingEventArgs.CommandCancelled) { dataObject = null; } return(dataObject); }