Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Safe Use of Third-Party Libraries | Safe Coding Practices
Python Security Best Practices

bookSafe Use of Third-Party Libraries

When you use third-party libraries in your Python projects, you introduce both powerful capabilities and potential risks. Libraries can contain vulnerabilities, malicious code, or unexpected changes that threaten your application's security and stability. Attackers sometimes target widely used libraries, or even publish compromised versions of popular packages to public repositories. To mitigate these risks, you must adopt safe coding practices and carefully manage your dependencies.

12345
# Importing and using a library without version pinning import pandas_datareader as pdr data = pdr.get_data_yahoo("AAPL") print(data.head())
copy

This approach, where you import and use a library without specifying an exact version, can introduce significant security risks. If the library is updated, deprecated, or replaced with a malicious version on the package index, your code may break or behave unexpectedly. Even a minor update could introduce vulnerabilities or incompatibilities, especially if you automatically install the latest version each time you deploy or build your project.

12345678910111213
# Secure approach: specifying exact library versions in requirements.txt # requirements.txt pandas-datareader==0.10.0 # Before installing, check for known vulnerabilities using pip: # pip install safety # safety check -r requirements.txt # In your Python code, import as usual: import pandas_datareader as pdr data = pdr.get_data_yahoo("AAPL") print(data.head())
copy

By pinning exact library versions in your dependency files, you ensure that everyone using your project installs the same, tested, and reviewed code. This reduces the risk of unexpected changes or malicious updates. Regularly checking your dependencies for known vulnerabilities helps you stay ahead of security threats and maintain a consistent, safe environment for your application.

Note
Note

One of the most infamous security incidents involving compromised libraries was the "event-stream" attack in the Node.js ecosystem, where a widely used package was hijacked and used to steal cryptocurrency. In Python, the python3-dateutil typo-squatting incident saw a malicious package uploaded to PyPI, aiming to steal sensitive data from unsuspecting users. These examples show why careful dependency management is essential for security.

1. What is a risk of using unpinned library versions?

2. How can you reduce the risk when using third-party libraries?

question mark

What is a risk of using unpinned library versions?

Select the correct answer

question mark

How can you reduce the risk when using third-party libraries?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

What are some other best practices for managing Python dependencies securely?

How can I automate vulnerability checks for my dependencies?

Can you explain how to update pinned dependencies safely?

Awesome!

Completion rate improved to 5.56

bookSafe Use of Third-Party Libraries

Desliza para mostrar el menú

When you use third-party libraries in your Python projects, you introduce both powerful capabilities and potential risks. Libraries can contain vulnerabilities, malicious code, or unexpected changes that threaten your application's security and stability. Attackers sometimes target widely used libraries, or even publish compromised versions of popular packages to public repositories. To mitigate these risks, you must adopt safe coding practices and carefully manage your dependencies.

12345
# Importing and using a library without version pinning import pandas_datareader as pdr data = pdr.get_data_yahoo("AAPL") print(data.head())
copy

This approach, where you import and use a library without specifying an exact version, can introduce significant security risks. If the library is updated, deprecated, or replaced with a malicious version on the package index, your code may break or behave unexpectedly. Even a minor update could introduce vulnerabilities or incompatibilities, especially if you automatically install the latest version each time you deploy or build your project.

12345678910111213
# Secure approach: specifying exact library versions in requirements.txt # requirements.txt pandas-datareader==0.10.0 # Before installing, check for known vulnerabilities using pip: # pip install safety # safety check -r requirements.txt # In your Python code, import as usual: import pandas_datareader as pdr data = pdr.get_data_yahoo("AAPL") print(data.head())
copy

By pinning exact library versions in your dependency files, you ensure that everyone using your project installs the same, tested, and reviewed code. This reduces the risk of unexpected changes or malicious updates. Regularly checking your dependencies for known vulnerabilities helps you stay ahead of security threats and maintain a consistent, safe environment for your application.

Note
Note

One of the most infamous security incidents involving compromised libraries was the "event-stream" attack in the Node.js ecosystem, where a widely used package was hijacked and used to steal cryptocurrency. In Python, the python3-dateutil typo-squatting incident saw a malicious package uploaded to PyPI, aiming to steal sensitive data from unsuspecting users. These examples show why careful dependency management is essential for security.

1. What is a risk of using unpinned library versions?

2. How can you reduce the risk when using third-party libraries?

question mark

What is a risk of using unpinned library versions?

Select the correct answer

question mark

How can you reduce the risk when using third-party libraries?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 3
some-alt