Security Guide For Python Developers
Security Guide For Python Developers
Python Developers
I
N Overview..................................................................1
D
How Python Packages Impact Project Security........ 5
E
Security Risks..........................................................9
X
e w
Over v i
A security footprint is the sum of a digital item’s
security requirements. For example, a program’s
security footprint would include all of the points
where action is necessary to prevent data breaches
or viruses.
A security footprint is the sum of a digital item’s security requirements. For example, a program’s
security footprint would include all of the points where action is necessary to prevent data breaches or
viruses.
Programming languages have security footprints, too. These footprints cover the points in which the
language is vulnerable to malicious actors. Any program written in a given programming language must
be designed with that language’s security footprint in mind in addition to its own issues.
Python is no different. The language’s security footprint includes three major points of risk:
For instance, Python has several vulnerabilities that occur when a program doesn’t sanitize user inputs
properly. These kinds of vulnerabilities allow users to force the program to take actions it shouldn’t
permit. However, you can easily fix these issues by making sure you’re aware of known vulnerabilities
within Python and coding appropriate preventative measures.
Direct Dependencies
As an open-source language, there are millions of packages available within Python. These packages can
be used as direct dependencies, or pieces of code that are essential for a program to run.
Since most programs use direct dependencies to perform basic operations, any problem within that
package exists within the program, too. If a dependency’s code includes a known vulnerability, the
program running that dependency is at risk.
Indirect Dependencies
Packages can also become indirect dependencies. A package can have another package as a direct
dependency. A program that uses the first package will automatically work with the second package
even though it’s not listed in the program’s code. The program is indirectly dependent on the second
package to function.
Just like direct dependencies, an indirect dependency can introduce security risks. However, indirect
dependencies are harder to spot. Python programmers need to be aware of not only the packages they
use in their programs but also the dependencies of those packages. For large programs, this can quickly
balloon into dozens — or even hundreds — of packages to monitor. This line of linked packages is known
as the software supply chain. An issue with any one part of the chain causes problems for every program
and package downstream.
These injections are particularly easy in Python because of how the language handles user commands.
Unless the program is written to “sanitize” user inputs, anything the user types into the program is
directly passed to the command line. If the user chooses to input a string that the command line can
read, it will then execute that code.
Sanitizing user inputs solves that problem. Sanitization is the process of ensuring that user input is only
ever read as a string, not as code. It takes a minimal amount of extra effort, and it can significantly
reduce your security risks.
Encoded Scripting
Python doesn’t always handle Unicode characters the way you would expect.
Hackers can use Unicode-encoded scripting to present strings that look like they
accomplish one task while actually accomplishing another. This allows hackers to
write code within a package that looks completely safe while actually embedding
dangerous commands within the scripting.
The easiest way to prevent this issue is to only work with trusted packages and dependencies. Avoid
working with unknown packages and developers. Combined with input sanitization, this will ensure
malicious actors can’t run code within your systems that you don’t approve.
There are two main ways XSS attacks occur. Either the hacker embeds the malicious code through a
web request or similar untrusted source, or the script is hidden in dynamic content the website has not
vetted.
For instance, advertisements were a significant source of XSS attacks for a long time. Websites that
didn’t carefully check the ads they showed users often presented dynamic ads that included XSS
programs. Web app developers need to be cautious both with how they handle requests of any kind as
well as the third-party code they allow to be displayed through their Python applications.
Python programs can be written with TLS certification turned off. While this saves a small amount of
computing power, it also puts the program at significant risk of attacks. Without TLS certificates, the
program can’t verify whether other sites and servers are trustworthy. Furthermore, the data they send
will not be encrypted, potentially permitting “man-in-the-middle” attacks if a hacker intercepts data
between the source and its destination.
Hardcoded Secrets
Once a hacker breaks into a system, their goal is usually to find sensitive information
that they can use to further their goals. A common target of many hackers is “secret”
data, including things like account names, passwords, or confidential paths and file
names. This information can be used to accomplish further attacks or steal funds
and accounts from their rightful owners.
That’s why hardcoding secrets in Python is such a security weakness. “Hardcoding’’ is the act of leaving
secret information unencrypted in the program’s code. Anyone who looks at the script can simply scroll
and find the sensitive data in plaintext.
2. IP address:
This package is used to help programs manipulate IPv4 and IPv6 addresses effectively. However,
the module also accepts leading zeros on IPv4 addresses. This permits hackers to bluff the IP
address checks the package is designed to perform. As a result, any program with an IP address
as a direct or indirect dependency is at risk of bluffed IP addresses and Denial of Service (DOS)
attacks. No version of ip address is free from this issue.
3. Cryptography:
The cryptography package offers Python developers different primitives and cryptographic
recipes they can use within their program. However, the fundamental structure of the package
makes it vulnerable to memoryview attacks in which users can overwrite the contents. While
this is not as great of a risk as other packages, cryptography’s popularity may make it a target in
future attacks. No version of the package is free from the issue.
4. Pillow:
Pillow is an imaging library that allows a project to use and implement a wide variety of image
extensions. Like urllib3, the package is kept up to date, but older versions with critical security
flaws are still in use. Versions prior to the 8.1.1 release are vulnerable to DOS attacks through
improperly monitored image sizes.
5. PyYAML:
The PyYAML package allows a program to parse and emit YAML, making it easier for humans to
understand data produced by Python. Unfortunately, the incredibly popular PyYAML package
also permits hackers to force programs to execute malicious code and bypass access controls
with ease. Versions after 6.0 are safe, but older versions are still in use. Developers need to make
sure they keep their PyYAML version updated to keep their apps safe.
7. Pygments:
This package is a syntax-highlighting package that makes it easier to spot when a Python
program is being written correctly. However, pygments was found to use regular expressions
within its programming. This permits DOS attacks when hackers overload the regular
expression and force the package to stop working. Pygment versions 2.7.4 and later do not
include this error.
8. Requests:
This HTTP client had several significant security errors in earlier versions. Packages prior to
2.20.0 made it easy for hackers to access credentials in several ways. Requests sent HTTP
authorizations to HTTP URLs, permitting sniffers to spot credentials in the request. Similarly,
hackers could read the Proxy-Authorization and Authorization headers to obtain sensitive
information like netrc passwords. Updating to a more recent version of the requests package is
essential for security.
9. RSA:
This package supports RSA implementation, offering extra encryption to Python developers.
However, versions before 4.7 did not handle ciphertexts securely. This gives hackers a path to
decrypting the information rsa is intended to protect. All programs that include rsa as a direct
or indirect dependency should immediately upgrade to the most recent version to remove this
vulnerability.
10. Django:
This package offers a high-level Python Web framework that developers can use to support their
web apps and write programs more quickly. The issue with django is that versions prior to 3.1.13
are vulnerable to SQL attacks. The package doesn’t handle GIS functions correctly. Hackers can
use this to force programs using django to perform malicious actions. Updating django to the
most recent version is enough to resolve this issue.
• Reliance on unsecured base or parent images: Containers are frequently created using “base”
images that are blank but include some basic formatting or “parent” images that come with
many already-included dependencies. If you use a base or parent image that has security
flaws, those issues will immediately affect your program as well.
• Lack of container visibility: Containers are designed to be dynamic and allow developers to
adjust to changing needs over time. However, that flexibility can also make it easy to lose track
of what’s actually included within a container or image. If you don’t know what’s in all of your
containers, you can’t successfully manage their security.
• Broad container communication: Many containers are intended to communicate with other
containers. This communication is critical to their function, but it needs to include careful
security filters. Without appropriate filtering, any hacker that can access one container will
also be able to enter any other container you use.
This doesn’t mean you should avoid using containers. These tools are too useful to ignore. However, it
does mean that you need to implement safety and security measures when you use containers,
Otherwise, you risk serious breaches.
Regularly check the Python hub to make sure you’re using the most recent stable
release. This ensures that anything you write has the most protection possible
against malicious actors. You should do the same for all of your dependencies, too,
since they are one of the most vulnerable points in Python programs’ security
footprints.
You can avoid these risky packages by working with trusted alternatives. Look for
packages that have been used and reviewed by many other Python developers. These
are more likely to work well. More importantly, any flaws are more likely to be
discovered and fixed because of the size of the user-base. That makes them a safer
choice for security-minded developers.
The Python hub and community are excellent at spreading the news about new
vulnerabilities. Check up on the state of the language regularly by visiting the Python
hub site. Major new vulnerabilities will usually be announced somewhere on the front
page, so everyone knows they need to take action.
There are several Python libraries you can use that will handle data sanitization for
you. For example, bleach is an HTML-sanitizing library that works off of a white-list,
automatically stripping markups and attributes from user input. You can also work
with frameworks like django, which come with native sanitization tools.
• QA code analysis
• Governance and lifecycle monitoring
• Application vulnerability detection
• Local security code scanning
With Kiuwan, you don’t have to guess about things like known vulnerabilities or flaws in your direct and
indirect dependencies. Kiuwan will monitor your programs, identify risks, suggest mitigation solutions,
and help you develop more secure programs with less stress.
It’s time to stop worrying about security flaws and start working with a partner that will support your
DevSecOps needs from development through production. If you’re ready to learn more about how Kiuwan
can support secure Python development within your organization, you can schedule your demo today.
GET IN TOUCH: