Skip to content

Commit 45fd603

Browse files
authored
Add support for .txt files (#27)
1 parent 5954034 commit 45fd603

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/ByteGuard.FileValidator/FileExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,10 @@ public static class FileExtensions
109109
/// Waveform Audio File Format.
110110
/// </summary>
111111
public const string Wav = ".wav";
112+
113+
/// <summary>
114+
/// Text File.
115+
/// </summary>
116+
public const string Txt = ".txt";
112117
}
113118
}

src/ByteGuard.FileValidator/FileValidator.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public class FileValidator
230230
{
231231
new byte[] { 0x57, 0x41, 0x56, 0x45 } // WAVE
232232
}
233+
},
234+
new FileDefinition
235+
{
236+
FileType = FileExtensions.Txt,
237+
AllowMissingSignature = true
233238
}
234239
};
235240

@@ -522,6 +527,12 @@ public bool HasValidSignature(string fileName, Stream stream)
522527
return false;
523528
}
524529

530+
// If the file definition allows for missing signatures, we can return early as the signature validation is effectively bypassed.
531+
if (fileDefinition.AllowMissingSignature)
532+
{
533+
return true;
534+
}
535+
525536
// As PDF documents are somewhat special in terms of both signature validation,
526537
// we need to investigate these files further. The PdfValidator is made specifically
527538
// for this purpose.

src/ByteGuard.FileValidator/Models/FileDefinition.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ internal class FileDefinition
1010
/// </summary>
1111
public string FileType { get; set; } = default!;
1212

13+
/// <summary>
14+
/// Whether this file type is allowed to omit a binary signature. Defaults to <c>false</c>.
15+
/// </summary>
16+
/// <remarks>
17+
/// Some file types, such as .txt files, do not have a binary signature.
18+
/// Setting this property to <c>true</c> allows files of this type to be considered valid even if they do not have a binary signature.
19+
/// This is useful for file types that do not have a binary signature, but still need to be validated by other mechanisms.
20+
/// </remarks>
21+
public bool AllowMissingSignature { get; set; } = false;
22+
1323
/// <summary>
1424
/// Valid header signatures.
1525
/// </summary>

tests/ByteGuard.FileValidator.Tests.Unit/FileValidatorTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,26 @@ public void HasValidSignature_FileNameDoesNotHaveExtension_ShouldThrowArgumentEx
418418
Assert.Throws<ArgumentException>(act);
419419
}
420420

421+
[Theory(DisplayName = "HasValidSignature(byte[]) should skip signature validation for file types that allow missing signatures and return true")]
422+
[InlineData(new byte[] { 0x74, 0x65, 0x73, 0x74 }, "test.txt")] // TXT
423+
public void HasValidSignature_FileTypeAllowsMissingSignature_ShouldReturnTrue(byte[] fileBytes, string fileName)
424+
{
425+
// Arrange
426+
var config = new FileValidatorConfiguration
427+
{
428+
SupportedFileTypes = [Path.GetExtension(fileName)],
429+
FileSizeLimit = ByteSize.MegaBytes(25),
430+
ThrowExceptionOnInvalidFile = true
431+
};
432+
var fileValidator = new FileValidator(config);
433+
434+
// Act
435+
var result = fileValidator.HasValidSignature(fileName, fileBytes);
436+
437+
// Assert
438+
Assert.True(result);
439+
}
440+
421441
[Theory(DisplayName = "IsOpenXmlFormat should return true for valid Open XML files")]
422442
[InlineData("test.docx")] // DOCX
423443
[InlineData("test.xlsx")] // XLSX

0 commit comments

Comments
 (0)