Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion MagicBytesValidator.Tests/Models/FileByteFilterMatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,69 @@ public void Should_not_match_pdf()
Assert.False(pdf.Matches(pdfTestData));
}

[Fact]
public void Should_match_pdf_with_trailing_bytes_in_default_mode()
{
var pdf = new Pdf();

var pdfTestData = "%PDF-\n%%EOF\nTRAILING"u8.ToArray();

Assert.True(pdf.Matches(pdfTestData, FileByteType.Lazy));
}

[Fact]
public void Should_not_match_pdf_with_trailing_bytes_in_strict_mode()
{
var pdf = new Pdf();

var pdfTestData = "%PDF-\n%%EOF\nTRAILING"u8.ToArray();

Assert.False(pdf.Matches(pdfTestData, FileByteType.Strict));
}

[Fact]
public void Should_not_match_pdf_when_eof_is_not_within_last_1024_bytes_in_default_mode()
{
var pdf = new Pdf();

var prefix = "%PDF-\n"u8.ToArray();
var eof = "%%EOF\n"u8.ToArray();

// put EOF early, then add >1024 bytes afterwards so it falls outside the last 1024 bytes
var trailing = new byte[1100];
for (var i = 0; i < trailing.Length; i++)
{
trailing[i] = (byte)'A';
}

var pdfTestData = prefix
.Concat(eof)
.Concat(trailing)
.ToArray();

Assert.False(pdf.Matches(pdfTestData, FileByteType.Lazy));
}

[Fact]
public void Should_match_pdf_when_eof_marker_is_present_in_default_mode()
{
var pdf = new Pdf();

var pdfTestData = "%PDF-\n...%%EOF...TAIL"u8.ToArray();

Assert.True(pdf.Matches(pdfTestData, FileByteType.Lazy));
}

[Fact]
public void Should_match_pdf_in_strict_mode_when_eof_is_at_end()
{
var pdf = new Pdf();

var pdfTestData = "%PDF-\n%%EOF"u8.ToArray();

Assert.True(pdf.Matches(pdfTestData, FileByteType.Strict));
}

[Fact]
public void Should_match_ppt()
{
Expand Down Expand Up @@ -144,4 +207,4 @@ public void Should_match_heic()

Assert.True(heic.Matches(testStream));
}
}
}
2 changes: 1 addition & 1 deletion MagicBytesValidator.Tests/ValidatorIsValidAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ public async Task Should_fail_incorrect_magicByte_sequence()
// Assert
Assert.False(invalidMagicByte);
}
}
}
43 changes: 25 additions & 18 deletions MagicBytesValidator/Formats/Pdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ namespace MagicBytesValidator.Formats;
/// <see href="https://www.garykessler.net/library/file_sigs.html"/>
public class Pdf : FileByteFilter
{
public Pdf() : base(
["application/pdf"],
["pdf"]
)
{
StartsWith([0x25, 0x50, 0x44, 0x46, 0x2D])
.EndsWithAnyOf(
[
[0x25, 0x25, 0x45, 0x4F, 0x46],
[0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0A],
[0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0A, 0x20],
[0x0D, 0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0D, 0x0A],
[0x0D, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0D], /* looks strange, but garykessler says so. */
[0x0D, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0A],
[0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0D, 0x0A],
]);
}
}
public Pdf() : base(
["application/pdf"],
["pdf"]
)
{
StartsWith([0x25, 0x50, 0x44, 0x46, 0x2D]); // %PDF-

EndsWithAnyOf(
[
[0x25, 0x25, 0x45, 0x4F, 0x46],
[0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0A],
[0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0A, 0x20],
[0x0D, 0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0D, 0x0A],
[0x0D, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0D], // garykessler variant
[0x0D, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0A],
[0x0A, 0x25, 0x25, 0x45, 0x4F, 0x46, 0x0D, 0x0A]
], FileByteType.Strict);

TailContains(
1024,
[0x25, 0x25, 0x45, 0x4F, 0x46], // %%EOF
FileByteType.Lazy
);
}
}
4 changes: 2 additions & 2 deletions MagicBytesValidator/MagicBytesValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>MagicBytesValidator</PackageId>
<Title>traperto GmbH</Title>
<Description>Validate files based on mime types, file extensions and magic byte sequences.</Description>
<Version>2.1.6</Version>
<Version>2.2.0</Version>
<Authors>Members of traperto GmbH</Authors>
<PackageProjectUrl>https://github.com/Traperto/magic-bytes-validator/blob/main/README.md</PackageProjectUrl>
<PackageTags>mime mimetype mimetypes magic magicbyte magicbytes extension extensions file
Expand All @@ -17,7 +17,7 @@
<Company>traperto GmbH</Company>

<PackageLicenseExpression>MIT</PackageLicenseExpression>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down
Loading