-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add Compression best practices guide #52968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
9091e1d
9de9456
aa7cc00
ad061e9
cd9a53f
fe05af9
8905ae3
1c68aad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| using System.Formats.Tar; | ||
| using System.IO.Compression; | ||
| // <SafeExtractEntry> | ||
| void SafeExtractEntry(ZipArchiveEntry entry, string destinationPath, long maxDecompressedSize) | ||
| { | ||
| // Check the declared uncompressed size first (can be spoofed, but is a fast first check). | ||
| if (entry.Length > maxDecompressedSize) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Entry '{entry.FullName}' declares size {entry.Length}, exceeding limit {maxDecompressedSize}."); | ||
| } | ||
|
|
||
| using Stream source = entry.Open(); | ||
| using FileStream destination = File.Create(destinationPath); | ||
|
|
||
| byte[] buffer = new byte[81920]; | ||
| long totalBytesRead = 0; | ||
| int bytesRead; | ||
|
|
||
| while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) | ||
| { | ||
| totalBytesRead += bytesRead; | ||
| if (totalBytesRead > maxDecompressedSize) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"Extraction of '{entry.FullName}' exceeded limit of {maxDecompressedSize} bytes."); | ||
| } | ||
| destination.Write(buffer, 0, bytesRead); | ||
| } | ||
| } | ||
| // </SafeExtractEntry> | ||
|
|
||
| // <SafeExtractArchive> | ||
| void SafeExtractArchive(ZipArchive archive, string destinationDir, | ||
| long maxTotalSize, int maxEntryCount) | ||
| { | ||
| // Some zip bombs contain millions of tiny entries (e.g., "42.zip"). | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
| if (archive.Entries.Count > maxEntryCount) | ||
| { | ||
| throw new InvalidOperationException("Archive contains an excessive number of entries."); | ||
| } | ||
|
|
||
| long totalExtracted = 0; | ||
| foreach (ZipArchiveEntry entry in archive.Entries) | ||
| { | ||
| totalExtracted += entry.Length; | ||
| if (totalExtracted > maxTotalSize) | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
| { | ||
| throw new InvalidOperationException( | ||
| $"Archive total decompressed size exceeds the allowed limit of {maxTotalSize} bytes."); | ||
| } | ||
| // ... extract each entry with per-entry limits too | ||
| } | ||
| } | ||
| // </SafeExtractArchive> | ||
|
|
||
| // <PathValidation> | ||
| void ValidatePaths(ZipArchive archive, string destinationDir) | ||
| { | ||
| string fullDestDir = Path.GetFullPath(destinationDir); | ||
| if (!fullDestDir.EndsWith(Path.DirectorySeparatorChar)) | ||
| fullDestDir += Path.DirectorySeparatorChar; | ||
|
|
||
| foreach (ZipArchiveEntry entry in archive.Entries) | ||
| { | ||
| string destPath = Path.GetFullPath(Path.Combine(fullDestDir, entry.FullName)); | ||
|
|
||
| if (!destPath.StartsWith(fullDestDir, StringComparison.Ordinal)) | ||
|
Comment on lines
+46
to
+54
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have any plans to make this easier to do correctly in the future?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do have the trailing separator check for both zips and tars, but for the higher-level, convenience
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MihaZupan There is this proposal for |
||
| throw new IOException( | ||
| $"Entry '{entry.FullName}' would extract outside the destination directory."); | ||
|
|
||
| // ... safe to extract | ||
| } | ||
| } | ||
| // </PathValidation> | ||
|
|
||
| // <VulnerablePattern> | ||
| void DangerousExtract(string extractDir) | ||
| { | ||
| // ⚠️ DANGEROUS: entry.FullName could contain "../" sequences | ||
| using ZipArchive archive = ZipFile.OpenRead("archive.zip"); | ||
| foreach (ZipArchiveEntry entry in archive.Entries) | ||
| { | ||
| string destinationPath = Path.Combine(extractDir, entry.FullName); | ||
| entry.ExtractToFile(destinationPath, overwrite: true); // NO path validation! | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| // </VulnerablePattern> | ||
|
|
||
| // <SafeExtractZip> | ||
| void SafeExtractZip(string archivePath, string destinationDir, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the safe way to use our API is to copy-paste such a helper into your code, we should consider exposing better APIs E.g.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've got a point, maybe we should consider creating per-entry safe extraction methods or add some options to let user decide how to interact with these apis |
||
| long maxTotalSize, long maxEntrySize, int maxEntryCount) | ||
| { | ||
| // Resolve the destination to an absolute path and ensure it ends with a | ||
| // directory separator. This trailing separator is essential — without it, | ||
| // the StartsWith check below could be tricked by paths like | ||
| // "/safe-dir-evil/" matching "/safe-dir". | ||
| string fullDestDir = Path.GetFullPath(destinationDir); | ||
| if (!fullDestDir.EndsWith(Path.DirectorySeparatorChar)) | ||
| fullDestDir += Path.DirectorySeparatorChar; | ||
|
|
||
| Directory.CreateDirectory(fullDestDir); | ||
|
|
||
| using var archive = new ZipArchive(File.OpenRead(archivePath), ZipArchiveMode.Read); | ||
|
|
||
| // Check the entry count up front. ZIP central directory is read eagerly, | ||
| // so archive.Entries.Count is available immediately without iterating. | ||
| if (archive.Entries.Count > maxEntryCount) | ||
| throw new InvalidOperationException("Archive contains too many entries."); | ||
|
|
||
| long totalSize = 0; | ||
| foreach (ZipArchiveEntry entry in archive.Entries) | ||
| { | ||
| // Enforce per-entry and cumulative size limits using the declared | ||
| // uncompressed size. Note: this value is read from the archive header | ||
| // and could be spoofed by a malicious archive — for defense in depth, | ||
| // also monitor actual bytes read during decompression (see the zip | ||
| // bomb section for a streaming size check example). | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
| totalSize += entry.Length; | ||
| if (entry.Length > maxEntrySize) | ||
| throw new InvalidOperationException( | ||
| $"Entry '{entry.FullName}' exceeds per-entry size limit."); | ||
| if (totalSize > maxTotalSize) | ||
| throw new InvalidOperationException("Archive exceeds total size limit."); | ||
|
|
||
| // Resolve the full destination path using Path.GetFullPath, which | ||
| // normalizes away any "../" segments. Then verify the result still | ||
| // starts with the destination directory. | ||
| string destPath = Path.GetFullPath(Path.Combine(fullDestDir, entry.FullName)); | ||
| if (!destPath.StartsWith(fullDestDir, StringComparison.Ordinal)) | ||
| throw new IOException( | ||
| $"Entry '{entry.FullName}' would extract outside the destination."); | ||
|
|
||
| // ZIP uses a convention where directory entries have names ending in '/'. | ||
| // Path.GetFileName returns empty for these, so we use that to | ||
| // distinguish directories from files. | ||
|
adegeo marked this conversation as resolved.
Outdated
|
||
| if (string.IsNullOrEmpty(Path.GetFileName(destPath))) | ||
| { | ||
| Directory.CreateDirectory(destPath); | ||
| } | ||
| else | ||
| { | ||
| // Ensure the parent directory exists before extracting the file. | ||
| Directory.CreateDirectory(Path.GetDirectoryName(destPath)!); | ||
|
rzikm marked this conversation as resolved.
|
||
| entry.ExtractToFile(destPath, overwrite: false); | ||
| } | ||
| } | ||
| } | ||
| // </SafeExtractZip> | ||
|
|
||
| // <SafeExtractTar> | ||
| void SafeExtractTar(Stream archiveStream, string destinationDir, | ||
| long maxTotalSize, long maxEntrySize, int maxEntryCount) | ||
| { | ||
| // Same trailing-separator technique as the ZIP example. | ||
| string fullDestDir = Path.GetFullPath(destinationDir); | ||
| if (!fullDestDir.EndsWith(Path.DirectorySeparatorChar)) | ||
| fullDestDir += Path.DirectorySeparatorChar; | ||
|
|
||
| Directory.CreateDirectory(fullDestDir); | ||
|
|
||
| using var reader = new TarReader(archiveStream); | ||
| TarEntry? entry; | ||
| long totalSize = 0; | ||
| int entryCount = 0; | ||
|
|
||
| // TAR has no central directory — entries are read one at a time. | ||
| // GetNextEntry() returns null when the archive is exhausted. | ||
| while ((entry = reader.GetNextEntry()) is not null) | ||
| { | ||
| if (++entryCount > maxEntryCount) | ||
| throw new InvalidOperationException("Archive contains too many entries."); | ||
|
|
||
| if (entry.Length > maxEntrySize) | ||
| throw new InvalidOperationException( | ||
| $"Entry '{entry.Name}' exceeds per-entry size limit."); | ||
| totalSize += entry.Length; | ||
| if (totalSize > maxTotalSize) | ||
| throw new InvalidOperationException("Archive exceeds total size limit."); | ||
|
|
||
| // Symbolic links and hard links can be used to write files outside the | ||
| // extraction directory or to overwrite sensitive files. The safest | ||
| // approach for untrusted input is to skip them entirely. | ||
| if (entry.EntryType is TarEntryType.SymbolicLink or TarEntryType.HardLink) | ||
| continue; | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // Global extended attributes are PAX metadata entries that apply to all | ||
| // subsequent entries. They contain no file data and should be skipped. | ||
| if (entry.EntryType is TarEntryType.GlobalExtendedAttributes) | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
| continue; | ||
|
|
||
| // Normalize and validate the path, same as the ZIP example. | ||
| string destPath = Path.GetFullPath(Path.Join(fullDestDir, entry.Name)); | ||
|
rzikm marked this conversation as resolved.
|
||
| if (!destPath.StartsWith(fullDestDir, StringComparison.Ordinal)) | ||
| throw new IOException( | ||
| $"Entry '{entry.Name}' would extract outside the destination."); | ||
|
|
||
| if (entry.EntryType is TarEntryType.Directory) | ||
| { | ||
| Directory.CreateDirectory(destPath); | ||
| } | ||
| else if (entry.DataStream is not null) | ||
| { | ||
| // For file entries, copy the data stream to a new file. | ||
| // We use entry.DataStream directly instead of ExtractToFile because | ||
| // ExtractToFile rejects symbolic and hard link entries (already | ||
| // filtered above) and requires a file path rather than a stream. | ||
| Directory.CreateDirectory(Path.GetDirectoryName(destPath)!); | ||
| using var fileStream = File.Create(destPath); | ||
| entry.DataStream.CopyTo(fileStream); | ||
|
alinpahontu2912 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
| } | ||
| // </SafeExtractTar> | ||
|
|
||
| // <StreamingApproach> | ||
| void StreamingModify() | ||
| { | ||
| // ✅ Streaming approach for large archives | ||
| using var input = new ZipArchive(File.OpenRead("large.zip"), ZipArchiveMode.Read); | ||
| using var output = new ZipArchive(File.Create("modified.zip"), ZipArchiveMode.Create); | ||
|
|
||
| foreach (var entry in input.Entries) | ||
| { | ||
| if (ShouldKeep(entry)) | ||
| { | ||
| var newEntry = output.CreateEntry(entry.FullName); | ||
| using var src = entry.Open(); | ||
| using var dst = newEntry.Open(); | ||
| src.CopyTo(dst); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| bool ShouldKeep(ZipArchiveEntry entry) => true; | ||
| // </StreamingApproach> | ||
|
|
||
| // <TarStreaming> | ||
| void TarStreamingRead(Stream archiveStream) | ||
| { | ||
| using var reader = new TarReader(archiveStream); | ||
| TarEntry? entry; | ||
| while ((entry = reader.GetNextEntry()) is not null) | ||
| { | ||
| if (entry.DataStream is not null) | ||
| { | ||
| string safePath = "output.bin"; | ||
| // Copy now — the stream becomes invalid after the next GetNextEntry() call | ||
| using var fileStream = File.Create(safePath); | ||
| entry.DataStream.CopyTo(fileStream); | ||
|
alinpahontu2912 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
rzikm marked this conversation as resolved.
Outdated
|
||
| // </TarStreaming> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
|
|
||
| </Project> |
Uh oh!
There was an error while loading. Please reload this page.