如何正確實現一個自定義Exception(二)

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

感謝

  • 飯勺oO
  • czd890

等大佬提供相關鏈接。

關注我的公衆號一起玩轉技術

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章