SecureFileValidator 1.0.3
.NET 8.0
This package targets .NET 8.0. The package is compatible with this framework or higher.
.NET Framework 4.7
This package targets .NET Framework 4.7. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SecureFileValidator --version 1.0.3
NuGet\Install-Package SecureFileValidator -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SecureFileValidator" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SecureFileValidator" Version="1.0.3" />
<PackageReference Include="SecureFileValidator" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SecureFileValidator --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SecureFileValidator, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SecureFileValidator@1.0.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SecureFileValidator&version=1.0.3
#tool nuget:?package=SecureFileValidator&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SecureFileValidator
跨 .NET Framework 4.7 與 .NET 8 的檔案安全驗證工具,支援常見格式(Word、Excel、MP4 等)的檔案簽名(magic number)比對,防止副檔名偽裝攻擊。
功能特色
- ✅ 支援 Office 格式 (DOCX, XLSX)
- ✅ 支援影音檔 (MP4, AVI, PNG, JPG 等)
- ✅ 檢查 ZIP 結構是否合法(如 Office OpenXML)
- ✅ 提供 ActionFilter 與 DataAnnotation 屬性,可於 ASP.NET MVC / Core 中驗證上傳檔案
- ✅ 可驗證單一檔案或整批上傳檔案
- ✅ 完整單元測試與多框架支援
安裝方式
套件參考(NuGet)
若使用 .NET 8
:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
若使用 .NET Framework 4.7
:
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.9" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
使用方式
1️⃣ 驗證副檔名與檔案內容是否符合
using (var stream = file.OpenReadStream())
{
bool valid = FileSignatureValidator.Validate(stream, file.FileName);
}
也可加入副檔名白名單:
FileSignatureValidator.Validate(stream, file.FileName, new[] { ".docx", ".xlsx" });
2️⃣ 套用 ActionFilterAttribute 進行 API 驗證
✅ 指定檔案欄位名稱:
[ValidateFileSignature("file")]
public IActionResult Upload(IFormFile file)
✅ 自動檢查所有上傳檔案(不指定參數名):
[ValidateFileSignature]
public IActionResult UploadAll()
3️⃣ 使用 DataAnnotation 屬性驗證模型檔案欄位
public class UploadModel
{
[ValidFileSignature(AllowedExtensions = new[] { ".docx", ".xlsx" }, ErrorMessage = "請上傳正確的檔案類型")]
public IFormFile File { get; set; }
}
單元測試
範例測試已涵蓋:
- ✅ 正常的副檔名與簽名比對
- ✅ 限制副檔名過濾
- ✅ 多檔案同時上傳時,自動驗證所有檔案
執行方式:
dotnet test
授權 License
此專案採用 MIT 授權,請自由使用並保留原始出處。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Framework | net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.7
- Microsoft.AspNet.Mvc (>= 5.2.7)
- System.ComponentModel.Annotations (>= 4.7.0)
- System.IO.Compression (>= 4.3.0)
- System.IO.Compression.ZipFile (>= 4.3.0)
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
-
net9.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.