Pushing Left, Like a Boss – Part 7: Code Review and Static Code Analysis

This article is about secure code review and Static Application Security Testing (SAST). Static analysis is a highly valuable activity which can find a lot of security problems, far before you get to the testing or release stages, potentially saving both time and money.
Note: SCA is Static Composition Analysis, verifying that your dependencies are not known to be vulnerable. I have heard many say "static code analysis" when referring to code review/SAST tools, shortening it to SCA for simplicity. We will not do that, SCA will only be used to refer to static composition analysis.
When application security folks say ‘static’ analysis, we mean that we will look at written code, as opposed to ‘dynamic’, which means when your code is running on a web server.
Since I wrote this article a few years ago, I have had a chance to do more in the code review space and spend some time working with SAST tools. Although my attention span is short, and I can be impatient at times (such as, for example, when I am awake), I can now spot several types of problems fairly easily. If you had asked me a few years ago if I would ever find code review pleasurable, I would have laughed, but now I find validating SAST results rather satisfying. It’s funny how much our opinions can change over time.
There are two options for doing code review; manual or with a tool. There are pros and cons to each, and using both will get you the best results.
When reviewing code manually for security you don’t read every line of code; you just review the security controls to ensure they are in all the places they should be and that they are implemented correctly. Although I have not completed hundreds of secure code reviews in my career, I do recall discovering in delight when there was no input validation on a data field, or that an app was not using stored procedures but inline SQL, both of which are big no-nos. It was so obvious when I knew where to look and what to look for. However, most code reviews are not so simple, and many bugs are difficult or nearly impossible to spot with only the naked eye.
Note: when I say “only review the security controls” I mean things like a login, input validation, authorization, authentication, integration points, etc. Anything that has to do with the security of the app.
When using a tool for code review you would use something called a ‘static code analyzer’ or a ‘SAST’ (Static Application Security Testing) tool. This special kind of software parses your code into areas of concern and attempts to follow every possible outcome. It takes a lot of processing power and can take hours or even days to complete. It then creates a report with approximately 60-80% false positives.
Note, since I first wrote this article new types of static code analysis tools have been created that allow for very fancy grepping (regex searching) of the code base, with templates to help you find problematic code. How cool is that!
I know what you are thinking right now: 80% false positives !?!?!?! Why would anyone want to use a tool like that? Let me explain.
The key to looking at results of an SAST tool is that the items it lists are not answers, they are hints of where to look for problems, that the code reviewer (hopefully a security expert) can investigate. This means instead of reading 20,000 lines of code, the code reviewer uses the tool, it finds 200 ‘clues’, and then from those 200 ‘clues’ they end up finding 20 real bugs. And many of those bugs they could not have found with just their eyes, because SAST tools can go several layers deep into the code, in a way humans just can’t.
When performing code review it is possible to find all sorts of other problems with your application, not just security issues. During one of my projects the code reviewer found several memory leaks. When we fixed them our application became lightening fast, which made our project team look amazing. There is so much more than just security problems that a good code reviewer can find; it is definitely a worth-while task if you want to build truly resilient and secure software.
Although we already discussed this in Part 5.2 Using Safe Dependencies, I’m going to bring it up again: everyone needs to verify the security of their 3rd party code/components/frameworks/libraries/whatever-you-want-to-call-the-code-in-your-app-that-you-didn’t-write. You must verify that they (3rd party components) are not known to be vulnerable. When I say ‘known to be vulnerable’, I mean there is currently information available on the internet about the vulnerability that is documented on what the problem is and/or how to exploit it.
Many organizations and industry spokespeople create a lot of fear, uncertainty and doubt (FUD) around zero days (vulnerabilities in popular software for which there is no existing patch), advanced persistent threat (APT - someone living on your network for an extended period of time, spying on you) or very advanced attackers, such as nation states. In reality, almost all serious security incidents are a result of our industry not keeping up with the basics; missing patches, people with admin privileges clicking on a phishing email while logged in, and well-known (and therefore preventable) software vulnerabilities, such as using code with known vulnerabilities in it. Essentially; basic security hygiene.
Bonus resource: My friend Paul Ionescu created a code review series.
Up next we will talk about the Testing Phase of the SDLC!