Bugfix: Handle directories uploaded into zip file. #3327
Merged
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.
A Zip archive only contains files and has no concept of directories. If the upload() VQL function is accidentally called on a directory, Velociraptor will try to read the directory and store it in the zip file. This leads to a 0 length file placed in the middle of a directory path:
/Foo/Bar <- zero length file
/Foo/Bar/Baz <- a real file.
This makes the real file impossible to extract and third party zip programs will be unable to write it once they created a file at the directory level above. This also confuses Velociraptor's zip accessor which emulates directories based on path prefixes and see /Foo/Bar as a file not a directory (so it does not recurse into it).
This PR ensures that when a directory is passed to upload() it will have a trailing / appended. This seems to be a convention with zip programs to denote a directory in a zip file. Additionally we consider a zero length file in the zip archive to be a directory regardless if it has a trailing / in order to be able to recover older collections which were written without the trailing /.