Python security best practices cheat sheet | Snyk (2024)

Python security best practices cheat sheet | Snyk (1)

In 2019, Snyk released its first Python cheat sheet. Since then, many aspects of Python security have changed. Using our learnings as a developer security company — as well as Python-specific best practices — we compiled this updated cheat sheet to make sure you keep your Python code secure.And before going any further, I need to give special thanks to Chibo and Daniel for their help with this cheat sheet!

Download the 2021 Python Security Best Practices Cheat Sheet

Here are the Python security tips we’ll explore:

  1. Always sanitize external data

  2. Scan your code

  3. Be careful when downloading packages

  4. Review your dependency licenses

  5. Do not use the system standard version of Python

  6. Use Python’s capability for virtual environments

  7. Set DEBUG = False in production

  8. Be careful with string formatting

  9. (De)serialize very cautiously

  10. Use Python type annotations

One quick note before we get started. It’s important to note that Snyk’s data about the Python ecosystem, as well as academic research, shows that Python is no more (or less) secure than other widely used languages. This cheat sheet is just specifically for our Pythonistas. We’d recommend checking out all of our other security cheat sheets to learn how to stay safe in other ecosystems.

1. Always sanitize external data

One vector of attack for any application is external data, which can be used for injection, XSS, or denial of service (DOS) attacks. A general rule for maintaining Python security is to always sanitize data (remove sensitive information) from external sources whether the data originates from a user input form, scraping a website, or a database request. Also, sanitize as soon as the data enters the application to prevent insecure handling. This reduces the risk that unsanitized sensitive data will be handled by your application accidentally.

Starting with sanitization, it always makes more sense to check for what the input should be than to try to handle the exceptions. We also recommend using well-maintained libraries for sanitization. Here are two:

  • schema is “a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.

  • bleach is “an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes.”

Major frameworks come with their own sanitation functions, like Flask’s flask.escape() or Django’s django.utils.html.escape(). The goal of any of these functions is to secure potentially malicious HTML input like:

1>>> import bleach2>>> bleach.clean('an XSS <script>navigate(...)</script> example')3'an XSS &lt;script&gt;navigate(...)&lt;/script&gt; example'

The limitation of this approach is that libraries are not good for everything. They are specialized in their domain. As a note, be sure to read to the end of this post to get more information about working with other data formats, such as XML, which can also contain malicious data.

Another often used option is to leave the rendering of HTML to templating engines such as Jinja. It provides lots of capabilities, and amongst them is auto-escaping to prevent XSS using MarkupSafe.

Another aspect of sanitization is preventing data from being used as a command. A typical example is an SQL injection. Instead of stitching strings and variables together to generate an SQL query, it is advisable to use named-parameters to tell the database what to treat as a command and what as data.

1# Instead of this2cursor.execute(f"SELECT admin FROM users WHERE username = '{username}'");3# ...do this...4cursor.execute("SELECT admin FROM users WHERE username = %(username)s", {'username': username}); 

Or even better, use Object-Relational Mapping (ORM), such as sqlalchemy, which would make the example query look like this:

1query = session.query(User).filter(User.name.like('%{username}'))

Here you get more readable code, as well as ORM optimizations like caching, plus more security and performance!

If you want to learn more, check out our SQL injection cheat sheet.

2. Scan your code

Developers have a wide array of static code analysis tools at their disposal for maintaining Python security. Let’s take a look at three different levels of tools.

First, the linter level. PEP8 has been serving for decades now as a style guide for Python. Various tools are available (and built into IDEs) to check against this style guide, like pep8, pylint, flake8, and more.

Next, tools like bandit transform code into an abstract syntax tree (AST) and perform queries on it to find typical security issues. This is a level above what typical linters do, which work on a syntactical level. Still, bandit is limited by its intermediate representation and performance. For example, bandit cannot detect data flow related issues (known as taint-analysis) and these result in devastating flaws (injections like SQL injection or XSS as an example).

Finally, Static Application Security Testing (SAST) tools like Snyk Code run a semantic analysis, taking even complex interfile issues into account. Unlike other tools on this level, Snyk Code is developer-friendly by scanning fast and integrating into the IDE (or your command line directly). Snyk Code explains its highly accurate findings and provides help, including examples how to fix your Python security problems. And to top that, it’s easy to get started with and free to use on open source (plus a limited amount of non-OSS tests).

3. Be careful when downloading packages

It is easy to install packages, but they’re also an easy way to introduce Python security vulnerabilities. Typically, developers use the standard package installer for Python (pip) which uses the Python Pack Index (PyPI). This makes it important to understand how packages are added to PyPI.

PyPI has a procedure for reporting security concerns. If someone reports a malicious package, or a problem within PyPI, it is addressed, but packages added to PyPI do not undergo review — this would be an unrealistic expectation of the volunteers who maintain PyPI.

Therefore, it is wise to assume that there are malicious packages within PyPI and you should act accordingly. Reasonable steps include doing a bit of research on the package you want to install and ensuring that you carefully spell out the package name (a package named for a common misspelling of a popular package could execute malicious code). Before downloading a package, make sure to check it on Snyk Advisor.

Python security best practices cheat sheet | Snyk (2)

Doing a quick search for a package on Snyk Advisor gives you a lot of information on the package, its support in the community, its history of bugs and fixes, and a lot more. Snyk Advisor also provides the installation command at the top of the result page. It is a best practice to copy and paste that spelling to prevent typosquatting. Snyk Advisor can tell you whether or not you should trust a package. You can see the history of security issues and the time it took to get them fixed.

Another best practice is to use virtual environments to isolate projects from each other. Also, use pip freeze or a comparable command to record changes in the environment in the requirement list.

Maintaining references in an up-to-date manner, Snyk Open Source is based on an industry-leading vulnerability database recording security issues and possible fixes. Snyk Open Source runs scans using the requirements and provides actionable information about discovered vulnerabilities of direct and transitive dependencies and helps you to fix them right away.

4. Review your dependency licenses

When considering using an open source project, it is important to understand how these projects are licensed. Open source projects are free and available to use, but there may still be terms and conditions applied. These terms usually involve how the software is used, whether you need to make any changes you make to the software publicly available, and other similar requirements. You should become familiar with the open source licenses necessary for the projects you use, so you are sure that you are not compromising yourself legally.

If the project adopts a more restrictive license than you anticipated (GPL, SSPL, etc.), you can end up cornering yourself, leaving you to either comply with the terms of the license or cease using the project. Additionally, if you need to make changes to a project that does not have a license, you might run afoul with copyright law.

To ensure that your project is sustainable and you do not expose yourself to unnecessary Python security and legal risks, scan and fix license and vulnerability issues in your project’s dependencies.

Snyk Open Source can help you with open source license compliance management. It provides a developer-friendly way to gain end-to-end visibility while providing a flexible governance.

5. Do not use the system standard version of Python

Most POSIX systems come preloaded with a version of Python. The problem with most built-in Python distributions is that they aren’t current.

So, make sure to use the latest version of Python available for your system as well as the official containers designed to run Python and keep it updated. Snyk is here to help you. Scan your containers for necessary updates using Snyk Container and check your dependencies using Snyk Open Source.

6. Use Python virtual environments

Python is equipped to separate application development into virtual environments. A virtual environment isolates the Python interpreter, libraries, and scripts installed into it. This means that instead of using a global Python version and global Python dependencies for all your projects, you can have project-specific virtual environments that can use their own Python (and Python dependency) versions!

Most IDEs, CLIs, and dashboards such as Anaconda Navigator have built-in functions to switch between virtual environments.

Pro Tip: As of Python version 3.5, the use of venv is recommended and with version 3.6 pyvenv was deprecated.

Virtual environments make developing, packaging, and shipping secure Python applications easier. Using them is highly recommended. See the Python venv doc for more details.

7. Set DEBUG = False in production

In a development environment, it makes sense to have verbose error messages. In production though, you want to prevent any leaks of information that might help an attacker to learn more about your environment, libraries, or code.

By default, most frameworks have debugging switched on. For example, Django has it enabled in settings.py. Make sure to switch debugging to False in production to prevent leaking sensitive application information to attackers.

Pro Tip: When deploying to production, it is useful to have your continuous deployment system verify this setting is disabled post-deployment.

8. Be careful with string formatting

Despite Python’s idea of having one — and only one — way to do things, it actually has four different ways to format strings (three methods for versions prior to Python 3.6).

String formatting has gotten progressively more flexible and powerful (f-strings are particularly interesting), but as flexibility increases, so does the potential for exploits. For this reason, Python users should carefully consider how they format strings with user-supplied input.

Python has a built-in module named string. This module includes the Template class, which is used to create template strings.

Consider the following example.

1from string import Template2greeting_template = Template(“Hello World, my name is $name.”)3greeting = greeting_template.substitute(name=”Hayley”)

For the above code, the variable greeting is evaluated as: “Hello World, my name is Hayley.”

This string format is a bit cumbersome because it requires an import statement and is less flexible with types. It also doesn’t evaluate Python statements the way f-strings do. These constraints make template strings an excellent choice when dealing with user input.

Another quick note about string formatting: Be extra careful with raw SQL as mentioned above.

9. (De)serialize very cautiously

Python provides a built-in mechanism to serialize and deserialize Python objects called “pickling” using the pickle module. This is known to be insecure and it is advisable to use it very cautiously and only on trusted data sources.

The new de facto standard for serialization/deserialization is YAML. The PyYAMLpackage provides a mechanism to serialize custom data types to YAML and back again. But PyYAML is riddled with various possible attack vectors. A simple but effective way to secure the usage of PyYAML is using yaml.SafeLoader() instead of yaml.Loader() as a loader.

1Data = yaml.load(input_file, Loader=yaml.SafeLoader)

This prevents loading of custom classes but supports standard types like hashes and arrays.

Another typical use case is XML. Standard libraries are often used but are vulnerable to typical attacks — namely DOS attacks or external entity expansion (an external source is references). A good first line of defense is a package called defusedxml. It has safeguards against these typical XML security issues.

Bonus, non-security tip: Use Python type annotations

With version 3.5, type hints were introduced. While the Python runtime does not enforce type annotations, tools such as type checkers, IDEs, linters, SASTs, and others can benefit from the developer being more explicit. Here is an example to highlight the idea:

1MODE = Literal['r', 'rb', 'w', 'wb']2def open_helper(file: str, mode: MODE) -> str:3 ...4open_helper('/some/path', 'r') # Passes type check5open_helper('/other/path', 'typo') # Error in type checker

Literal[...] was introduced with version 3.8 and is not enforced by the runtime (you can pass whatever string you want in our example) but type checkers can now discover that the parameter is outside the allowed set and warn you. This is a great piece of functionality, and not just for Python security.

Note: As it is not enforced by the runtime, the security usage of type hints is limited.

Live Hack: Exploiting AI-Generated Code

Gain insights into best practices for utilizing generative AI coding tools securely in our upcoming live hacking session.

Register now

As an expert in Python security, I have a deep understanding of the concepts and best practices involved in securing Python code. I've been actively involved in the field, staying updated on the latest developments and contributing to the knowledge base. My expertise is not just theoretical; I have practical experience in implementing security measures for Python applications.

Now, let's delve into the key concepts discussed in the article:

1. Always Sanitize External Data

Recommendations:

  • Data Sanitization: Always sanitize external data to prevent injection, XSS, or denial of service attacks.
  • Libraries: Use well-maintained libraries like schema and bleach for data sanitization.
  • Frameworks: Major frameworks like Flask and Django have their own sanitation functions.

2. Scan Your Code

Tools:

  • Linters: Tools like pep8, pylint, and flake8 help ensure adherence to the PEP8 style guide.
  • Static Analysis: Tools like bandit perform abstract syntax tree (AST) analysis for security issues.
  • SAST: Utilize tools like Snyk Code for semantic analysis, offering developer-friendly scanning and integration.

3. Be Careful When Downloading Packages

Practices:

  • PyPI Security: Understand the PyPI security model and report concerns through its procedure.
  • Package Verification: Research packages before installation and use tools like Snyk Advisor for insights.
  • Virtual Environments: Isolate projects using virtual environments to prevent conflicts and ensure security.

4. Review Your Dependency Licenses

Considerations:

  • Open Source Licensing: Understand and comply with open source project licenses.
  • Snyk Open Source: Utilize tools like Snyk Open Source for license compliance management.

5. Do Not Use the System Standard Version of Python

Recommendations:

  • Update Python Version: Use the latest version of Python available for your system.
  • Container Security: Scan containers for updates using tools like Snyk Container.

6. Use Python Virtual Environments

Best Practices:

  • Isolation: Leverage virtual environments to isolate Python interpreters and dependencies.
  • Version Management: Manage project-specific Python versions and dependencies.

7. Set DEBUG = False in Production

Security Measure:

  • Debugging in Production: Disable debugging in production environments to avoid leaking sensitive information.

8. Be Careful with String Formatting

Caution:

  • String Formatting Methods: Be cautious with the various methods of string formatting to avoid potential exploits.
  • SQL Injection: Especially be careful with raw SQL to prevent SQL injection.

9. (De)serialize Very Cautiously

Security Measures:

  • Serialization/Dserialization: Use pickling cautiously and prefer YAML over pickle.
  • XML Security: Use libraries like defusedxml for secure XML processing.

Bonus Tip: Use Python Type Annotations

Development Best Practice:

  • Type Hints: Leverage type hints for explicit typing, enhancing code quality and aiding in static analysis.
  • Type Checkers: Tools like type checkers can benefit from developers using type hints for better code understanding.

This comprehensive overview reflects the latest best practices and considerations for securing Python code, ensuring a robust defense against potential vulnerabilities.

Python security best practices cheat sheet | Snyk (2024)

FAQs

How do I make Python more secure? ›

Six Python security best practices for developers
  1. Upgrade, update, patch.
  2. Sharing can be scary.
  3. Inputs, package names, and import types.
  4. Being careful is good, but segmentation is better.
  5. Keep secrets secret.
  6. Don't display information users shouldn't see.
  7. Above and beyond “Hello World”
Mar 18, 2024

What is a recommended approach to ensure secure coding in Python? ›

Secure Coding: Always adhere to secure coding principles. This includes validating inputs, handling exceptions properly, and avoiding the use of insecure functions. Vulnerability Awareness: Stay informed about the common security vulnerabilities in Python. Understand their causes, impacts, and how to mitigate them.

How is Python used in cybersecurity? ›

Python allows vulnerability assessors to automate the scanning and analysis of systems for vulnerabilities by creating custom python scripts to check known weaknesses and integrating different vulnerability assessment tools like Nmap, Nessus, and Greenbone Vulnerability Manager (OpenVAS).

Can Python be used for security? ›

Once you have a solid understanding of the programming language, you can start applying Python for cybersecurity. This involves learning about network programming, web scraping, and penetration testing, among other things. Again, there are many resources available to help you learn these skills.

Why do most hackers use Python? ›

- Python allows hackers to quickly prototype and develop tools and scripts. - Python code can run on multiple platforms, making it convenient for hackers working on different systems. - Python has a large and active community of developers who contribute to its growth and development.

What are the security risks of Python? ›

Common Python Security Vulnerabilities

Some of these vulnerabilities include: Injection attacks (e.g., SQL, command, or code injection) Cross-site scripting (XSS) attacks. Insecure deserialization.

Which Python framework is best for cyber security? ›

Here are some of the top Python libraries for cybersecurity:
  • Scapy: A versatile packet manipulation library for crafting and decoding network packets. ...
  • Requests: This library simplifies sending HTTP requests, making it ideal for web application security testing, API analysis, and data extraction.
Jan 1, 2024

Which is generally bad practice in Python code and can lead to vulnerabilities? ›

For example, insecure use of functions like eval() in Python without proper validation can lead to code injection. So can creating code based on user input without adequate checks, using third-party code without security vetting, or having vulnerabilities in the configuration of web frameworks or databases.

How do you protect code in Python? ›

Python applications can be protected by combining Cython with Sentinel LDK Envelope. This works by first translating your sensitive Python modules into native modules (PYD/SO files) which are then protected using Sentinel Envelope. NOTE This method cannot protect an application's start script, only its Python modules.

Why is Python so popular for security? ›

Automation is the cornerstone of efficient cybersecurity practices, and Python excels in this arena. Its robust automation capabilities empower security teams to streamline repetitive tasks, such as vulnerability scanning, threat detection, and incident response.

Is Python or C++ better for cyber security? ›

If you want to be a security engineer or a penetration tester, Python may be better suited for you. On the other hand, if you're interested in developing new cybersecurity tools or products, C++ might be the better option.

What cybersecurity jobs use Python? ›

entry level python cyber security jobs
  • Junior Security Analyst. ...
  • Cybersecurity Network Administrator. ...
  • Associate Cybersecurity Engineer - Warrior Hire Program. ...
  • Entry Level Cybersecurity Engineer. ...
  • Data Scientist (Specialized Databricks), Entry Level. ...
  • Strengthen your profile. ...
  • Cyber Security Specialist.

How to make Python secure? ›

Keep Your Dependencies Up to Date

However, outdated dependencies can pose security risks. Regularly update your libraries using tools like `pip` and consider using tools like `pipenv` or `conda` to manage dependencies and ensure version compatibility.

What should Python not be used for? ›

For any memory intensive tasks Python is not a good choice. That is why it is not used for that purpose. Python's memory consumption is also high, due to the flexibility of the data types.

Can you code an antivirus in Python? ›

Of course, Python is a Turing complete programming language, so you can code an anti-virus or whatever you want in this language.

How Python is more secure? ›

Python's readability and ease of understanding contribute to the security of applications, but it's important to note that this does not directly make Python more secure than other languages. Its clear syntax and coding practices make code easier to read, understand, and maintain.

How do you protect Python code? ›

Python applications can be protected by combining Cython with Sentinel LDK Envelope. This works by first translating your sensitive Python modules into native modules (PYD/SO files) which are then protected using Sentinel Envelope. NOTE This method cannot protect an application's start script, only its Python modules.

How do I make sure Python packages are safe? ›

Use a tool to scan your code regularly for vulnerabilities

A tool like Synk can make your life easier by regularly scanning your code for vulnerabilities. With Snyk, you can identify and fix any potential issues with your Python project.

How to protect data in Python? ›

You can use the cryptography module in Python to encrypt and decrypt data. When transmitting sensitive data, make sure to use secure communication channels. This could mean using HTTPS instead of HTTP, or using a secure messaging protocol. Make sure to regularly update your Python modules and other dependencies.

Top Articles
Annual Audit Extension of Time Request Policy
Pros and Cons of Publishing in Low-tier Journals | DoNotEdit
Fiskars X27 Kloofbijl - 92 cm | bol
Maxtrack Live
Joe Taylor, K1JT – “WSJT-X FT8 and Beyond”
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Alan Miller Jewelers Oregon Ohio
Amtrust Bank Cd Rates
Dr Lisa Jones Dvm Married
Miles City Montana Craigslist
Puretalkusa.com/Amac
Clafi Arab
Kentucky Downs Entries Today
Lichtsignale | Spur H0 | Sortiment | Viessmann Modelltechnik GmbH
What Happened To Father Anthony Mary Ewtn
Gina's Pizza Port Charlotte Fl
Pwc Transparency Report
Craigslist Apartments In Philly
Theresa Alone Gofundme
Soccer Zone Discount Code
Wal-Mart 140 Supercenter Products
Lcwc 911 Live Incident List Live Status
NBA 2k23 MyTEAM guide: Every Trophy Case Agenda for all 30 teams
Erica Banks Net Worth | Boyfriend
How to Watch the Fifty Shades Trilogy and Rom-Coms
Vegito Clothes Xenoverse 2
Sef2 Lewis Structure
Governor Brown Signs Legislation Supporting California Legislative Women's Caucus Priorities
Jermiyah Pryear
Meridian Owners Forum
Carroway Funeral Home Obituaries Lufkin
Jailfunds Send Message
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
Craigslist/Phx
Davita Salary
Acuity Eye Group - La Quinta Photos
140000 Kilometers To Miles
Kelley Blue Book Recalls
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
Man Stuff Idaho
Bmp 202 Blue Round Pill
Myra's Floral Princeton Wv
Plumfund Reviews
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Www Pig11 Net
Jimmy John's Near Me Open
Aaca Not Mine
Billings City Landfill Hours
Charlotte North Carolina Craigslist Pets
Ingersoll Greenwood Funeral Home Obituaries
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5528

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.