You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Obsolete SerializationFormat.Binary in DataTable/DataSet and put it behind an appcontext switch #39289
From offline discussions with Arthur and friends, we may want to eliminate support for SerializationFormat.Binary anyway, since it's not compatible between .NET Framework and .NET Core / .NET 5.0+. The only data format compatible between the different runtimes is SerializationFormat.Xml.
What should the behaviour be if a user attempts to use binary serialization once the code to allow it has been removed? silent no-op or throwing of an exception? If an exception there is already an invalid enum value exception (I think ArgumentException) present which could be thrown or should there be a new one indicating that it has been removed? Should SerializationFormat.Binary be marked as obsolete? Should any exceptions in System.Data.Common have their serialization/deserialization methods marked as obsolete while this work is being done?
From offline conversations the most straightforward way to do this would be to mark the Binary enum field as obsolete and to have the SerializationFormat property setter throw ArgumentException if any value other than Xml is provided. Let that bake for a release, then if all is well rip out the now-unused logic.
@jeffhandley I don't think so. There are two issues at play here.
The first issue (as tracked here) is the removal of BinaryFormatterwithinDataSet itself. This would still allow DataSet instances themselves to be serialized with BinaryFormatter, it's just that they'd be forced to use the same XML-based payload format that Full Framework uses. This is different from "consumers of .NET should stop using BinaryFormatter" and probably deserves its own obsoletion code.
The second issue (tracked by dotnet/designs#141) is the eventual removal of [Serializable] attributes, serialization ctors, and ISerializable interfaces from all built-in .NET types, including DataSet. At that point, it won't matter whether you specify SerializationFormat.Binary or SerializationFormat.Xml. Neither will work since the code paths which consumed them will no longer exist.
Note: I should clarify a possible point of confusion. The SerializationFormat enum is applicable only when using BinaryFormatter to serialize or deserialize an instance of DataSet or DataTable. The enum value controls whether BinaryFormatter itself should use a binary or an XML format for the persisted payload. You can continue to use XmlSerializer to serialize / deserialize instances of DataSet and DataTable (though this is discouraged for untrusted input) because XmlSerializer never looks at the value of this enum.
The SerializationFormat enum is applicable only when using BinaryFormatter to serialize or deserialize an instance of DataSet or DataTable. The enum value controls whether BinaryFormatter itself should use a binary or an XML format for the persisted payload.
Can someone clarify this? This statement seems to suggest that BinaryFormatter can be used regardless of what SerializationFormat's value is? I'm looking at SerializeDataSet and see this code
if(remotingFormat!=SerializationFormat.Xml){use BF to serialize. Nothing here suggests XML unless I'm missing something?}else{// old behaviourstringstrSchema=GetXmlSchemaForRemoting(null);string?strData=null;info.AddValue(KEY_XMLSCHEMA,strSchema);StringBuilderstrBuilder=newStringBuilder(EstimatedXmlStringSize()*2);StringWriterstrWriter=newStringWriter(strBuilder,CultureInfo.InvariantCulture);XmlTextWriterw=newXmlTextWriter(strWriter);WriteXml(w,XmlWriteMode.DiffGram);strData=strWriter.ToString();info.AddValue(KEY_XMLDIFFGRAM,strData);}
It looks like SerializationFormat.Binary uses BinaryFormatter while SerializationFormat.XML uses XMLWriter?
DataTable and SerializationFormat are something of strange beasts. The best way to think about DataTable is as a container around other data. The SerializationFormat enum controls how the data within the container is serialized, and this could be using a format different than the container (DataTable) itself.
The default value is DataTable.RemotingFormat = RemotingFormat.Xml. This default value means that if you're using code like the following:
The container (DataTable) is written using a BinaryFormatter-serialized format, because the caller instantiated a BinaryFormatter and passed a DataTable to it. But under the covers, DataTable is really XML-serializing its internal values and writing a bunch of XML literal strings to the underlying formatter. If you look at the raw payload bytes, it basically looks like the payload says "Hi, I'm a BinaryFormatter-serialized DataTable, and I have a bunch of string members!" It's akin to BinaryFormatter-serializing the following pseudo-code:
If you instead set DataTable.RemotingFormat = RemotingFormat.Binary, then it recursively calls back into BinaryFormatter for each value it's serializing. So both the container (the DataTable itself) and all of its internal values are using BinaryFormatter. It's akin to BinaryFormatter-serializing the following pseudo-code:
[Serializable]publicclassMyDataTable{publicstringColumnName1;publicTypeColumnType1;publicbyte[]ColumnValue1SerializedAsBinaryFormatter;// note: raw bytes instead of stringpublicstringColumnName2;publicTypeColumnType2;publicbyte[]ColumnValue2SerializedAsBinaryFormatter;// note: raw bytes instead of string/* ... */}
When serializing, DataTable also inserts a marker specifying which RemotingFormat was in use, that way it can call back into the correct code paths during deserialization. If the incoming payload contains the RemotingFormat.Xml marker, it unwraps its inner contents using XML serialization. If the incoming payload contains the RemotingFormat.Binary marker, it unwraps its inner contents using BinaryFormatter serialization.
The suggestion here is to dishonor the RemotingFormat.Binary switch for both serialization and deserialization unless the app developer has explicitly consented to its usage.
changed the title [-]Remove BinaryFormatter usage from DataSet[/-][+]Obsolete SerializationFormat.Binary in DataTable/DataSet and put it behind an appcontext switch[/+]on Feb 10, 2022
Submitted #65139 to obsolete SerializationFormat.Binary and put it behind an appcontext switch, for .NET 7.0. #65140 tracks removing the logic entirely in .NET 8.0.
Activity
Wraith2 commentedon Jul 15, 2020
What should the behaviour be if a user attempts to use binary serialization once the code to allow it has been removed? silent no-op or throwing of an exception? If an exception there is already an invalid enum value exception (I think ArgumentException) present which could be thrown or should there be a new one indicating that it has been removed? Should SerializationFormat.Binary be marked as obsolete? Should any exceptions in System.Data.Common have their serialization/deserialization methods marked as obsolete while this work is being done?
GrabYourPitchforks commentedon Jul 15, 2020
From offline conversations the most straightforward way to do this would be to mark the
Binaryenum field as obsolete and to have theSerializationFormatproperty setter throwArgumentExceptionif any value other thanXmlis provided. Let that bake for a release, then if all is well rip out the now-unused logic.jeffhandley commentedon Jul 18, 2020
@GrabYourPitchforks would we want to reuse the same diagnostic id as binary formatter’s obsoletion (
SYSLIB0011)?GrabYourPitchforks commentedon Jul 18, 2020
@jeffhandley I don't think so. There are two issues at play here.
The first issue (as tracked here) is the removal of
BinaryFormatterwithinDataSetitself. This would still allowDataSetinstances themselves to be serialized withBinaryFormatter, it's just that they'd be forced to use the same XML-based payload format that Full Framework uses. This is different from "consumers of .NET should stop usingBinaryFormatter" and probably deserves its own obsoletion code.The second issue (tracked by dotnet/designs#141) is the eventual removal of
[Serializable]attributes, serialization ctors, andISerializableinterfaces from all built-in .NET types, includingDataSet. At that point, it won't matter whether you specifySerializationFormat.BinaryorSerializationFormat.Xml. Neither will work since the code paths which consumed them will no longer exist.Note: I should clarify a possible point of confusion. The
SerializationFormatenum is applicable only when usingBinaryFormatterto serialize or deserialize an instance ofDataSetorDataTable. The enum value controls whetherBinaryFormatteritself should use a binary or an XML format for the persisted payload. You can continue to useXmlSerializerto serialize / deserialize instances ofDataSetandDataTable(though this is discouraged for untrusted input) becauseXmlSerializernever looks at the value of this enum.pgovind commentedon Feb 11, 2021
Can someone clarify this? This statement seems to suggest that
BinaryFormattercan be used regardless of whatSerializationFormat's value is? I'm looking atSerializeDataSetand see this codeIt looks like
SerializationFormat.Binaryuses BinaryFormatter whileSerializationFormat.XMLuses XMLWriter?cc @GrabYourPitchforks @ajcvickers
GrabYourPitchforks commentedon Feb 11, 2021
DataTableandSerializationFormatare something of strange beasts. The best way to think aboutDataTableis as a container around other data. TheSerializationFormatenum controls how the data within the container is serialized, and this could be using a format different than the container (DataTable) itself.The default value is
DataTable.RemotingFormat = RemotingFormat.Xml. This default value means that if you're using code like the following:The container (DataTable) is written using a BinaryFormatter-serialized format, because the caller instantiated a BinaryFormatter and passed a DataTable to it. But under the covers, DataTable is really XML-serializing its internal values and writing a bunch of XML literal strings to the underlying formatter. If you look at the raw payload bytes, it basically looks like the payload says "Hi, I'm a BinaryFormatter-serialized DataTable, and I have a bunch of string members!" It's akin to BinaryFormatter-serializing the following pseudo-code:
If you instead set
DataTable.RemotingFormat = RemotingFormat.Binary, then it recursively calls back into BinaryFormatter for each value it's serializing. So both the container (the DataTable itself) and all of its internal values are using BinaryFormatter. It's akin to BinaryFormatter-serializing the following pseudo-code:When serializing, DataTable also inserts a marker specifying which RemotingFormat was in use, that way it can call back into the correct code paths during deserialization. If the incoming payload contains the
RemotingFormat.Xmlmarker, it unwraps its inner contents using XML serialization. If the incoming payload contains theRemotingFormat.Binarymarker, it unwraps its inner contents using BinaryFormatter serialization.The suggestion here is to dishonor the
RemotingFormat.Binaryswitch for both serialization and deserialization unless the app developer has explicitly consented to its usage.[-]Remove BinaryFormatter usage from DataSet[/-][+]Obsolete SerializationFormat.Binary in DataTable/DataSet and put it behind an appcontext switch[/+]roji commentedon Feb 10, 2022
Submitted #65139 to obsolete SerializationFormat.Binary and put it behind an appcontext switch, for .NET 7.0. #65140 tracks removing the logic entirely in .NET 8.0.
Additional tasks:
@GrabYourPitchforks let me know if this all looks OK and if there's anything I've missed.
Obsolete and put SerializationFormat.Binary behind an appcontext swit…
gewarren commentedon Mar 17, 2022
@roji Obsoleting
SerializationFormat.Binaryis a breaking change, correct? If so, can you log an issue at https://github.com/dotnet/docs/issues/new?assignees=gewarren&labels=breaking-change%2CPri1%2Cdoc-idea&template=breaking-change.yml&title=%5BBreaking+change%5D%3A+? I can add the docs for the new SYSLIB0038 diagnostic.roji commentedon Mar 18, 2022
@gewarren thanks - opened dotnet/docs#28726.
Can you point me to where I'd add some docs on the DataSet and DataTable security guidance page?
gewarren commentedon Mar 18, 2022
You can edit that file here: https://github.com/dotnet/docs/blob/main/docs/framework/data/adonet/dataset-datatable-dataview/security-guidance.md. There's a section titled "Deserialize a DataSet or DataTable via BinaryFormatter".
roji commentedon Aug 8, 2022
Tracking the remaining docs work on my list, will do that as coding work winds down on 7.0.