Skip to main content

Static Analysis Post-Processor for processing taint analysis results.

Project description

lint tests pyre

SAPP

SAPP stands for Static Analysis Post Processor. SAPP takes the raw results of Pysa and makes them explorable both through a command line interface and a web UI.

Installation

To run SAPP, you will need Python 3.7 or later. SAPP can be installed through PyPI with pip install fb-sapp.

Getting Started

This guide assumes that you have results from a Pysa run saved in a ~/example directory. If you are new to Pysa, you can follow this tutorial to get started.

Processing the Results

The postprocessing will translate the raw output containing models for every analyzed function into a format that is more suitable for exploration.

[~/example]$ sapp --database-name sapp.db analyze taint-output.json

After the results have been processed we can now explore them through the UI and a command line interface. We will briefly look at both of those methods here.

Web Interface

Start the web interface with

[~/example]$ sapp --database-name sapp.db server --source-directory=<WHERE YOUR CODE LIVES>

and visit http://localhost:5000 in your browser (note: the URL displayed in the code output currently will not work). You will be presented with a list of issues that provide access to example traces.

Command Line Interface

The same information can be accessed through the command line interface:

[~/example]$ sapp --database-name sapp.db explore

This will launch a custom IPython interface that's connected to the sqlite file. In this mode, you can dig into the issues that Pyre surfaces. Following is an example of how to use the various commands.

Start out by listing all known issues:

==========================================================
Interactive issue exploration. Type 'help' for help.
==========================================================

[ run 1 ]
>>> issues
Issue 1
    Code: 5001
 Message: Possible shell injection Data from [UserControlled] source(s) may reach [RemoteCodeExecution] sink(s)
Callable: source.convert
 Sources: input
   Sinks: os.system
Location: source.py:9|22|32
Found 1 issues with run_id 1.

As expected, we have 1 issue. To select it:

[ run 1 ]
>>> issue 1
Set issue to 1.

Issue 1
    Code: 5001
 Message: Possible shell injection Data from [UserControlled] source(s) may reach [RemoteCodeExecution] sink(s)
Callable: source.convert
 Sources: input
   Sinks: os.system
Location: source.py:9|22|32

View how the data flows from source to sink:

[ run 1 > issue 1 > source.convert ]
>>> trace
     # ⎇  [callable]       [port]      [location]
     1    leaf             source      source.py:8|17|22
 --> 2    source.convert   root        source.py:9|22|32
     3    source.get_image formal(url) source.py:9|22|32
     4    leaf             sink        source.py:5|21|28

Move to the next callable:

[ run 1 > issue 1 > source.convert ]
>>> n
     # ⎇  [callable]       [port]      [location]
     1    leaf             source      source.py:8|17|22
     2    source.convert   root        source.py:9|22|32
 --> 3    source.get_image formal(url) source.py:9|22|32
     4    leaf             sink        source.py:5|21|28

Show the source code at that callable:

[ run 1 > issue 1 > source.get_image ]
>>> list
In source.convert [source.py:9|22|32]
     4      command = "wget -q https:{}".format(url)
     5      return os.system(command)
     6
     7  def convert() -> None:
     8      image_link = input("image link: ")
 --> 9      image = get_image(image_link)
                              ^^^^^^^^^^

Move to the next callable and show source code:

[ run 1 > issue 1 > source.get_image ]
>>> n
     # ⎇  [callable]       [port]      [location]
     1    leaf             source      source.py:8|17|22
     2    source.convert   root        source.py:9|22|32
     3    source.get_image formal(url) source.py:9|22|32
 --> 4    leaf             sink        source.py:5|21|28

[ run 1 > issue 1 > leaf ]
>>> list
In source.get_image [source.py:5|21|28]
     1  import os
     2
     3  def get_image(url: str) -> int:
     4      command = "wget -q https:{}".format(url)
 --> 5      return os.system(command)
                             ^^^^^^^
     6
     7  def convert() -> None:
     8      image_link = input("image link: ")
     9      image = get_image(image_link)

Jump to the first callable and show source code:

[ run 1 > issue 1 > leaf ]
>>> jump 1
     # ⎇  [callable]       [port]      [location]
 --> 1    leaf             source      source.py:8|17|22
     2    source.convert   root        source.py:9|22|32
     3    source.get_image formal(url) source.py:9|22|32
     4    leaf             sink        source.py:5|21|28

[ run 1 > issue 1 > leaf ]
>>> list
In source.convert [source.py:8|17|22]
     3  def get_image(url: str) -> int:
     4      command = "wget -q https:{}".format(url)
     5      return os.system(command)
     6
     7  def convert() -> None:
 --> 8      image_link = input("image link: ")
                         ^^^^^
     9      image = get_image(image_link)

You can refer to the help command to get more information about available commands in the command line interface.

Terminology

A single SAPP database can keep track of more than just a single run. This opens up the possibility of reasoning about newly introduced issues in a codebase.

Every invocation of

[~/example]$ sapp --database-name sapp.db analyze taint-output.json

will add a single run to the database. An issue can exist over multiple runs (we typically call the issue in a single run an instance). You can select a run from the web UI and look at all the instances of that run. You can also chose to only show the instances of issues that are newly introduced in this run in the filter menu.

Each instance consists of a data flow from a particular source kind (e.g. user controlled input) into a callable (i.e. a function or method), and a data flow from that callable into a particular sink kind (e.g. RCE).

Note: the data can come from different sources of the same kind and flow into different sinks of the same kind. The traces view of a single instance represents a multitude of traces, not just a single trace.

Filters

SAPP filters are used to include/exclude which issues are shown to you by the issue properties you choose. Filters are useful to remove noise from the output from your static analysis tool, so you can focus on the particular properties of issues you care about.

SAPP functionality can be accessed through the web interface or through a subcommand of sapp filter.

File Format

A filter is required to have a name and at least one other key, excluding description. Filters can be stored as JSON in the following format:

{
    "name": "Name of filter",
    "description": "Description for the filter",
    "features": [
        {
            "mode": "all of",
            "features": [
                "via:feature1",
                "feature2",
            ]
        },
        {
            "mode": "any of",
            "features": [
                "always-via:feature3",
            ]
        },
        {
            "mode": "none of",
            "features": [
                "type:feature5",
            ]
        }
    ],
    "codes": [
        5005
    ],
    "paths": [
        "filename.py"
    ],
    "callables": [
        "main.function_name",
    ],
    "traceLengthFromSources": [
        0,
        3
    ],
    "traceLengthToSinks": [
        0,
        5
    ],
    "is_new_issue": false
}

You can find some example filters to reference in the pyre-check repo

Importing filters

You can import a filter from a file by running:

[~/example]$ sapp --database-name sapp.db filter import filter-filename.json

You can also import all filters within a directory by running:

[~/example]$ sapp --database-name sapp.db filter import path/to/list_of_filters

Exporting filters

You can view a filter in a SAPP DB by running:

[~/example]$ sapp --database-name sapp.db filter export "filter name"

You can export a filter from a SAPP DB to a file by running:

[~/example]$ sapp --database-name sapp.db filter export "filter name" --output-path /path/to/filename.json

Deleting filters

You can delete filters by name with:

[~/example]$ sapp --database-name sapp.db filter delete "filter name 1" "filter name 2" "filter name 3"

Filtering list of issues

You can apply a filter to a list of issues by run number. For example, the following command will show you a list of issues after applying example-filter to run 1:

[~/example]$ sapp --database-name sapp.db filter issues 1 example-filter.json

You can also apply a list of filters to a single list of issues by run number. SAPP will apply each filter individually from the directory you specify to the list of issues and merge results into a single list of issues to show you. For example, the following command will show you a list of issues after applying every filter in list_of_filters to run 1:

[~/example]$ sapp --database-name sapp.db filter issues 1 path/to/list_of_filters

SARIF Output

You can get the output of a filtered run in SARIF by first storing warning codes information from the static analysis tool in SAPP:

sapp --database-name sapp.db update warning-codes taint-metadata.json

Then running sapp filter issues with --output-format=sarif:

sapp --database-name sapp.db filter issues 1 path/to/list_of_filters --output-format sarif

Development Environment Setup

Start by cloning the repo and setting up a virtual environment:

$ git clone git@github.com:facebook/sapp.git && cd sapp
$ python3 -m venv ~/.venvs/sapp
$ source ~/.venvs/sapp/bin/activate
(sapp) $ pip3 install -r requirements.txt

Run the flask server in debug mode:

(sapp) $ python3 -m sapp.cli server --debug

Parse static analysis output and save to disk:

(sapp) $ python3 -m sapp.cli analyze taint-output.json

Installing dependencies for frontend:

(sapp) $ cd sapp/ui/frontend && npm install

To run SAPP with hot reloading of the Web UI, you need have the frontend and backend running simultaneously. In a production environment, the frontend application is compiled and served directly by the backend exposed on port 5000. But in a development environment, the frontend runs in port 3000 and the backend runs in port 5000. You can indicate to SAPP to run in the development environment with the debug flag

Run the flask server and react app in development mode:

(sapp) $ python3 -m sapp.cli server --debug
(sapp) $ cd sapp/ui/frontend && npm run-script start

Then visit http://localhost:3000

FAQ

Why is SAPP it's own project and not just part of Pysa?

Stay tuned for future announcements.

License

SAPP is licensed under the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fb-sapp-0.5.1.tar.gz (2.0 MB view details)

Uploaded Source

Built Distribution

fb_sapp-0.5.1-py3-none-any.whl (2.0 MB view details)

Uploaded Python 3

File details

Details for the file fb-sapp-0.5.1.tar.gz.

File metadata

  • Download URL: fb-sapp-0.5.1.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.3

File hashes

Hashes for fb-sapp-0.5.1.tar.gz
Algorithm Hash digest
SHA256 9e1ffd2a3e154fe49b564627f8bfb809e1717cc7178e0dd50ab55405654c4ac6
MD5 10bd85637e39d755bfb422d94ef0ec28
BLAKE2b-256 a78c1587eba916b1ee26c2fdd2b55399f3257db5dbbec4554fd79ee4c6d788af

See more details on using hashes here.

Provenance

File details

Details for the file fb_sapp-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: fb_sapp-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 2.0 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.3

File hashes

Hashes for fb_sapp-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9d62064300fa05f1615545b678cf223110afe5b9f5f8fa6ad7602c1e26d19795
MD5 c7abe9e1be072a561d924f655cbccdc6
BLAKE2b-256 95431cbe078f63541a3d880a4e58e1ae84041fecd93e7687f02484961c8991fb

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page