Skip to content

Commit da44b13

Browse files
authored
Merge pull request #14515 from atorralba/atorralba/java/spring-csrf-improv
Java: Improve java/spring-disabled-csrf-protection
2 parents 4246ebf + 4ecda9c commit da44b13

File tree

11 files changed

+79
-6
lines changed

11 files changed

+79
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** Provides predicates to reason about disabling CSRF protection in Spring. */
2+
3+
import java
4+
5+
/** Holds if `call` disables CSRF protection in Spring. */
6+
predicate disablesSpringCsrfProtection(MethodAccess call) {
7+
call.getMethod().hasName("disable") and
8+
call.getReceiverType()
9+
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
10+
"CsrfConfigurer<HttpSecurity>")
11+
or
12+
call.getMethod()
13+
.hasQualifiedName("org.springframework.security.config.annotation.web.builders",
14+
"HttpSecurity", "csrf") and
15+
call.getArgument(0)
16+
.(MemberRefExpr)
17+
.getReferencedCallable()
18+
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
19+
"AbstractHttpConfigurer", "disable")
20+
}

java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.ql

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
*/
1313

1414
import java
15+
import semmle.code.java.security.SpringCsrfProtection
1516

1617
from MethodAccess call
17-
where
18-
call.getMethod().hasName("disable") and
19-
call.getReceiverType()
20-
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
21-
"CsrfConfigurer<HttpSecurity>")
18+
where disablesSpringCsrfProtection(call)
2219
select call, "CSRF vulnerability due to protection being disabled."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The query `java/spring-disabled-csrf-protection` has been improved to detect more ways of disabling CSRF in Spring.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Type new Customizer<CsrfConfigurer<HttpSecurity>>(...) { ... } uses out-of-scope type variable B. Note the Java extractor is known to sometimes do this; the Kotlin extractor should not. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testFailures
2+
failures
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
3+
4+
public class SpringCsrfProtectionTest {
5+
protected void test(HttpSecurity http) throws Exception {
6+
http.csrf(csrf -> csrf.disable()); // $ hasSpringCsrfProtectionDisabled
7+
http.csrf().disable(); // $ hasSpringCsrfProtectionDisabled
8+
http.csrf(AbstractHttpConfigurer::disable); // $ hasSpringCsrfProtectionDisabled
9+
}
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java
2+
import semmle.code.java.security.SpringCsrfProtection
3+
import TestUtilities.InlineExpectationsTest
4+
5+
module SpringCsrfProtectionTest implements TestSig {
6+
string getARelevantTag() { result = "hasSpringCsrfProtectionDisabled" }
7+
8+
predicate hasActualResult(Location location, string element, string tag, string value) {
9+
tag = "hasSpringCsrfProtectionDisabled" and
10+
exists(MethodAccess call | disablesSpringCsrfProtection(call) |
11+
call.getLocation() = location and
12+
element = call.toString() and
13+
value = ""
14+
)
15+
}
16+
}
17+
18+
import MakeTest<SpringCsrfProtectionTest>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --javac-args -cp ${testdir}/../../../stubs/springframework-5.3.8

java/ql/test/stubs/springframework-5.3.8/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/springframework-5.3.8/org/springframework/security/config/annotation/web/configurers/AbstractHttpConfigurer.java

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)