前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何正确实现一个自定义Exception(二)

如何正确实现一个自定义Exception(二)

作者头像
MJ.Zhou
发布2023-09-13 08:28:19
1460
发布2023-09-13 08:28:19
举报
文章被收录于专栏:.NET开发那点事.NET开发那点事

上一篇《如何正确实现一个自定义 Exception》发布后获得不少 star。有同学表示很担忧,原来自己这么多年一直写错了。其实大家不用过分纠结,如果写的是 .NET CORE 1.0+ 的程序,那么大概率是没有问题的。

有大佬已经在评论区指出这些信息是过时的了。确实在.NET CORE 发布之后,Exception 已经不在推荐实现 ISerializable 接口。让我们细说一下。

BinaryFormatter security vulnerabilities

上一篇我们谈论了这么多,其实都是在说 ISerializable 的 patten。ISerializable 主要的作用就是给 BinaryFormatter 序列化器提供指示如何进行序列化/反序列化。也就是说这个接口基本上就是给 BinaryFormatter 设计的。BinaryFormatter 主要是给 .NET remoting 技术服务(一种古老的 RPC 技术,听过的都是老司机,不太确定 WCF 的 Binary 序列化是否使用该技术)。

但是很不幸,这个 BinaryFormatter 存在严重的安全风险。

BinaryFormatter 类型会带来风险,不建议将其用于数据处理。 即使应用程序认为自己正在处理的数据是可信的,也应尽快停止使用 BinaryFormatterBinaryFormatter 不安全,无法确保安全。

不光是 BinaryFormatter 有风险,以下这些序列化器同样存在风险,应避免使用:

  • SoapFormatter
  • LosFormatter
  • NetDataContractSerializer
  • ObjectStateFormatter

当前微软主要推荐使用:

  • System.Text.Json
  • XmlSerializer

https://learn.microsoft.com/zh-cn/dotnet/standard/serialization/binaryformatter-security-guide

其实不光是 .NET,其他语言在序列化反序列化上都很容易引入安全漏洞,比如 JAVA 的 jackson 就爆过序列化安全漏洞。

BinaryFormatter Obsolete

由于 remoting 技术在 .NET CORE 中已经废弃,并且有严重的安全风险,所以微软开始慢慢淘汰 BinaryFormatter 这个接口。

以下是 binaryformatter-obsoletion 的 roadmap:

  • .NET 5 (Nov 2020)
  • Allow disabling BinaryFormatter via an opt-in feature switch
  • ASP.NET projects disable BinaryFormatter by default but can re-enable
  • WASM projects disable BinaryFormatter with no ability to re-enable
  • All other project types (console, WinForms, etc.) enable BinaryFormatter by default
  • .NET produces guidance document on migrating away from BinaryFormatter
  • All outstanding BinaryFormatter-related issues resolved won't fix
  • Introduce a BinaryFormatter tracing event source
  • Serialize and Deserialize marked obsolete as warning
  • .NET 6 (Nov 2021)
  • No new Serializable types introduced
  • No new calls to BinaryFormatter from any first-party dotnet org code base
  • All first-party dotnet org code bases begin migration away from BinaryFormatter
  • .NET 7 (Nov 2022)
  • All first-party dotnet org code bases continue migration away from BinaryFormatter
  • References to BinaryFormatter APIs marked obsolete as warnings in .NET 5 now result
  • in build errors
  • A back-compat switch is made available to turn these back to warnings
  • .NET 8 (Nov 2023)
  • All first-party dotnet org code bases complete migration away from BinaryFormatter
  • BinaryFormatter disabled by default across all project types
  • All not-yet-obsolete BinaryFormatter APIs marked obsolete as warning
  • Additional legacy serialization infrastructure marked obsolete as warning
  • No new Serializable types introduced (all target frameworks)
  • .NET 9 (Nov 2024)
  • Remainder of legacy serialization infrastructure marked obsolete as warning
  • BinaryFormatter infrastructure removed from .NET
  • Back-compat switches also removed

从 .NET5 开始会出现警告,到.NET6 BUILD 的时候直接会是 ERROR,但是可以强制启用。到.NET9 会完全从 .NET 中移除。

那么既然 BinaryFormatter 在目前已经不在推荐使用,自然我们的自定义 Exception 也不用遵循 ISerializable patten 了。以下链接是微软给出的当前自定义 Exception 实现的建议,太长就不复制了。总之已经不在需求实现 protected 的序列化构造器,也不用 override GetObjectData 方法。

https://github.com/dotnet/docs/issues/34893

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • BinaryFormatter security vulnerabilities
  • BinaryFormatter Obsolete
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com