-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from neilharvey/bug-fix/malicious-file-formats
Fix PDF header vulnerability
- Loading branch information
Showing
13 changed files
with
112 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.IO; | ||
|
||
namespace FileSignatures.Formats; | ||
|
||
public class AdobePdf : Pdf | ||
{ | ||
private const byte VersionNumberPlaceholder = 0x00; | ||
|
||
public AdobePdf() : base([ | ||
0x25, 0x21, 0x50, 0x53, 0x2D, 0x41, 0x64, 0x6F, 0x62, 0x65, 0x2D, VersionNumberPlaceholder, 0x2E, | ||
VersionNumberPlaceholder, 0x20, 0x50, 0x44, | ||
0x46, 0x2D, VersionNumberPlaceholder, 0x2E, VersionNumberPlaceholder | ||
]) | ||
{ | ||
} | ||
|
||
public override bool IsMatch(Stream stream) | ||
{ | ||
if (stream == null || (stream.Length < HeaderLength && HeaderLength < int.MaxValue) || Offset > stream.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
stream.Position = Offset; | ||
|
||
for (var i = 0; i < Signature.Count; i++) | ||
{ | ||
var b = stream.ReadByte(); | ||
if (!IsSignatureByte(b, i)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected bool IsSignatureByte(int value, int signatureIndex) | ||
{ | ||
return IsVersionNumber(value, Signature[signatureIndex]) | ||
|| value == Signature[signatureIndex]; | ||
} | ||
|
||
private static bool IsVersionNumber(int value, byte signatureByte) | ||
{ | ||
var isNumber = value is >= 0x30 and <= 0x39; | ||
return signatureByte == VersionNumberPlaceholder && isNumber; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace FileSignatures.Formats; | ||
|
||
/// <summary> | ||
/// Specifies the format of an OpenDocument file. | ||
/// </summary> | ||
public abstract class OpenDocument : Zip | ||
{ | ||
private const int SubsignatureOffset = 30; | ||
|
||
protected OpenDocument(byte[] subsignature, string mediaType, string extension) : base(SubsignatureOffset + subsignature.Length, mediaType, extension) | ||
{ | ||
Subsignature = new ReadOnlyCollection<byte>(subsignature); ; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the subsignature of the OpenDocument file. | ||
/// This will be the media type in byte format e.g. 'mimetypeapplication/vnd.oasis.opendocument.presentation' | ||
/// </summary> | ||
public ReadOnlyCollection<byte> Subsignature { get; } | ||
|
||
public override bool IsMatch(Stream stream) | ||
{ | ||
if (!base.IsMatch(stream)) | ||
return false; | ||
|
||
stream.Position = SubsignatureOffset; | ||
|
||
for (int i = 0; i < Subsignature.Count; i++) | ||
{ | ||
var b = stream.ReadByte(); | ||
if (b != Subsignature[i]) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-137 KB
test/FileSignatures.Tests/Samples/test_header_somewhere_in_1024_first_bytes.pdf
Binary file not shown.