Skip to content

Disable BinaryFormatter across most .NET 8 project types #31591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Copyright (c) .NET Foundation. All rights reserved.
When https://github.com/Microsoft/visualfsharp/issues/3207 is fixed,
remove the block below and move it into the shared .targets file.
-->
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '7.0'))">
<WarningsAsErrors Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
<PropertyGroup>
<WarningsAsErrors Condition="'$(_BinaryFormatterObsoleteAsError)' == 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ Copyright (c) .NET Foundation. All rights reserved.
When https://github.com/Microsoft/visualfsharp/issues/3207 is fixed,
remove the block below and move it into the shared .targets file.
-->
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '7.0'))">
<WarningsAsErrors Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
<PropertyGroup>
<WarningsAsErrors Condition="'$(_BinaryFormatterObsoleteAsError)' == 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,44 @@ Copyright (c) .NET Foundation. All rights reserved.
<IncludeProjectsNotInAssetsFileInDepsFile Condition="'$(IncludeProjectsNotInAssetsFileInDepsFile)' == ''">true</IncludeProjectsNotInAssetsFileInDepsFile>
</PropertyGroup>

<!--
BinaryFormatter Disabling
===========================

The EnableUnsafeBinaryFormatterSerialization setting controls both runtime and compile-time behavior.
The behavior is slightly different depending on the project type and target runtime.

If the property is explicitly set to TRUE:
- The APIs are obsolete as warning, and calls to the APIs will succeed at runtime.

If the property is explicitly set to FALSE:
- On .NET 5 & 6, the APIs are obsolete as warning, and calls to the APIs will fail at runtime.
- On .NET 7+, the APIs are obsolete as error, and calls to the APIs will fail at runtime.

If the property is not explicitly TRUE or FALSE:
- On .NET 5 & 6, the APIs are obsolete as warning, and calls to the APIs will succeed at runtime.
- On .NET 7, the APIs are obsolete as error, but calls to the APIs will succeed at runtime.
- On .NET 8+, the APIs are obsolete as error, and calls to the APIs will fail at runtime
unless the SDK has opted in to keeping legacy BinaryFormatter behavior around.

n.b. The APIs are already marked obsolete (as warning) in .NET 5, so we don't need to special-case
them unless we want to upgrade them to warn-as-error.
-->

<PropertyGroup Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != 'true' AND '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '7.0'))">
<!--
Certain project types need to re-enable BinaryFormatter by default; it will still be warn-as-error but will work at runtime.
WinForms: enabled through 8.0 (but not after)
WPF: enabled through 8.0 (but not after)
-->
<_ProjectTypeRequiresBinaryFormatter Condition="'$(UseWindowsForms)' == 'true' AND $([MSBuild]::VersionLessThanOrEquals($(TargetFrameworkVersion), '8.0'))">true</_ProjectTypeRequiresBinaryFormatter>
<_ProjectTypeRequiresBinaryFormatter Condition="'$(UseWPF)' == 'true' AND $([MSBuild]::VersionLessThanOrEquals($(TargetFrameworkVersion), '8.0'))">true</_ProjectTypeRequiresBinaryFormatter>
<!-- controls warn as error -->
<_BinaryFormatterObsoleteAsError>true</_BinaryFormatterObsoleteAsError>
<!-- controls runtime behavior (AppContext & trimming) -->
<EnableUnsafeBinaryFormatterSerialization Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '8.0')) AND '$(_ProjectTypeRequiresBinaryFormatter)' != 'true'">false</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>

<PropertyGroup>
<CoreBuildDependsOn>
_CheckForBuildWithNoBuild;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ Copyright (c) .NET Foundation. All rights reserved.

<PropertyGroup>
<!--
ASP.NET 5.0 explicitly disables this functionality by default, requiring the app author
ASP.NET 5.0+ explicitly disables this functionality by default, requiring the app author
to opt-in to re-enabling the feature. The way to opt back in is to specify the below
element with the value "true". It is anticipated that future releases of .NET will
disable this functionality by default across all project types, at which point this logic
can be removed from the ASP.NET-specific SDK.
element with the value "true". This functionality is disabled across most project types
by default in .NET 8; however, we need to keep the line below since it's not conditioned
on a version check like the normal SDK targets logic is.
-->
<EnableUnsafeBinaryFormatterSerialization Condition="'$(EnableUnsafeBinaryFormatterSerialization)' == ''">false</EnableUnsafeBinaryFormatterSerialization>
</PropertyGroup>
Expand Down