Python

This article shows you how to analyze your applications written in Python using NG SAST.

Prerequisites

On the machine where you're planning to run ShiftLeft, ensure that you've:

  • Set up and authenticated with ShiftLeft
  • Set up the version of Python you used to write your application
  • Set up Python 3.8 (regardless of which version of Python your app uses -- this is required by ShiftLeft) and make sure python3.8 is available in your PATH
  • Set up the python3.8-venv package (Linux only)

Because you must have Python 3.8 installed on the machine where NG SAST runs, we recommend using a Python virtual environment tool (such as pyenv or virtualenv) to manage multiple versions of Python.

NG SAST only supports applications written using Python 3.8 or earlier.

SCA: To identify open-source vulnerabilities in Python applications, ShiftLeft CORE requires one of the following package formats: the Pipfile, requirements.txt, the requirements directory, poetry.lock or setup.py files.

CI/CD-based scans

The build agent must have Python 3.8** set up and support the creation of Python virtual environments.

Linux users: ShiftLeft requires a glibc-based operating system, such as Ubuntu or Debian. We strongly recommend using Ubuntu 20.04 if at all possible.

Considerations

ShiftLeft's Python analyzer attempts to gather as much information about your project as possible to achieve accuracy. It expects an environment set up close to the one you have for running your project. This means that the Python interpreter must find all of the project's dependencies set up in one of the directories of its module search path. The most straightforward way to do this is to create a virtual environment and install the project's dependencies in it.

ShiftLeft supports setups that do not use virtual environments as long as the Python interpreter can find the dependencies in its search path. You can specify additional directories to look for dependencies using the --extra-sys-paths flag.

Additionally, the Python analyzer goes through your project's files and modules in a similar way to the Python interpreter. This process is crucial for gathering necessary information and can be fragile for certain setups. If the analyzer cannot follow one of the imports in your project, the analysis will proceed. Still, the files related to the import may not be included in the resulting analysis. However, NG SAST may not detect security vulnerabilities related to these files. To receive a complete analysis possible, include the --strict-deps flag.

Analyzing your Python application

note

ShiftLeft offers a sample application that you can use to run and test NG SAST. It also includes a functioning configuration file to demonstrate how you can leverage GitHub Actions to automate code analysis whenever you open a new Pull Request (PR).

We also offer samples for GitLab integration, as well as configurations for Docker, Linux, macOS, and Windows.

Before running code analysis, please run pip install using the version of Python you used to write the application and make sure this is successful.

pip install -r requirements.txt

On macOS and in some Linux environments, pip and python may be using version 2 instead of version 3. If so, use pip3 and python3 instead.

To analyze your Python application:

sl analyze --app <name> --python [<path>]
ParameterDescription
--app <name>The name of the application to be analyzed
--pythonThe flag identifying the application as written in Python
<path>The path to the Python app to be analyzed

The analysis accepts additional parameters after a double hyphen --.

For example, the following CLI invocation ignores the dev-folder directory and all directories named experiments and adds a new entry to Python's module search path:

sl analyze --app <name> --python [<path>] -- --ignore-paths /path/to/dev-folder --ignore-dir-names experiments --extra-sys-paths /path/to/lib/python3.8/site-packages
Additional parameterDescription
--extra-sys-paths [<path>]Include additional module search paths in the analysis. Required for applications that aren't written using v3.8
--strict-depsRequires that all of the project's module paths can be followed for analysis to proceed
--ignore-pathsIgnores the specified paths from the analysis. Requires the full path, not a relative path, to be provided
--ignore-dir-namesExcludes all matching directories from the analysis. You must provide the exact directory name

Such parameters, if valid, are passed directly to the Python plugin.

To provide an additional module search path for the analysis:

sl analyze --app <name> --python [<path>] -- --extra-sys-paths [<path>]

To specify that the analysis should only continue if all the project's modules can be followed:

sl analyze --app <name> --python [<path>] -- --strict-deps

To ignore specific paths from the analysis:

sl analyze --app <name> --python [<path>] -- --ignore-paths [<ignore_path_1>] [<ignore_path_2>]

To ignore specific directory names from the analysis:

sl analyze --app <name> --python [<path>] -- --ignore-dir-names [<ignore_dir_name_1>] [<ignore_dir_name_2>]

To enable detailed logging of the analysis in case of troubleshooting:

sl analyze --app <name> --python [<path>] -- -l debug # append --verbose if providing logs to ShiftLeft for support

SCA

To identify open-source vulnerabilities, ShiftLeft CORE automatically searches for build manifests in the project path you provided when running sl analyze. However, depending on how your project repo is structured, you may need to provide --oss-project-dir <project-path> so that ShiftLeft CORE can identify where your dependencies are located.


See the CLI reference for additional sl analyze options.

Python Vulnerabilities

Tagging results with your branch name

To include the branch name in your NG SAST results, allowing you to distinguish one set of results from another, add the following to your invocation of ShiftLeft:

sl analyze --tag branch=`git symbolic-ref --short HEAD`

If you're working in a GitHub environment (e.g., GitHub Actions), you can also use --tag branch=${{ github.head_ref }} to populate your branch name.

If you don't provide a branch name, but ShiftLeft detects one available in your environment, it will use that name.

Troubleshooting

If you have any issues scanning your project, please see our general troubleshooting page, as well as our Python-specific suggestions that follow.

Package installation

Before running analysis on your Python application, make sure to run pip install:

pip install -r requirements.txt

Python versions

If you receive the following error when analyzing your Python application, please make sure that you have Python v3.8 installed (regardless of which version of Python you app uses):

Error: analyze: python3.8 is required but not found | issuer=/go/src/github.com/ShiftLeftSecurity/go-services/cmd/sl/main.go:1824
analyze: python3.8 is required but not found
Error: Process completed with exit code 1.

Module import errors

  • Check to see if running pip install for your application was successful.

  • If using a private registry, such as Sonatype or JFrog, ensure the .pypirc file contains the authentication token required to install all the dependencies.

  • Some applications might have a common directory referred to by multiple packages. To provide this additional module search path for the analysis:

    sl analyze --app <name> --python [<path>] -- --extra-sys-paths [<path>]
  • Some applications have development and test scripts that contain errors preventing security analysis. To omit specific paths from the analysis:

    sl analyze --app <name> --python [<path>] -- --ignore-paths [<ignore_path_1>] [<ignore_path_2>]

    The paths are separated by spaces.

  • To ignore specific directory by name from the analysis:

    sl analyze --app <name> --python [<path>] -- --ignore-dir-names [<ignore_dir_name_1>] [<ignore_dir_name_2>]
  • To enable detailed logging of the analysis in case of troubleshooting:

    sl analyze --verbose --app <name> --python [<path>] -- -l debug

    The detailed log file can then be found in the user's temporary directory.