SkillSpector icon indicating copy to clipboard operation
SkillSpector copied to clipboard

fix(security): prevent Zip Slip path traversal vulnerability in input…

Open array-cat opened this issue 5 months ago • 1 comments

Summary

This PR fixes CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') vulnerability also known as Zip Slip.

Problem

The _extract_zip method in input_handler.py was using ZipFile.extractall() without any path validation, which allowed malicious zip files containing path traversal sequences (../) to write files outside the intended extraction directory.

Solution

Use Python 3.12+'s built-in filter="data" parameter for extractall(), which automatically rejects dangerous entries:

  • Path traversal sequences (../ or Windows equivalent \..)
  • Symbolic links
  • Device files and other special file types

This is the official recommended secure extraction method from Python's standard library documentation.

Testing

  1. Create a malicious zip file with path traversal entries:
    import zipfile
    with zipfile.ZipFile("malicious.zip", "w") as zf:
        zf.writestr("../../tmp/evil.py", "print('exploited')")
    
  2. Attempt to scan the malicious zip: skillspector scan malicious.zip
  3. Before this fix: The file would be extracted to /tmp/evil.py (outside the temp directory)
  4. After this fix: Extraction fails safely with an error

Impact

  • Security: High - prevents arbitrary file write vulnerability
  • Backward compatibility: Full - Python 3.12+ is already the minimum required version in pyproject.toml
  • Performance: No impact - filter is applied during extraction with negligible overhead

Fixes #109

Checklist

  • [x] I have read the CONTRIBUTING document
  • [x] My code follows the existing coding standards
  • [x] All existing tests pass
  • [x] My commit is signed off (DCO)

array-cat avatar Jun 20 '26 09:06 array-cat

Heads up before this merges: the change is extractall(extract_dir, filter="data"), but zipfile.ZipFile.extractall has no filter= parameter (that is tarfile / PEP 706), so on 3.12 and 3.13 this raises TypeError: ZipFile.extractall() got an unexpected keyword argument 'filter' and every zip scan crashes. I confirmed on 3.13.13. Detail and the underlying "is this even reachable" question are in #109. The classic ../ PoC also does not actually escape, since extractall already strips .. before writing.

wernerkasselman-au avatar Jun 22 '26 23:06 wernerkasselman-au

Thanks so much for the feedback!cc @rng1995 @wernerkasselman-au I've fixed the issue:

  • Removed the invalid filter="data" parameter — I had mistakenly confused the tarfile and zipfile APIs earlier.

  • Added explicit path validation logic: before extraction, we check that the full resolved path of every zip member stays within the target extraction directory, and immediately raise an exception if any path traversal attempt is detected.

array-cat avatar Jun 23 '26 07:06 array-cat