Skip to content
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

Enable peekable-based binary sniffing on ZipArchiveArtifacts #2773

Merged
merged 6 commits into from
Feb 5, 2024
Merged
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
1 change: 1 addition & 0 deletions ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* BUG: Fix `UnsupportedOperationException` in `ZipArchiveArtifact`.
* BUG: Fix `MultithreadedAnalyzeCommandBase` to return rich return code with the `--rich-return-code` option.
* NEW: Add `IsBinary` property to `IEnumeratedArtifact` and implement the property in `ZipArchiveArtifact`.
* NEW: Switch to content-based `IsBinary` categorization for `ZipArchiveArtifact`s.
* PRF: Change default `max-file-size-in-kb` parameter to 10 megabytes.
* PRF: Add support for efficiently peeking into non-seekable streams for binary/text categorization.
* NEW: Add a new `--timeout-in-seconds` parameter to `AnalyzeOptionsBase`, which will override the `TimeoutInMilliseconds` property in `AnalyzeContextBase`.
Expand Down
25 changes: 17 additions & 8 deletions src/Sarif/ZipArchiveArtifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public bool IsBinary
{
get
{
string extension = Path.GetExtension(Uri.ToString());
return this.binaryExtensions.Contains(extension);
GetArtifactData();
return this.bytes != null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this check was cheap, now it looks like it's potentially much more expensive since you're introspecting the actual stream. Are there any concerns around perf changes here?

Copy link
Contributor Author

@scottoneil-ms scottoneil-ms Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort of, but that expectation has to be baked into the whole premise of categorizing based on contents.

I would say I just gained appreciation on another thread that the way we protect ourselves from scanning obviously bogus content is the file deny extensions, which remains a feature. So you could imagine that once you've passed that check, we're going to scan, so the only perf-sensitivity to this change would be when we do the first read. (Not if.) That's not a high-stakes question.

Interested in Michael's take on this.

}
}

Expand Down Expand Up @@ -91,8 +91,20 @@ public byte[] Bytes
{
if (this.contents == null && this.bytes == null)
{
string extension = Path.GetExtension(Uri.ToString());
if (this.binaryExtensions.Contains(extension))
const int PeekWindowBytes = 1024;
var peekable = new PeekableStream(this.Stream, PeekWindowBytes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be disposed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, peekable just has the underlying stream and a managed buffer. 1k, in this case.


byte[] header = new byte[PeekWindowBytes];
int length = this.Stream.Read(header, 0, header.Length);
bool isText = FileEncoding.IsTextualData(header, 0, length);

peekable.Rewind();

if (isText)
{
this.contents = new StreamReader(Stream).ReadToEnd();
}
else
{
// The underlying System.IO.Compression.DeflateStream throws on reads to get_Length.
using var ms = new MemoryStream((int)SizeInBytes.Value);
Expand All @@ -113,12 +125,9 @@ public byte[] Bytes
ms.Read(this.bytes, 0, this.bytes.Length);
}
}
else
{
this.contents = new StreamReader(Stream).ReadToEnd();
}
}
}

this.entry = null;
}

Expand Down
16 changes: 10 additions & 6 deletions src/Test.UnitTests.Sarif/ArtifactProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;

using FluentAssertions;

Expand All @@ -32,14 +31,19 @@ public void MultithreadedZipArchiveArtifactProvider_RetrieveSizeInBytesBeforeRet
[Fact]
public void MultithreadedZipArchiveArtifactProvider_RetrieveSizeInBytesBeforeRetrievingBytes()
{
string entryContents = $"{Guid.NewGuid()}";
string filePath = this.GetType().Assembly.Location;
using FileStream reader = File.OpenRead(filePath);

int headerSize = 1024;
byte[] data = new byte[headerSize];
reader.Read(data, 0, data.Length);

// Note that even thought we populate an archive with text contents, the extension
// of the archive entry indicates a binary file. So we expect binary data on expansion.
ZipArchive zip = CreateZipArchiveWithTextContents("test.exe", entryContents);
// Note that even thought we populate an archive with binary contents, the extension
// of the archive entry indicates a text file. We still expect binary data on expansion.
ZipArchive zip = CreateZipArchiveWithBinaryContents("test.txt", data);
var artifactProvider = new MultithreadedZipArchiveArtifactProvider(zip, FileSystem.Instance);

ValidateBinaryContents(artifactProvider.Artifacts, Encoding.UTF8.GetBytes(entryContents));
ValidateBinaryContents(artifactProvider.Artifacts, data);
}

[Fact]
Expand Down
Loading