CCyberwiki

Static application security testing

Static application security testing
TypeSecurity testing method
Also known asSAST, source code analysis, static code analysis
ExaminesSource code, or compiled code, without running it
Result formatSARIF 2.1.0 (OASIS)
Example toolsCodeQL, Semgrep, Bandit (Python), Brakeman (Ruby on Rails)
Related methodsDAST, SCA, Secret scanning

Static application security testing (SAST) looks for security flaws in an application's source code, or in its compiled form, without running the program.1 Also called source code analysis, it is usually done as part of code review, which is also known as white-box testing.2 Each finding points to a file and a line number.1 Static code scanning is one of the eleven minimum techniques for verifying software in NISTIR 8397, a 2021 guideline written under US Executive Order 14028.3

How the analysis works

Many of the techniques come from compilers. Lexical analysis turns the source text into tokens, such as a variable name, an operator or a string constant. A control flow graph represents the program as basic blocks of statements, connected by directed edges for each possible jump from one block to another.2

Security rules often rest on data flow analysis, which works out the values a variable can hold at each point in the program and where those values travel. CodeQL models this as a graph whose nodes are the values that exist at run time, separate from the syntax tree. Local data flow follows values inside one function; global data flow also follows them between functions and through object properties, at a higher cost in time.4

Taint analysis applies data flow to security. Data from outside the program, such as a request parameter, is a source and is marked tainted. A function where tainted data is dangerous, such as a database query call or eval, is a sink. A function that checks or cleans the data is a sanitizer. The tool reports a finding when tainted data reaches a sink without passing a sanitizer.5 Taint also passes through operations that build a new value from an old one: after y = x + 1, the variable y is tainted if x was.4

The query from the SQL injection article shows the path a taint rule follows:

// Node.js with the pg library
const sql = "SELECT * FROM orders WHERE id = " + req.query.id;
const { rows } = await pool.query(sql);

The request parameter req.query.id is the source. Joining it to the SQL text makes sql tainted, and pool.query is the sink, so the tool reports the second line. In the parameterized form the value travels in a separate array and the SQL text is a fixed string, so no tainted data reaches the query text.

Rules and results

A SAST tool applies a set of rules, each written for one weakness. In Semgrep a rule is a YAML entry with an id, a message that says why the code matched and how to fix it, a severity of LOW, MEDIUM, HIGH or CRITICAL chosen by the rule's author, the languages it applies to, and a code pattern.6 The following rule, based on an example in the Semgrep documentation, flags every MD5 call in Python:

rules:
  - id: md5-usage
    languages:
      - python
    message: Found md5 usage
    pattern: hashlib.md5(...)
    severity: HIGH

A rule turns into a taint rule with mode: taint, and then lists its sources, sinks and sanitizers in place of a single pattern.5

The Static Analysis Results Interchange Format (SARIF) is an OASIS standard for the output of static analysis tools; the current version is 2.1.0, with errata approved in August 2023.7 GitHub code scanning accepts SARIF from third-party tools and also runs CodeQL, the analysis engine GitHub develops. It shows each finding as an alert in the repository and closes the alert once the code that caused it is fixed.8

Running a scan

The Semgrep command line tool scans a project with semgrep scan, run from the root of the code. It exits with code 0 whenever the scan finishes, even with findings; the --error flag makes it exit with code 1 when there are findings, which lets a continuous integration job fail.9 A comment // nosemgrep: rule-id on the matched line or the line above it marks a finding as ignored; the finding is still recorded with that status.10

Limits

SAST finds some classes of flaw well, such as SQL injection and buffer overflows. It finds others poorly: authentication problems, access control flaws and insecure use of cryptography. On their own, current tools catch only a relatively small share of application security flaws, and they report many false positives.1

False positives arise because the tool cannot be sure what happens to data on its way through the application, and also when the data passes through closed-source components or outside systems. False negatives arise when the flaw lies in a component the tool does not see, or depends on how the runtime environment is configured.2 Data flow also stops at library functions whose source is missing, and some call targets are known only at run time.4 Configuration problems that are not represented in the code are out of reach, and many tools struggle with code that cannot be compiled.1

The OWASP Benchmark, a runnable Java and Python application with deliberate vulnerabilities, is built to measure the speed and accuracy of detection tools, and every vulnerability it scores is meant to be exploitable.11 Tools can be compared on their support for the language, their coverage of the OWASP Top 10, their false positive and false negative rates, and their Benchmark score.1

Relation to other tests

SAST reads the code a team wrote. DAST sends requests to the running application. SCA checks the third-party packages the code depends on for known vulnerabilities, and Secret scanning searches the code and its history for credentials. NISTIR 8397 lists these as separate techniques, next to Fuzzing and Threat modelling.3 Some tools combine SAST, DAST and SCA.1

In SecFlash

SecFlash reads the code of an app and reports its findings on the Security page with the file and line, and most codes carry a Common Weakness Enumeration number. Codes include SF-SEC-704 for SQL built from strings, SF-SEC-756 for a database function that builds SQL from its own input, SF-SEC-710 for HTML output that is not escaped, SF-SEC-705 for code run from a string or unsafe loading, SF-SEC-706 for a shell command built from input, SF-SEC-716 for an outgoing request to an address from input, SF-SEC-717 for a file path built from input, SF-SEC-742 for a redirect to an address from input and SF-SEC-707 for weak hashing or encryption. SF-SEC-711 covers other risky patterns, such as debug mode left on.

See also

References

  1. a b c d e f "Source Code Analysis Tools". OWASP Community. OWASP Foundation.
  2. a b c Dewhurst, Ryan, et al. "Static Code Analysis". OWASP Community. OWASP Foundation.
  3. a b Black, Paul; Guttman, Barbara; Okun, Vadim (October 2021). "Guidelines on Minimum Standards for Developer Verification of Software". NISTIR 8397. NIST.
  4. a b c "About data flow analysis". CodeQL documentation. GitHub.
  5. a b "Taint analysis overview". Semgrep documentation.
  6. back "Rule syntax". Semgrep documentation.
  7. back OASIS (28 August 2023). "Static Analysis Results Interchange Format (SARIF) Version 2.1.0 Plus Errata 01". OASIS Standard.
  8. back "Code scanning". GitHub Docs. GitHub.
  9. back "Semgrep CLI". Semgrep documentation.
  10. back "Ignore files, folders, and code". Semgrep documentation.
  11. back "OWASP Benchmark". OWASP Projects. OWASP Foundation.