-
Notifications
You must be signed in to change notification settings - Fork 92
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8e3f096
yar
scottoneil-ms c4de01e
release notes
scottoneil-ms ff44265
Merge remote-tracking branch 'origin/main' into users/scott/sniffZip
scottoneil-ms b8f6f6b
Merge remote-tracking branch 'origin/main' into users/scott/sniffZip
scottoneil-ms 0d12563
Merge branch 'main' into users/scott/sniffZip
michaelcfanning b0c98ba
Merge branch 'main' into users/scott/sniffZip
scottoneil-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -34,8 +34,8 @@ public bool IsBinary | |
{ | ||
get | ||
{ | ||
string extension = Path.GetExtension(Uri.ToString()); | ||
return this.binaryExtensions.Contains(extension); | ||
GetArtifactData(); | ||
return this.bytes != null; | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
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. Does this need to be disposed? 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. 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); | ||
|
@@ -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; | ||
} | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.