Package: fb-sapp
SAPPSAPP stands for Static Analysis Post Processor. SAPP takes the raw results ofPysaandMariana Trench, and makes them explorable both through a command-line interface and a web UI.SAPP is also available onGitHub Marketplace as a GitHub ActionInstallationTo run SAPP, you will needPython 3.8 or later. SAPP can be installed throughPyPIwithpip install fb-sapp.Getting StartedThis guide assumes that you have results from a Pysa run saved in an~/exampledirectory. If you are new to Pysa, you can followthis tutorialto get started.Processing the ResultsThe postprocessing will translate the raw output containing models for every analyzed function into a format that is more suitable for exploration.[~/example]$sapp--database-namesapp.dbanalyzetaint-output.jsonAfter 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 InterfaceStart the web interface with[~/example]$sapp--database-namesapp.dbserver--source-directory=<WHEREYOURCODELIVES>and visithttp://localhost:13337in 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 InterfaceThe same information can be accessed through the command-line interface:[~/example]$sapp--database-namesapp.dbexploreThis will launch a customIPythoninterface that is 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 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|32View 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|28Move 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|28Show 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)HelpYou can refer to thehelpcommand to get more information about available commands in the command-line interface.$ sapp --help
Usage: sapp [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --verbosity LVL             Either CRITICAL, ERROR, WARNING, INFO or
                                  DEBUG
  -r, --repository DIRECTORY      Root of the repository (regardless of the
                                  directory analyzed)
  --database-name, --dbname FILE
  --database-engine, --database [sqlite|memory]
                                  database engine to use
  --tool [pysa|mariana-trench]    tool the data is coming from
  -h, --help                      Show this message and exit.

Commands:
  analyze  parse static analysis output and save to disk
  explore  interactive exploration of issues
  filter
  lint     Output DB models in a lint-friendly format
  server   backend flask server for exploration of issues
  updateTerminologyA single SAPP database can keep track of more than just a single run. This opens up the possibility of reasoning aboutnewly introduced issuesin a codebase.Every invocation of[~/example]$sapp--database-namesapp.dbanalyzetaint-output.jsonwill add a singlerunto the database. Anissuecan exist over multiple runs (we typically call the issue in a single run aninstance). You can select a run from the web UI and look at all the instances of that run. You can also choose to only show the instances of issues that are newly introduced in this run in the filter menu.Each instance consists of adata flowfrom a particular source kind (e.g. user-controlled input) into acallable(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.FiltersSAPP 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 a subcommand ofsapp filter.File FormatA filter is required to have anameand at least one other key, excludingdescription. 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 thepyre-check repoImporting filtersYou can import a filter from a file by running:[~/example]$sapp--database-namesapp.dbfilterimportfilter-filename.jsonYou can also import all filters within a directory by running:[~/example]$sapp--database-namesapp.dbfilterimportpath/to/list_of_filtersExporting filtersYou 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.jsonDeleting filtersYou can delete filters by name with:[~/example]$sapp--database-namesapp.dbfilterdelete"filter name 1""filter name 2""filter name 3"Filtering list of issuesYou 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 applyingexample-filterto run1:[~/example]$sapp--database-namesapp.dbfilterissues1example-filter.jsonYou 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 inlist_of_filtersto run1:[~/example]$sapp--database-namesapp.dbfilterissues1path/to/list_of_filtersSARIF OutputYou can get the output of a filtered run inSARIFby first storing warning codes information from the static analysis tool in SAPP:sapp--database-namesapp.dbupdatewarning-codestaint-metadata.jsonThen runningsapp filter issueswith--output-format=sarif:sapp--database-namesapp.dbfilterissues1path/to/list_of_filters--output-formatsarifDevelopment Environment SetupStart by cloning the repo and setting up a virtual environment:$gitclonegit@github.com:facebook/sapp.git&&cdsapp
$python3-mvenv~/.venvs/sapp
$source~/.venvs/sapp/bin/activate(sapp)$pip3install-rrequirements.txtRun the flask server in debug mode:(sapp)$python3-msapp.cliserver--debugParse static analysis output and save to disk:(sapp)$python3-msapp.clianalyzetaint-output.jsonInstalling dependencies for frontend:(sapp)$cdsapp/ui/frontend&&npminstallTo 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 port13337. But in a development environment, the frontend runs in port3000by default if thePORTenvironment variable is not set and the backend runs in port13337. You can indicate to SAPP to run in the development environment with thedebugflag.Run the flask server and react app in development mode:(sapp)$python3-msapp.cliserver--debug(sapp)$cdsapp/ui/frontend&&npmrun-scriptstartThen visithttp://localhost:3000(orhttp://<HOST>:<PORT>if you have set theHOSTand/orPORTenvironment variable).LicenseSAPP is licensed under the MIT license.