Skip to content

Commit

Permalink
Merge pull request #14515 from atorralba/atorralba/java/spring-csrf-i…
Browse files Browse the repository at this point in the history
…mprov

Java: Improve java/spring-disabled-csrf-protection
  • Loading branch information
atorralba authored Oct 18, 2023
2 parents 4246ebf + 4ecda9c commit da44b13
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 6 deletions.
20 changes: 20 additions & 0 deletions java/ql/lib/semmle/code/java/security/SpringCsrfProtection.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** Provides predicates to reason about disabling CSRF protection in Spring. */

import java

/** Holds if `call` disables CSRF protection in Spring. */
predicate disablesSpringCsrfProtection(MethodAccess call) {
call.getMethod().hasName("disable") and
call.getReceiverType()
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
"CsrfConfigurer<HttpSecurity>")
or
call.getMethod()
.hasQualifiedName("org.springframework.security.config.annotation.web.builders",
"HttpSecurity", "csrf") and
call.getArgument(0)
.(MemberRefExpr)
.getReferencedCallable()
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
"AbstractHttpConfigurer", "disable")
}
7 changes: 2 additions & 5 deletions java/ql/src/Security/CWE/CWE-352/SpringCSRFProtection.ql
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
*/

import java
import semmle.code.java.security.SpringCsrfProtection

from MethodAccess call
where
call.getMethod().hasName("disable") and
call.getReceiverType()
.hasQualifiedName("org.springframework.security.config.annotation.web.configurers",
"CsrfConfigurer<HttpSecurity>")
where disablesSpringCsrfProtection(call)
select call, "CSRF vulnerability due to protection being disabled."
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The query `java/spring-disabled-csrf-protection` has been improved to detect more ways of disabling CSRF in Spring.
Original file line number Diff line number Diff line change
@@ -0,0 +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. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
testFailures
failures
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;

public class SpringCsrfProtectionTest {
protected void test(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable()); // $ hasSpringCsrfProtectionDisabled
http.csrf().disable(); // $ hasSpringCsrfProtectionDisabled
http.csrf(AbstractHttpConfigurer::disable); // $ hasSpringCsrfProtectionDisabled
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java
import semmle.code.java.security.SpringCsrfProtection
import TestUtilities.InlineExpectationsTest

module SpringCsrfProtectionTest implements TestSig {
string getARelevantTag() { result = "hasSpringCsrfProtectionDisabled" }

predicate hasActualResult(Location location, string element, string tag, string value) {
tag = "hasSpringCsrfProtectionDisabled" and
exists(MethodAccess call | disablesSpringCsrfProtection(call) |
call.getLocation() = location and
element = call.toString() and
value = ""
)
}
}

import MakeTest<SpringCsrfProtectionTest>
1 change: 1 addition & 0 deletions java/ql/test/query-tests/security/CWE-352/options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle-extractor-options: --javac-args -cp ${testdir}/../../../stubs/springframework-5.3.8

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit da44b13

Please sign in to comment.