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

Unhandled edge case in _sanitizedPath #680

Closed
BlueSquare1 opened this issue Jul 10, 2023 · 1 comment
Closed

Unhandled edge case in _sanitizedPath #680

BlueSquare1 opened this issue Jul 10, 2023 · 1 comment

Comments

@BlueSquare1
Copy link

Description:

There is an unhandeled edge case where we can crash the application when it tries to unzip a specially crafted zip file

Technical details:

Below is a code snippet from the _sanitizedPath function used to sanitize file paths coming from zip entries before extraction to prevent path traversal, the function prepends file:/// to the provided path, standardizes it using NSURL and then removes the prepended file:///, when provided with the following path however /.., the standardized path returned by NSURL becomes file://, which has 7 characters, the package however expects at least 8 characters (length of the prepended text), this results in crashing the application.

// Add scheme "file:///" to support sanitation on names with a colon like "file:a/../../../usr/bin"
strPath = [@"file:///" stringByAppendingString:strPath];

// Sanitize path traversal characters to prevent directory backtracking. Ignoring these characters mimicks the default behavior of the Unarchiving tool on macOS.
// "../../../../../../../../../../../tmp/test.txt" -> "tmp/test.txt"
// "a/b/../c.txt" -> "a/c.txt"
strPath = [NSURL URLWithString:strPath].standardizedURL.absoluteString;

// Remove the "file:///" scheme
strPath = [strPath substringFromIndex:8];

PoC:

import zipfile

def compress_file(filename):
    with zipfile.ZipFile('payload.zip', 'w') as zipf:
        zipf.writestr(filename, "Test payload")

filename = '/..'

compress_file(filename)
@BlueSquare1 BlueSquare1 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 20, 2023
Coeur added a commit that referenced this issue Jul 22, 2023
@Coeur
Copy link
Member

Coeur commented Jul 22, 2023

Thank you. Fixed in 49a22a5

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

No branches or pull requests

2 participants