-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14515 from atorralba/atorralba/java/spring-csrf-i…
…mprov Java: Improve java/spring-disabled-csrf-protection
- Loading branch information
Showing
11 changed files
with
79 additions
and
6 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
java/ql/lib/semmle/code/java/security/SpringCsrfProtection.qll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
java/ql/src/change-notes/2023-10-16-spring-disabled-csrf-protection-improved.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
1 change: 1 addition & 0 deletions
1
java/ql/test/query-tests/security/CWE-352/CONSISTENCY/typeParametersInScope.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
2 changes: 2 additions & 0 deletions
2
java/ql/test/query-tests/security/CWE-352/SpringCsrfProtectionTest.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
testFailures | ||
failures |
10 changes: 10 additions & 0 deletions
10
java/ql/test/query-tests/security/CWE-352/SpringCsrfProtectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
java/ql/test/query-tests/security/CWE-352/SpringCsrfProtectionTest.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
10 changes: 10 additions & 0 deletions
10
...ework-5.3.8/org/springframework/security/config/annotation/web/builders/HttpSecurity.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 3 additions & 1 deletion
4
...rg/springframework/security/config/annotation/web/configurers/AbstractHttpConfigurer.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...-5.3.8/org/springframework/security/config/annotation/web/configurers/CsrfConfigurer.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.