Skip to content

Commit 6d22086

Browse files
author
ChristianHaase
committed
feat: correctly validate pdf signature and split open xml and odf validation
Updated readme, license and copyright notice, as well as nuget package information.
1 parent ced9b5d commit 6d22086

File tree

11 files changed

+521
-147
lines changed

11 files changed

+521
-147
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2025 ByteGuard
3+
Copyright (c) 2025 ByteGuard Contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ It helps you enforce consistent file upload rules by checking:
66
- Allowed file extensions
77
- File size limits
88
- File signatures (magic numbers) to detect spoofed types
9-
- Internal ZIP structure for Office Open XML / OpenDocument formats (`.docx`, `.xlsx`, `.pptx`, `.odt`)
9+
- Specification conformance for Office Open XML / Open Document Formats (`.docx`, `.xlsx`, `.pptx`, `.odt`)
1010

1111
> ⚠️ **Important:** This library should be part of a **defense-in-depth** strategy.
1212
It does not replace antivirus scanning, sandboxing, or other security controls.
@@ -16,7 +16,7 @@ It does not replace antivirus scanning, sandboxing, or other security controls.
1616
- ✅ Validate files by **extension**
1717
- ✅ Validate files by **size**
1818
- ✅ Validate files by **signature (_magic-numbers_)**
19-
- ✅ Validate files by **internal ZIP structure** for archive-based formats (_Open XML and OpenDocument formats_)
19+
- ✅ Validate files by **specification conformance** for archive-based formats (_Open XML and Open Document Formats_)
2020
- ✅ Validate using file path, `Stream`, or `byte[]`
2121
- ✅ Configure which file types to support
2222
- ✅ Configure whether to **throw exceptions** or simply return a boolean
@@ -51,7 +51,7 @@ var isValid = fileValidator.IsValidFile("example.pdf", fileStream);
5151
### Using the fluent builder
5252

5353
```csharp
54-
var configuration = new FileValidatorConfigurationBuilder()
54+
var configuration = new FileValidatorConfigurationBuilder()
5555
.AllowFileTypes(FileExtensions.Pdf, FileExtensions.Jpg, FileExtensions.Png)
5656
.SetFileSizeLimit(ByteSize.MegaBytes(25))
5757
.SetThrowExceptionOnInvalidFile(false)
@@ -71,13 +71,14 @@ The `FileValidator` class provides methods to validate specific aspects of a fil
7171
> 1. Extension validation
7272
> 2. File size validation
7373
> 3. Signature (magic-number) validation
74-
> 4. Optional Open XML / OpenDocument structure validation (for supported types)
74+
> 4. Optional Open XML / Open Document Format specification conformance validation (for supported types)
7575
7676
```csharp
7777
bool isExtensionValid = fileValidator.IsValidFileType(fileName);
7878
bool isFileSizeValid = fileValidator.HasValidSize(fileStream);
7979
bool isSignatureValid = fileValidator.HasValidSignature(fileName, fileStream);
8080
bool isOpenXmlValid = fileValidator.IsValidOpenXmlDocument(fileName, fileStream);
81+
bool isOpenDocumentFormatValid = fileValidator.IsValidOpenDocumentFormat(fileName, fileStream);
8182
```
8283

8384
### Example
@@ -137,11 +138,11 @@ The following file extensions are supported by the `FileValidator`:
137138

138139
For some formats, additional checks are performed:
139140

140-
- **Office Open XML / OpenDocument** (`.docx`, `.xlsx`, `.pptx`, `.odt`):
141+
- **Office Open XML / Open Document Format** (`.docx`, `.xlsx`, `.pptx`, `.odt`):
141142
- Extension
142143
- File size
143144
- Signature
144-
- Internal ZIP structure (basic format sanity)
145+
- Specification conformance
145146

146147
- **Other binary formats** (e.g. images, audio, video such as `.jpg`, `.png`, `.mp3`, `.mp4`):
147148
- Extension
@@ -164,10 +165,12 @@ When `ThrowExceptionOnInvalidFile` is set to `true`, validation functions will t
164165

165166
| Exception type | Scenario |
166167
|--|--|
168+
| `EmptyFileException` | Thrown when the file content is `null` or empty, indicating a file without any content. |
167169
| `UnsupportedFileException` | Thrown when the file extension is not in the list of supported types. |
168170
| `InvalidFileSizeException` | Thrown when the file size exceeds the configured file size limit. |
169171
| `InvalidSignatureException` | Thrown when the file's signature does not match the expected signature for its type. |
170172
| `InvalidOpenXmlFormatException` | Thrown when the internal structure of an Open XML file is invalid (`.docx`, `.xlsx`, `.pptx`, etc.). |
173+
| `InvalidOpenDocumentFormatException` | Thrown when the specification conformance of an Open Document Format file is invalid (`.odt`, etc.). |
171174

172175
## When to use this package
173176

@@ -176,4 +179,4 @@ When `ThrowExceptionOnInvalidFile` is set to `true`, validation functions will t
176179
- ✅ When you want **defense-in-depth** against spoofed or malicious files
177180

178181
## License
179-
_ByteGuard FileValidator is copyright © ByteGuard Contributors - Provided under the MIT license._
182+
_ByteGuard FileValidator is Copyright © ByteGuard Contributors - Provided under the MIT license._

src/ByteGuard.FileValidator/ByteGuard.FileValidator.csproj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net48;net8.0;net9.0;net10.0</TargetFrameworks>
55
<VersionPrefix>0.1.0</VersionPrefix>
6-
<Authors>ByteGuard, detilium</Authors>
7-
<Owners>detilium</Owners>
8-
<Description></Description>
6+
<Authors>ByteGuard Contributors, detilium</Authors>
7+
<Description>ByteGuard File Validator is a security-focused .NET library for validating user-supplied files, providing a configurable API to help you enforce safe and consistent file handling across your applications.</Description>
98
<PackageProjectUrl>https://github.com/ByteGuard-HQ/byteguard-file-validator-net</PackageProjectUrl>
109
<RepositoryUrl>https://github.com/ByteGuard-HQ/byteguard-file-validator-net</RepositoryUrl>
11-
<PackageTags>byteguard, file-validator, security</PackageTags>
10+
<RepositoryType>git</RepositoryType>
11+
<PackageTags>byteguard file-validator file-validation file-upload upload-validation security application-security appsec</PackageTags>
1212
<PackageReadmeFile>README.md</PackageReadmeFile>
13+
<Copyright>Copyright © ByteGuard Contributors</Copyright>
14+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1315
</PropertyGroup>
1416

1517
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
1618
<Reference Include="System.IO.Compression" />
1719
</ItemGroup>
1820

1921
<ItemGroup>
20-
<None Include="..\..\..\README.md" Pack="true" PackagePath="\" />
22+
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
2123
</ItemGroup>
2224

2325
<ItemGroup>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace ByteGuard.FileValidator.Exceptions
4+
{
5+
/// <summary>
6+
/// Exception type used specifically when a given file, which is expected to be an Open Document Format (ODF) file,
7+
/// does not adhere to the expected internal ODF structure.
8+
/// /// </summary>
9+
public class InvalidOpenDocumentFormatException : Exception
10+
{
11+
public InvalidOpenDocumentFormatException()
12+
: base("Invalid Open Document Format file.")
13+
{
14+
}
15+
16+
public InvalidOpenDocumentFormatException(string message) : base(message)
17+
{
18+
}
19+
20+
public InvalidOpenDocumentFormatException(string message, Exception innerException) : base(message, innerException)
21+
{
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)