Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Python for the Paranoid: How to secure your Pyt...

Avatar for Sau Sau
August 25, 2026

Python for the Paranoid: How to secure your Python code

Presented at PyCon Hong Kong 2025

Avatar for Sau

Sau

August 25, 2026

More Decks by Sau

Other Decks in Technology

Transcript

  1. “The most secure code in the world is code which

    is never written.” - Colin Percival 4
  2. OWASP Top 10 (2021) There’s also OWASP Top 10 LLM,

    etc Source: https://owasp.org/Top10/ 6
  3. Today’s Agenda 1. Unsafe Data 2. Unsafe Algorithms 3. Unsafe

    Versions 4. Unsafe Third-party Packages 8
  4. With a simple Google search… Source: A better zip bomb

    https://www.bamsoftware.com/hacks/zipbomb/ 11
  5. 1.1 Denial of Service (DoS) Zip bomb (Zip-of-death) XML bomb

    (Billion laughs attack) < 1 kB � 3 GB 12
  6. History of pickle 2003: Warning about use of pickle in

    Python 2.3 (PEP-307) 2022: Proposal of SafeTensors, a safer format in the ML community 2024: BentoML pickle-related vulnerability (CVE-2024-2912) 2025: On Hugging Face, 45% of models continue to use pickle* *Kellas et al (2025) "PickleBall: Secure Deserialization of Pickle-based Machine Learning Models" https://arxiv.org/html/2508.15987v1 16
  7. Lesson: Always validate your input! Not only ZIP, XML, pickle.

    YAML, JSON, etc can also be unsafe. 2021: Vulnerability in Tensorflow (CVE-2021-37678) 18
  8. History of random 20xx: random is not for security uses

    2012: Plone (CMS) vulnerability (CVE-2012-5508) 2016: Addition of secrets module in Python 3.6 (PEP-506) 2025: Dify vulnerability (CVE-2025-1796) 22
  9. MD5 collision attack Source: Nat McHugh (2015) "Create your own

    MD5 collisions" https://natmchugh.blogspot.com/2015/02/create-your-own-md5-collisions.html 25
  10. MD5 collision attack (potential impact) “I recommend Alice” “I grant

    Alice full access” Source: Hash Collisions (The Poisoned Message Attack) "The Story of Alice and her Boss" https://www.sos.cs.ru.nl/applications/courses/security2015/md5collisions/index.html 26
  11. Comparing hashing algorithms �� MD5 dc20dd27bf6ad19d14abba6837cfffd8 �� pyconhk2025 SHA-1 SHA-256

    9c8d02347c5f6698c9fbdcbad902f6d3ac5937e5 4399f1015fd012fae2dc6e7c61c3976500f01168b 96bebc53515cab0535affd0 27
  12. Example: Zip bomb fix in Python 2023 Sep: Zipfile vulnerability

    (CVE-2024-0450) Python 3.12.2, 3.11.8, 3.10.13, 3.9.18, 3.8.18 2024 Jan: Security patch released 34
  13. Lesson: Use latest Python versions if you can It’s FREE!

    Minor releases largely backward compatible (for Python 3.x to Python 3.y where y > x) For libraries, use automated tools like Dependabot or Renovate 36
  14. Anyone can upload packages to PyPI asynciio -> asyncio beaufifulsoup

    -> beautifulsoup matplotib -> matplotlib request -> requests pytorch -> torch Source: https://pypi.org/project/pytorch/ 40
  15. CI/CD 2025: GitHub Action tj-actions/changed-file was compromised, leaking secrets from

    20,000 repos (CVE-2025-30066) Source: https://unit42.paloaltonetworks.com/github-actions-supply-chain-attack/ 42
  16. Lesson: Pin 📌 or lock🔒 your files Python: Lock with

    poetry.lock, Pipfile.lock, uv.lock 🔒 Github Actions: Pin with the SHA commit hash📌 - uses: actions/checkout@main - uses: actions/checkout@v4 - uses: actions/checkout@692973e3d937129bcbf40652 eb9f2f61becf3332 # v4.1.7 - uses: actions/[email protected] 43
  17. Today’s Agenda 1. Unsafe Data 2. Unsafe Algorithms 3. Unsafe

    Versions 4. Unsafe Third-party Packages 44
  18. Recap 1. Unsafe Data: Validate input data 2. Unsafe Algorithms:

    Refer to recommended hash functions. Use secrets over random for security uses 3. Unsafe Versions: Take note of Python/library lifespan. Use tools such as uv/ Dependabot/ Renovate 4. Unsafe 3rd party: Pay attention to the entire ecosystem. Build safely by pinning or locking to the commit hash 46
  19. GitHub recommended settings (advanced level) GitHub Actions learnings from the

    recent nx hack https://jessehouwing.net/github-actions-learnings-from-the-recent-nx-hack/ GitHub Actions: A Cloudy Day for Security - Part 1 https://binarysecurity.no/posts/2025/08/securing-gh-actions-part1 50
  20. How to secure your Python code Scope is beyond Python:

    algorithms, CI/CD, etc Think not only about “what works”, but also “what can break” 55