Skip to content

Commit 2a5908f

Browse files
committed
python: require all settings be vulnerable
at least all thos not in tests
1 parent 3416f07 commit 2a5908f

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

python/ql/src/Security/CWE-352/CSRFProtectionDisabled.ql

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,24 @@
1414
import python
1515
import semmle.python.Concepts
1616

17-
from HTTP::Server::CsrfProtectionSetting s
18-
where
19-
s.getVerificationSetting() = false and
20-
not exists(HTTP::Server::CsrfLocalProtectionSetting p | p.csrfEnabled()) and
17+
predicate relevantSetting(HTTP::Server::CsrfProtectionSetting s) {
2118
// rule out test code as this is a common place to turn off CSRF protection.
2219
// We don't use normal `TestScope` to find test files, since we also want to match
2320
// a settings file such as `.../integration-tests/settings.py`
2421
not s.getLocation().getFile().getAbsolutePath().matches("%test%")
25-
select s, "Potential CSRF vulnerability due to forgery protection being disabled or weakened."
22+
}
23+
24+
predicate vulnerableSetting(HTTP::Server::CsrfProtectionSetting s) {
25+
s.getVerificationSetting() = false and
26+
not exists(HTTP::Server::CsrfLocalProtectionSetting p | p.csrfEnabled()) and
27+
relevantSetting(s)
28+
}
29+
30+
from HTTP::Server::CsrfProtectionSetting setting
31+
where
32+
vulnerableSetting(setting) and
33+
// We have seen examples of dummy projects with vulnerable settings alongside a main
34+
// project with a protecting settings file. We want to rule out this scenario, so we
35+
// require all non-test settings to be vulnerable.
36+
forall( HTTP::Server::CsrfProtectionSetting s| relevantSetting(s) | vulnerableSetting(s) )
37+
select setting, "Potential CSRF vulnerability due to forgery protection being disabled or weakened."

0 commit comments

Comments
 (0)