Skip to main content

Common Misconfigurations

  1. Using outdated packages with known CVEs
  2. Not scanning dependencies for vulnerabilities
  3. Ignoring security advisories
  4. Missing dependency pinning
  5. Not checking package integrity

Vulnerable Example

Secure Solution

Key Commands for Updating

Here are the essential commands for securely managing your Python dependencies.

1. Audit for Vulnerabilities

Use these tools (listed in requirements-dev.txt) to scan your project.

2. Generate Pinned Requirements (The Secure Way)

This is the most secure method. It uses pip-tools to generate a locked requirements.txt from a simple requirements.in file. Step 1: Create a requirements.in file with your top-level dependencies:
Step 2: Install pip-tools (usually from requirements-dev.txt):
Step 3: Compile the file, generating hashes. This creates the secure requirements.txt file.
To update all packages later, just re-run this command.

3. Install from Secure Requirements

This command installs the exact versions from your generated file. If your .pip.conf is set up, it will fail if any hashes don’t match.

4. Sync Your Environment

A better way to install is using pip-sync, which comes with pip-tools. It installs only what’s in requirements.txt and removes anything else, perfectly syncing your environment.

5. Check for Outdated Packages

This command lists any packages that have newer versions available.

Best Practices

  • Use tools like Safety, pip-audit, or Snyk.
  • Pin exact versions in production.
  • Use pip-tools to generate pinned requirements.txt files with hashes.
  • Regularly update dependencies by re-compiling your requirements.txt.
  • Use virtual environments.