Static Code AnalysisTill Oberbeckmann5 min read

Static Code Analysis: Identifying and Fixing Security Vulnerabilities in an Early Stage

Static code analysis can be used to examine source code for many vulnerabilities before it is compiled. How does it work?

No software is free from bugs. In practice, bugs, usability issues, and security flaws will inevitably surface that the developers did not anticipate. However, static code analysis allows you to examine source code for many vulnerabilities before it is even compiled. In this article, we explain how it works, which analysis tools you need, and why manual analysis remains indispensable.

What Is Static Code Analysis?

Static code analysis identifies vulnerabilities within source code using a variety of techniques. This takes place during the code review process in the implementation phase -- before the code has been compiled into a runnable program. Static code analysis is a critical tool within a security development lifecycle. At this early stage, fixing issues is relatively inexpensive because adjustments can be made far more easily than in a production environment.

Techniques in Static Code Analysis

Static code analysis employs various techniques. In practice, tools combine all these techniques in a single interface, enabling automated checks of source code for a wide range of security vulnerabilities at once.

  • Data flow analysis simulates running software to collect security-relevant information about it.
  • Taint analysis traces user-controlled variables back to functions that could represent security vulnerabilities. For example, an attacker could pass specially crafted data through such a variable to gain access to other areas of the application.
  • Lexical analysis transforms the code syntax into information tokens, making it machine-readable for the tool. These tokens are then compared against development standards and systematically varied to identify vulnerabilities.

Tools for Static Code Analysis

For every major programming language, tools are available that combine multiple code analysis techniques in a single interface. In theory, fully automated static code analysis would not only replace the security analyst's work but also deliver an exceptionally high level of security. In practice, however, today's tools are not yet capable of identifying every type of security vulnerability found in modern software. They do not replace an analyst's manual work, but they help systematically review the code and find vulnerabilities more efficiently.

Analyze Java Source Code with SpotBugs and Find Security Bugs

SpotBugs lets you examine the source code of Java applications. This free tool checks code for over 400 known bugs and security vulnerabilities right after installation.

Building on SpotBugs, Find Security Bugs was developed to scan Java web applications for vulnerabilities as well. Find Security Bugs is available as a free download and currently detects 138 different security flaws. The tool is also available as a plug-in for all major development environments.

Scan C/C++ Source Code for Security Vulnerabilities with FlawFinder

FlawFinder is the go-to tool for static analysis of C and C++ code. It examines source code for potential security risks and reports them prioritized by risk level. FlawFinder is written in Python and can be easily installed via pip. Once set up, a single command scans the entire source code directory.

Check PHP Application Syntax with the PHP Code Sniffer

PHP Code Sniffer is a free, open-source script that lets you validate your PHP application's code against the PEAR standard and automatically correct errors. Installation and usage are straightforward. PHP Code Sniffer is compatible with most editors and can be added as an add-on.

Static Code Analysis: An Indispensable Tool for Better Security and Higher Software Quality

Static code analysis gives developers the opportunity to identify and proactively fix security issues before the software reaches a runnable state. Tools are available for every programming language that automatically scan code for various vulnerabilities. However, code analysis is not a silver bullet -- it cannot uncover every security flaw, so the manual expertise of a security analyst remains essential.