8000 Remove support for `UseLegacyDangerousClipboardDeserializationMode` a… · dotnet/wpf@8698172 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8698172

Browse files
Remove support for UseLegacyDangerousClipboardDeserializationMode and permanently disallow deserialization of dangerous types.
- Removes support for AppContext flag `UseLegacyDangerousClipboardDeserializationMode` - Permanently limits clipboard-deserialziation to primitive non-text types only. Fixes #1132
1 parent 580a428 commit 8698172

File tree

7 files changed

+25
-47
lines changed

7 files changed

+25
-47
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/CoreAppContextSwitches.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -337,25 +337,6 @@ public static bool AllowExternalProcessToBlockAccessToTemporaryFiles
337337

338338
#endregion
339339

340-
#region EnableLegacyDangerousClipboardDeserializationMode
341-
342-
internal const string EnableLegacyDangerousClipboardDeserializationModeSwitchName = "Switch.System.Windows.EnableLegacyDangerousClipboardDeserializationMode";
343-
private static int _enableLegacyDangerousClipboardDeserializationMode;
344-
public static bool EnableLegacyDangerousClipboardDeserializationMode
345-
{
346-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
347-
get
348-
{
349-
/// <summary>
350-
/// Malicious managed objects could be placed in the clipboard lying about its format,
351-
/// to fix this OleConverter now restricts object deserialization in some cases.
352-
/// When this switch is enabled behavior falls back to deserializing without restriction.
353-
/// </summary>
354-
return LocalAppContext.GetCachedSwitchValue(EnableLegacyDangerousClipboardDeserializationModeSwitchName, ref _enableLegacyDangerousClipboardDeserializationMode);
355-
}
356-
}
357-
358-
#endregion
359340
}
360341
#pragma warning restore 436
361342
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/AppContextDefaultValues.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ private static void InitializeNetFxSwitchDefaultsForNetCoreRuntime()
6060
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.ShouldNotRenderInNonInteractiveWindowStationSwitchName, false);
6161
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.DoNotUsePresentationDpiCapabilityTier3OrGreaterSwitchName, false);
6262
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.AllowExternalProcessToBlockAccessToTemporaryFilesSwitchName, false);
63-
LocalAppContext.DefineSwitchDefault(CoreAppContextSwitches.EnableLegacyDangerousClipboardDeserializationModeSwitchName, false);
6463
}
6564
}
6665
#pragma warning restore 436

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/clipboard.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -490,17 +490,6 @@ public static void SetDataObject(object data, bool copy)
490490
//
491491
//------------------------------------------------------
492492

493-
/// <summary>
494-
/// Determines whether the legacy dangerous clipboard deserialization mode should be used based on the AppContext switch and Device Guard policies.
495-
/// </summary>
496-
/// <returns>
497-
/// If Device Guard is enabled this method returns false, otherwise it returns the AppContext switch value.
498-
/// </returns>
499-
internal static bool UseLegacyDangerousClipboardDeserializationMode()
500-
{
501-
return !IsDeviceGuardEnabled && CoreAppContextSwitches.EnableLegacyDangerousClipboardDeserializationMode;
502-
}
503-
504493
/// <summary>
505494
/// Places data on the system Clipboard and uses copy to specify whether the data
506495
/// should remain on the Clipboard after the application exits.

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/dataobject.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,13 +2863,29 @@ private object GetDataFromHGLOBAL(string format, IntPtr hglobal)
28632863
{
28642864
data = ReadBitmapSourceFromHandle(hglobal);
28652865
}
2866-
// Restrict deserialization to only primitives
2867-
// and strings here to prevent potentially malicious objects from
2866+
// Limit deserialization to only primitive types, which consist of the following:
2867+
//
2868+
// DataFormats.CommaSeparatedValue
2869+
// DataFormats.FileDrop
2870+
// DataFormats.Html
2871+
// DataFormats.OemText
2872+
// DataFormats.PenData
2873+
// DataFormats.Rtf
2874+
// DataFormats.Serializable
2875+
// DataFormats.Text
2876+
// DataFormats.UnicodeText
2877+
// DataFormats.WaveAudio
2878+
// DataFormats.Xaml
2879+
// DataFormats.XamlPackage
2880+
// DataFormats.StringFormat
2881+
//
2882+
// Out of these primitive types, we will disallow deserialization of
2883+
// DataFormats.StringFormat to prevent potentially malicious objects from
28682884
// being deserialized as part of a "text" copy-paste or drag-drop.
2885+
//
28692886
// The rest of the following formats are pre-defined in the OS,
2870-
// they are not managed objects so we shouldn't try to deserialize them as such,
2871-
// allow primitives in a best effort for compat, but restrict other types.
2872-
else if (!Clipboard.UseLegacyDangerousClipboardDeserializationMode())
2887+
// they are not managed objects - an so we will not attempt to deserialize them.
2888+
else
28732889
{
28742890
bool restrictDeserialization =
28752891
(IsFormatEqual(format, DataFormats.StringFormat) ||
@@ -2888,11 +2904,7 @@ private object GetDataFromHGLOBAL(string format, IntPtr hglobal)
28882904

28892905
data = ReadObjectFromHandle(hglobal, restrictDeserialization);
28902906
}
2891-
else
2892-
{
2893-
data = ReadObjectFromHandle(hglobal, restrictDeserialization: false);
2894-
}
2895-
}
2907+
}
28962908

28972909
return data;
28982910
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Ink/XamlClipboardData.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ protected override void DoPaste(IDataObject dataObject)
100100

101101
if< F438 /span> ( !String.IsNullOrEmpty(xml) )
102102
{
103-
bool useRestrictiveXamlReader = !Clipboard.UseLegacyDangerousClipboardDeserializationMode();
104-
UIElement element = XamlReader.Load(new System.Xml.XmlTextReader(new System.IO.StringReader(xml)), useRestrictiveXamlReader) as UIElement;
103+
UIElement element = XamlReader.Load(new System.Xml.XmlTextReader(new System.IO.StringReader(xml)), useRestrictiveXamlReader: true) as UIElement;
105104
if (element != null)
106105
{
107106
ElementList.Add(element);

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextEditorCopyPaste.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,7 @@ private static bool PasteXaml(TextEditor This, string pasteXaml)
923923
try
924924
{
925925
// Parse the fragment into a separate subtree
926-
bool useRestrictiveXamlReader = !Clipboard.UseLegacyDangerousClipboardDeserializationMode();
927-
object xamlObject = XamlReader.Load(new XmlTextReader(new System.IO.StringReader(pasteXaml)), useRestrictiveXamlReader);
926+
object xamlObject = XamlReader.Load(new XmlTextReader(new System.IO.StringReader(pasteXaml)), useRestrictiveXamlReader: true);
928927
TextElement flowContent = xamlObject as TextElement;
929928

930929
success = flowContent == null ? false : PasteTextElement(This, flowContent);

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ internal static object LoadElement(Stream stream)
343343
parserContext.BaseUri = entryPartUri;
344344

345345
// Call xaml parser
346-
bool useRestrictiveXamlReader = !Clipboard.UseLegacyDangerousClipboardDeserializationMode();
347-
xamlObject = XamlReader.Load(xamlEntryPart.GetStream(), parserContext, useRestrictiveXamlReader);
346+
xamlObject = XamlReader.Load(xamlEntryPart.GetStream(), parserContext, useRestrictiveXamlReader: true);
348347

349348
// Remove the temporary uri from the PackageStore
350349
PackageStore.RemovePackage(packageUri);

0 commit comments

Comments
 (0)
0