Skip to content

Commit 75d519e

Browse files
committed
Add compound file magic header validation
1 parent 514135c commit 75d519e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

QuickLook.Plugin/QuickLook.Plugin.ArchiveViewer/CompoundFileBinary/CompoundFileExtractor.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System;
1919
using System.Collections.Generic;
2020
using System.IO;
21+
using System.Linq;
2122
using System.Runtime.InteropServices.ComTypes;
2223

2324
namespace QuickLook.Plugin.ArchiveViewer.CompoundFileBinary;
@@ -42,6 +43,22 @@ public static void ExtractToDirectory(string compoundFilePath, string destinatio
4243
Directory.CreateDirectory(destinationDirectory);
4344
}
4445

46+
// Ensure the compound file exists
47+
if (!File.Exists(compoundFilePath))
48+
throw new FileNotFoundException("Compound file not found.", compoundFilePath);
49+
50+
// Validate magic header for OLE compound file: D0 CF 11 E0 A1 B1 1A E1
51+
byte[] magicHeader = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
52+
byte[] header = new byte[8];
53+
using (FileStream fs = new(compoundFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
54+
{
55+
int read = fs.Read(header, 0, header.Length);
56+
if (read < header.Length || !header.SequenceEqual(magicHeader))
57+
{
58+
throw new InvalidDataException("The specified file does not appear to be an OLE Compound File (invalid header).");
59+
}
60+
}
61+
4562
// Open the compound file as an IStorage implementation wrapped by DisposableIStorage.
4663
using DisposableIStorage storage = new(compoundFilePath, STGM.DIRECT | STGM.READ | STGM.SHARE_EXCLUSIVE, IntPtr.Zero);
4764
IEnumerator<STATSTG> enumerator = storage.EnumElements();

0 commit comments

Comments
 (0)