Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

SV.CSRF.TOKEN

This error identifies state changing request handlers that do not validate incoming requests against a stored CSRF token.

Vulnerability and risk

Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user’s web browser to perform an unwanted action on a trusted site for which the user is currently authenticated.

A standard approach to mitigating CSRF vulnerabilities is to use synchronizer tokens. A cryptographically secure random token is uniquely associated with a user session and added as a hidden field to any form that results in a state changing operation. The server rejects the requested action if the CSRF token fails validation.

Code examples

Vulnerable code example 1

6    void doPost(HttpServletRequest req, HttpServletResponse resp) {
7        String action = req.getParameter("action");
8        String id = req.getParameter("id");
9    
10       if("update".equals(action)){
11           updateUser(id, req);
12       }
13       //...
14   }
15
16   void updateUser(String userid, HttpServletRequest req) {
17       //...
18   }

SV.CSRF.TOKEN is reported on line 6: ‘req’ is never checked against a possible cross-site request forgery attack. The handler should verify that the request contains a valid CSRF token before changing the state of the application.

Fixed code example 1

6    void doPost(HttpServletRequest req, HttpServletResponse resp) {
7        if
8    (req.getParameter("_csrf_token").equals(req.getSession().getAttribute("_csrf_token"))) {
9            String action = req.getParameter("action");
10           String id = req.getParameter("id");
11 
12           if ("update".equals(action)) {
13               updateUser(id, req);
14           }
15           //...
16       }
17   }
18
19   void updateUser(String id, HttpServletRequest req) {
20       //...
21   }

The updated code validates the request before modifying the application state. If the token submitted with the request doesn’t match the token stored in the user’s session, the request is denied.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information.