Skip to content

Commit cde59fa

Browse files
authored
Merge pull request spotbugs#112 from KengoTODA/enable-spring-csrf-rules
add two rules which was introduced at find-sec-bugs v1.6
2 parents 32d6423 + 6d5784a commit cde59fa

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

generate_profiles/BuildXmlFiles.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ criticalBugs = [ //RCE or powerful function
108108
"EL_INJECTION",
109109
"SEAM_LOG_INJECTION",
110110
"OBJECT_DESERIALIZATION",
111-
"MALICIOUS_XSLT"
111+
"MALICIOUS_XSLT",
112+
"SPRING_CSRF_PROTECTION_DISABLED",
113+
"SPRING_CSRF_UNRESTRICTED_REQUEST_MAPPING"
112114
]
113115

114116
majorJspBugs = ["XSS_REQUEST_PARAMETER_TO_JSP_WRITER",

src/main/java/org/sonar/plugins/findbugs/rules/FindSecurityBugsRulesDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public final class FindSecurityBugsRulesDefinition implements RulesDefinition {
2727

2828
public static final String REPOSITORY_KEY = "findsecbugs";
2929
public static final String REPOSITORY_NAME = "Find Security Bugs";
30-
public static final int RULE_COUNT = 76;
30+
public static final int RULE_COUNT = 78;
3131

3232
@Override
3333
public void define(Context context) {

src/main/resources/org/sonar/plugins/findbugs/rules-findsecbugs.xml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,112 @@ such a setter. The use of these parameters should be reviewed to make sure they
10901090
This class should be analyzed to make sure that remotely exposed methods are safe to expose to potential attackers.&lt;/p&gt;</description>
10911091
<tag>security</tag>
10921092
</rule>
1093+
<rule key='SPRING_CSRF_PROTECTION_DISABLED' priority='CRITICAL'>
1094+
<name>Security - Spring CSRF protection disabled</name>
1095+
<configKey>SPRING_CSRF_PROTECTION_DISABLED</configKey>
1096+
<description>&lt;p&gt;Disabling Spring Security's CSRF protection is unsafe for standard web applications.&lt;/p&gt;
1097+
&lt;p&gt;A valid use case for disabling this protection would be a service exposing state-changing operations
1098+
that is guaranteed to be used only by non-browser clients.&lt;/p&gt;
1099+
&lt;p&gt;
1100+
&lt;b&gt;Insecure configuration:&lt;/b&gt;&lt;br/&gt;
1101+
&lt;pre&gt;@EnableWebSecurity
1102+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
1103+
1104+
@Override
1105+
protected void configure(HttpSecurity http) throws Exception {
1106+
http.csrf().disable();
1107+
}
1108+
}&lt;/pre&gt;
1109+
&lt;/p&gt;
1110+
&lt;p&gt;
1111+
&lt;b&gt;References&lt;/b&gt;&lt;br/&gt;
1112+
&lt;a href="https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection"&gt;Spring Security Official Documentation: When to use CSRF protection&lt;/a&gt;&lt;br/&gt;
1113+
&lt;a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29"&gt;OWASP: Cross-Site Request Forgery&lt;/a&gt;&lt;br/&gt;
1114+
&lt;a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29_Prevention_Cheat_Sheet"&gt;OWASP: CSRF Prevention Cheat Sheet&lt;/a&gt;&lt;br/&gt;
1115+
&lt;a href="https://cwe.mitre.org/data/definitions/352.html"&gt;CWE-352: Cross-Site Request Forgery (CSRF)&lt;/a&gt;
1116+
&lt;/p&gt;</description>
1117+
<tag>cwe</tag>
1118+
<tag>security</tag>
1119+
</rule>
1120+
<rule key='SPRING_CSRF_UNRESTRICTED_REQUEST_MAPPING' priority='CRITICAL'>
1121+
<name>Security - Spring CSRF unrestricted RequestMapping</name>
1122+
<configKey>SPRING_CSRF_UNRESTRICTED_REQUEST_MAPPING</configKey>
1123+
<description>&lt;p&gt;Methods annotated with &lt;code&gt;RequestMapping&lt;/code&gt; are by default mapped to all the HTTP request methods.
1124+
However, Spring Security's CSRF protection is not enabled by default
1125+
for the HTTP request methods &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;HEAD&lt;/code&gt;, &lt;code&gt;TRACE&lt;/code&gt;, and &lt;code&gt;OPTIONS&lt;/code&gt;
1126+
(as this could cause the tokens to be leaked).
1127+
Therefore, state-changing methods annotated with &lt;code&gt;RequestMapping&lt;/code&gt; and not narrowing the mapping
1128+
to the HTTP request methods &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;DELETE&lt;/code&gt;, or &lt;code&gt;PATCH&lt;/code&gt;
1129+
are vulnerable to CSRF attacks.&lt;/p&gt;
1130+
&lt;p&gt;
1131+
&lt;b&gt;Vulnerable Code:&lt;/b&gt;&lt;br/&gt;
1132+
&lt;pre&gt;@Controller
1133+
public class UnsafeController {
1134+
1135+
@RequestMapping("/path")
1136+
public void writeData() {
1137+
// State-changing operations performed within this method.
1138+
}
1139+
}&lt;/pre&gt;
1140+
&lt;/p&gt;
1141+
&lt;p&gt;
1142+
&lt;b&gt;Solution (Spring Framework 4.3 and later):&lt;/b&gt;&lt;br/&gt;
1143+
&lt;pre&gt;@Controller
1144+
public class SafeController {
1145+
1146+
/**
1147+
* For methods without side-effects use @GetMapping.
1148+
*/
1149+
@GetMapping("/path")
1150+
public String readData() {
1151+
// No state-changing operations performed within this method.
1152+
return "";
1153+
}
1154+
1155+
/**
1156+
* For state-changing methods use either @PostMapping, @PutMapping, @DeleteMapping, or @PatchMapping.
1157+
*/
1158+
@PostMapping("/path")
1159+
public void writeData() {
1160+
// State-changing operations performed within this method.
1161+
}
1162+
}&lt;/pre&gt;
1163+
&lt;/p&gt;
1164+
&lt;p&gt;
1165+
&lt;b&gt;Solution (Before Spring Framework 4.3):&lt;/b&gt;&lt;br/&gt;
1166+
&lt;pre&gt;@Controller
1167+
public class SafeController {
1168+
1169+
/**
1170+
* For methods without side-effects use either
1171+
* RequestMethod.GET, RequestMethod.HEAD, RequestMethod.TRACE, or RequestMethod.OPTIONS.
1172+
*/
1173+
@RequestMapping(value = "/path", method = RequestMethod.GET)
1174+
public String readData() {
1175+
// No state-changing operations performed within this method.
1176+
return "";
1177+
}
1178+
1179+
/**
1180+
* For state-changing methods use either
1181+
* RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, or RequestMethod.PATCH.
1182+
*/
1183+
@RequestMapping(value = "/path", method = RequestMethod.POST)
1184+
public void writeData() {
1185+
// State-changing operations performed within this method.
1186+
}
1187+
}&lt;/pre&gt;
1188+
&lt;/p&gt;
1189+
&lt;p&gt;
1190+
&lt;b&gt;References&lt;/b&gt;&lt;br/&gt;
1191+
&lt;a href="https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#csrf-use-proper-verbs"&gt;Spring Security Official Documentation: Use proper HTTP verbs (CSRF protection)&lt;/a&gt;&lt;br/&gt;
1192+
&lt;a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29"&gt;OWASP: Cross-Site Request Forgery&lt;/a&gt;&lt;br/&gt;
1193+
&lt;a href="https://www.owasp.org/index.php/Cross-Site_Request_Forgery_%28CSRF%29_Prevention_Cheat_Sheet"&gt;OWASP: CSRF Prevention Cheat Sheet&lt;/a&gt;&lt;br/&gt;
1194+
&lt;a href="https://cwe.mitre.org/data/definitions/352.html"&gt;CWE-352: Cross-Site Request Forgery (CSRF)&lt;/a&gt;
1195+
&lt;/p&gt;</description>
1196+
<tag>cwe</tag>
1197+
<tag>security</tag>
1198+
</rule>
10931199
<rule key='SQL_INJECTION_HIBERNATE' priority='CRITICAL'>
10941200
<name>Security - Potential SQL/HQL Injection (Hibernate)</name>
10951201
<configKey>SQL_INJECTION_HIBERNATE</configKey>

0 commit comments

Comments
 (0)