Skip to content

Commit 02834a7

Browse files
committed
docs: update README and bump package version
- Update README to document FileByteType validation modes (Strict vs Lazy) and new optional parameters - Add usage examples for selecting validationType in IFormFileTypeProvider and Validator - Document mode-specific magic byte configuration on FileByteFilter (optional FileByteType parameter) - Bump MagicBytesValidator package version in csproj to reflect the new API/documentation
1 parent 6a702fb commit 02834a7

2 files changed

Lines changed: 141 additions & 7 deletions

File tree

MagicBytesValidator/MagicBytesValidator.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PackageId>MagicBytesValidator</PackageId>
55
<Title>traperto GmbH</Title>
66
<Description>Validate files based on mime types, file extensions and magic byte sequences.</Description>
7-
<Version>2.1.6</Version>
7+
<Version>2.2.0</Version>
88
<Authors>Members of traperto GmbH</Authors>
99
<PackageProjectUrl>https://github.com/Traperto/magic-bytes-validator/blob/main/README.md</PackageProjectUrl>
1010
<PackageTags>mime mimetype mimetypes magic magicbyte magicbytes extension extensions file
@@ -17,7 +17,7 @@
1717
<Company>traperto GmbH</Company>
1818

1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
20-
20+
2121
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2222
</PropertyGroup>
2323

README.md

Lines changed: 139 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ The existing `IFileType`s can be expanded in various ways.
77
- Install nuget package into your project:
88

99
```powershell
10-
Install-Package MagicBytesValidator -Version 2.1.6
10+
Install-Package MagicBytesValidator -Version 2.2.0
1111
```
1212

1313
```bash
14-
dotnet add package MagicBytesValidator --version 2.1.6
14+
dotnet add package MagicBytesValidator --version 2.2.0
1515
```
1616

1717
- Reference in your csproj:
1818
```xml
19-
<PackageReference Include="MagicBytesValidator" Version="2.1.6" />
19+
<PackageReference Include="MagicBytesValidator" Version="2.2.0" />
2020
```
2121

2222
### How to use it?
@@ -47,6 +47,140 @@ var fileType = await streamFileTypeProvider.TryFindUnambiguousAsync(fileStream,
4747
```c#
4848
var isValid = await validator.IsValidAsync(memoryStream, fileType, CancellationToken.None);
4949
```
50+
## Validation strictness (FileByteType)
51+
52+
Some formats support multiple validation strategies (e.g. strict vs. lazy rules).
53+
For this purpose, the library exposes `FileByteType`:
54+
55+
- `FileByteType.Strict` (default)
56+
- `FileByteType.Lazy` (optional relaxed/"lazy" rules for certain formats)
57+
58+
### Validate an uploaded IFormFile with a specific validation type
59+
60+
The form file provider accepts an optional `validationType` parameter:
61+
62+
```c#
63+
var fileType = await formFileTypeProvider.FindValidatedTypeAsync(
64+
formFile,
65+
null,
66+
CancellationToken.None,
67+
validationType: MagicBytesValidator.Models.FileByteType.Lazy
68+
);
69+
```
70+
71+
Example using `Lazy` (lazy rules if the format supports it):
72+
73+
```c#
74+
var fileType = await formFileTypeProvider.FindValidatedTypeAsync(
75+
formFile,
76+
null,
77+
CancellationToken.None,
78+
validationType: MagicBytesValidator.Models.FileByteType.Lazy
79+
);
80+
```
81+
82+
### Validate a stream with a specific validation type
83+
84+
The validator also accepts an optional `validationType` parameter:
85+
86+
```c#
87+
var isValid = await validator.IsValidAsync(
88+
memoryStream,
89+
fileType,
90+
CancellationToken.None,
91+
validationType: MagicBytesValidator.Models.FileByteType.Strict
92+
);
93+
```
94+
95+
Example using `Lazy`:
96+
97+
```c#
98+
var isValid = await validator.IsValidAsync(
99+
memoryStream,
100+
fileType,
101+
CancellationToken.None,
102+
validationType: MagicBytesValidator.Models.FileByteType.Lazy
103+
);
104+
```
105+
106+
> Note: If a format does not define any `Lazy`-specific checks, `Lazy` behaves like “global checks only”.
107+
> This keeps existing formats unchanged unless they opt into mode-specific rules.
108+
109+
### Example: Lazy ("relaxed") PDF validation
110+
111+
Some PDFs contain additional trailing bytes after the `%%EOF` marker. While strict validation may require the file
112+
to end with `%%EOF`, lazy validation can accept the `%%EOF` marker anywhere within the last 1024 bytes of the file
113+
(behaviour tolerated by common PDF viewers).
114+
115+
## Expand the file type mapping
116+
117+
- Get mapping:
118+
119+
```csharp
120+
// use the validator:
121+
var mapping = validator.Mapping;
122+
123+
// use the formFileTypeProvider:
124+
var mapping = formFileTypeProvider.Mapping;
125+
126+
// or create a new instance of the mapping:
127+
var mapping = new MagicBytesValidator.Services.Mapping();
128+
```
129+
130+
- Register a single `FileByteFilter`:
131+
132+
```csharp
133+
mapping.Register(
134+
new FileByteFilter(
135+
"traperto/trp", // MIME type
136+
new[] { "trp" } // file extensions
137+
) {
138+
// magic byte sequences
139+
StartsWith(new byte?[]
140+
{
141+
0x78, 0x6c, 0x2f, 0x5f, 0x72, 0x65
142+
})
143+
.EndsWith(new byte?[]
144+
{
145+
0xFF, 0xFF
146+
})
147+
}
148+
);
149+
```
150+
151+
- `FileByteFilter`s with specific offset checks:
152+
153+
```csharp
154+
mapping.Register(
155+
new FileByteFilter(
156+
"traperto/trp", // MIME type
157+
new[] { "trp" } // file extensions
158+
) {
159+
// magic byte sequences
160+
Specific(new ByteCheck(512, new byte?[] { 0xFD }));
161+
}
162+
);
163+
```
164+
165+
`ByteCheck` allows for negative offset values to look for a specific offset counting from the end of file.
166+
167+
### Optional: register mode-specific magic byte checks (Strict/Lazy)
168+
169+
When configuring a `FileByteFilter`, fluent methods accept an optional `FileByteType` parameter.
170+
If omitted, the check is global (applies to all validation types). If specified, the check applies only
171+
to that validation type.
172+
173+
Example:
174+
175+
```csharp
176+
StartsWith(new byte?[] { 0x25, 0x50, 0x44, 0x46, 0x2D }) // global
177+
.EndsWithAnyOf(new[]
178+
{
179+
new byte?[] { 0x25, 0x25, 0x45, 0x4F, 0x46 }
180+
}, MagicBytesValidator.Models.FileByteType.Strict) // strict only
181+
.TailContains(1024, new byte?[] { 0x25, 0x25, 0x45, 0x4F, 0x46 },
182+
MagicBytesValidator.Models.FileByteType.Lazy); // lazy only
183+
```
50184

51185
#### Expand the file type mapping
52186

@@ -59,7 +193,7 @@ var mapping = validator.Mapping;
59193
var mapping = formFileTypeProvider.Mapping;
60194

61195
// or create a new instance of the mapping:
62-
var mapping = new MagicBytesValidator.Services.Mapping();
196+
var mapping = new MagicBytesValidator.Services.Mapping();
63197
```
64198

65199
- Register a single `FileByteFilter`:
@@ -191,4 +325,4 @@ This can be useful when debugging or validating newly added FileTypes.
191325
▓▓ ▓▓▓▓▓▓▓▓▓▓
192326
▓▓
193327
▓▓▓▓▓▓▓▓▓ ▓▓
194-
```
328+
```

0 commit comments

Comments
 (0)