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

DataFlowStack Common Library #14673

Closed
wants to merge 59 commits into from

Conversation

ropwareJB
Copy link
Contributor

No description provided.

Dilan Bhalla and others added 30 commits April 24, 2023 14:46
Manual Merge: C# ZipSlip Conflict
Compatible with the latest released version of the CodeQL CLI
This pr is auto merged as it contains a mandatory file and is opened for more than 10 days.
Compatible with the latest released version of the CodeQL CLI
Compatible with the latest released version of the CodeQL CLI
Compatible with the latest released version of the CodeQL CLI
Compatible with the latest released version of the CodeQL CLI
Compatible with the latest released version of the CodeQL CLI
Dilan and others added 23 commits September 12, 2023 16:13
Compatible with the latest released version of the CodeQL CLI
Compatible with the latest released version of the CodeQL CLI
Compatible with the latest released version of the CodeQL CLI
…dels-libraries-and-queries-migration (#19)

16 cryptography models libraries and queries migration

Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
Compatible with the latest released version of the CodeQL CLI
Co-authored-by: Mathias Vorreiter Pedersen <[email protected]>
…_pick

Brodes/additional target cherry pick
…s-for-cpp (#23)

C++: Add an abstract class that can be used to extend `viableCallable`

Co-authored-by: Mathias Vorreiter Pedersen <[email protected]>
Copy link
Contributor

github-actions bot commented Nov 3, 2023

QHelp previews:

csharp/ql/src/Security Features/CWE-022/ZipSlip.qhelp

Arbitrary file access during archive extraction ("Zip Slip")

Extracting files from a malicious zip file, or similar type of archive, is at risk of directory traversal attacks if filenames from the archive are not properly validated.

Zip archives contain archive entries representing each file in the archive. These entries include a file path for the entry, but these file paths are not restricted and may contain unexpected special elements such as the directory traversal element (..). If these file paths are used to create a filesystem path, then a file operation may happen in an unexpected location. This can result in sensitive information being revealed or deleted, or an attacker being able to influence behavior by modifying unexpected files.

For example, if a zip file contains a file entry ..\sneaky-file, and the zip file is extracted to the directory c:\output, then naively combining the paths would result in an output file path of c:\output\..\sneaky-file, which would cause the file to be written to c:\sneaky-file.

Recommendation

Ensure that output paths constructed from zip archive entries are validated to prevent writing files to unexpected locations.

The recommended way of writing an output file from a zip archive entry is to conduct the following in sequence:

  1. Use Path.Combine(destinationDirectory, archiveEntry.FullName) to determine the raw output path.
  2. Use Path.GetFullPath(..) on the raw output path to resolve any directory traversal elements.
  3. Use Path.GetFullPath(destinationDirectory + Path.DirectorySeparatorChar) to determine the fully resolved path of the destination directory.
  4. Validate that the resolved output path StartsWith the resolved destination directory, aborting if this is not true.
    Another alternative is to validate archive entries against a whitelist of expected files.

Example

In this example, a file path taken from a zip archive item entry is combined with a destination directory. The result is used as the destination file path without verifying that the result is within the destination directory. If provided with a zip file containing an archive path like ..\sneaky-file, then this file would be written outside the destination directory.

using System.IO;
using System.IO.Compression;

class Bad
{
    public static void WriteToDirectory(ZipArchiveEntry entry,
                                        string destDirectory)
    {
        string destFileName = Path.Combine(destDirectory, entry.FullName);
        entry.ExtractToFile(destFileName);
    }
}

To fix this vulnerability, we need to make three changes. Firstly, we need to resolve any directory traversal or other special characters in the path by using Path.GetFullPath. Secondly, we need to identify the destination output directory, again using Path.GetFullPath, this time on the output directory. Finally, we need to ensure that the resolved output starts with the resolved destination directory, and throw an exception if this is not the case.

using System.IO;
using System.IO.Compression;

class Good
{
    public static void WriteToDirectory(ZipArchiveEntry entry,
                                        string destDirectory)
    {
        string destFileName = Path.GetFullPath(Path.Combine(destDirectory, entry.FullName));
        string fullDestDirPath = Path.GetFullPath(destDirectory + Path.DirectorySeparatorChar);
        if (!destFileName.StartsWith(fullDestDirPath)) {
            throw new System.InvalidOperationException("Entry is outside the target dir: " +
                                                                                 destFileName);
        }
        entry.ExtractToFile(destFileName);
    }
}

References

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants