Skip to content

Commit 774c811

Browse files
committed
python: move CSRF concepts inside HTTP::Server
1 parent 1e9840d commit 774c811

File tree

4 files changed

+76
-76
lines changed

4 files changed

+76
-76
lines changed

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -105,76 +105,6 @@ module FileSystemWriteAccess {
105105
}
106106
}
107107

108-
/**
109-
* A data-flow node that enables or disables Cross-site request forgery protection
110-
* in a global manner.
111-
*
112-
* Extend this class to refine existing API models. If you want to model new APIs,
113-
* extend `CsrfProtectionSetting::Range` instead.
114-
*/
115-
class CsrfProtectionSetting extends DataFlow::Node instanceof CsrfProtectionSetting::Range {
116-
/**
117-
* Gets the boolean value corresponding to if CSRF protection is enabled
118-
* (`true`) or disabled (`false`) by this node.
119-
*/
120-
boolean getVerificationSetting() { result = super.getVerificationSetting() }
121-
}
122-
123-
/** Provides a class for modeling new CSRF protection setting APIs. */
124-
module CsrfProtectionSetting {
125-
/**
126-
* A data-flow node that enables or disables Cross-site request forgery protection
127-
* in a global manner.
128-
*
129-
* Extend this class to model new APIs. If you want to refine existing API models,
130-
* extend `CsrfProtectionSetting` instead.
131-
*/
132-
abstract class Range extends DataFlow::Node {
133-
/**
134-
* Gets the boolean value corresponding to if CSRF protection is enabled
135-
* (`true`) or disabled (`false`) by this node.
136-
*/
137-
abstract boolean getVerificationSetting();
138-
}
139-
}
140-
141-
/**
142-
* A data-flow node that enables or disables Cross-site request forgery protection
143-
* for a specific part of an application.
144-
*
145-
* Extend this class to refine existing API models. If you want to model new APIs,
146-
* extend `CsrfLocalProtectionSetting::Range` instead.
147-
*/
148-
class CsrfLocalProtectionSetting extends DataFlow::Node instanceof CsrfLocalProtectionSetting::Range {
149-
/**
150-
* Gets a request handler whose CSRF protection is changed.
151-
*/
152-
Function getRequestHandler() { result = super.getRequestHandler() }
153-
154-
/** Holds if CSRF protection is enabled by this setting */
155-
predicate csrfEnabled() { super.csrfEnabled() }
156-
}
157-
158-
/** Provides a class for modeling new CSRF protection setting APIs. */
159-
module CsrfLocalProtectionSetting {
160-
/**
161-
* A data-flow node that enables or disables Cross-site request forgery protection
162-
* for a specific part of an application.
163-
*
164-
* Extend this class to model new APIs. If you want to refine existing API models,
165-
* extend `CsrfLocalProtectionSetting` instead.
166-
*/
167-
abstract class Range extends DataFlow::Node {
168-
/**
169-
* Gets a request handler whose CSRF protection is changed.
170-
*/
171-
abstract Function getRequestHandler();
172-
173-
/** Holds if CSRF protection is enabled by this setting */
174-
abstract predicate csrfEnabled();
175-
}
176-
}
177-
178108
/** Provides classes for modeling path-related APIs. */
179109
module Path {
180110
/**
@@ -956,6 +886,76 @@ module HTTP {
956886
abstract DataFlow::Node getValueArg();
957887
}
958888
}
889+
890+
/**
891+
* A data-flow node that enables or disables Cross-site request forgery protection
892+
* in a global manner.
893+
*
894+
* Extend this class to refine existing API models. If you want to model new APIs,
895+
* extend `CsrfProtectionSetting::Range` instead.
896+
*/
897+
class CsrfProtectionSetting extends DataFlow::Node instanceof CsrfProtectionSetting::Range {
898+
/**
899+
* Gets the boolean value corresponding to if CSRF protection is enabled
900+
* (`true`) or disabled (`false`) by this node.
901+
*/
902+
boolean getVerificationSetting() { result = super.getVerificationSetting() }
903+
}
904+
905+
/** Provides a class for modeling new CSRF protection setting APIs. */
906+
module CsrfProtectionSetting {
907+
/**
908+
* A data-flow node that enables or disables Cross-site request forgery protection
909+
* in a global manner.
910+
*
911+
* Extend this class to model new APIs. If you want to refine existing API models,
912+
* extend `CsrfProtectionSetting` instead.
913+
*/
914+
abstract class Range extends DataFlow::Node {
915+
/**
916+
* Gets the boolean value corresponding to if CSRF protection is enabled
917+
* (`true`) or disabled (`false`) by this node.
918+
*/
919+
abstract boolean getVerificationSetting();
920+
}
921+
}
922+
923+
/**
924+
* A data-flow node that enables or disables Cross-site request forgery protection
925+
* for a specific part of an application.
926+
*
927+
* Extend this class to refine existing API models. If you want to model new APIs,
928+
* extend `CsrfLocalProtectionSetting::Range` instead.
929+
*/
930+
class CsrfLocalProtectionSetting extends DataFlow::Node instanceof CsrfLocalProtectionSetting::Range {
931+
/**
932+
* Gets a request handler whose CSRF protection is changed.
933+
*/
934+
Function getRequestHandler() { result = super.getRequestHandler() }
935+
936+
/** Holds if CSRF protection is enabled by this setting */
937+
predicate csrfEnabled() { super.csrfEnabled() }
938+
}
939+
940+
/** Provides a class for modeling new CSRF protection setting APIs. */
941+
module CsrfLocalProtectionSetting {
942+
/**
943+
* A data-flow node that enables or disables Cross-site request forgery protection
944+
* for a specific part of an application.
945+
*
946+
* Extend this class to model new APIs. If you want to refine existing API models,
947+
* extend `CsrfLocalProtectionSetting` instead.
948+
*/
949+
abstract class Range extends DataFlow::Node {
950+
/**
951+
* Gets a request handler whose CSRF protection is changed.
952+
*/
953+
abstract Function getRequestHandler();
954+
955+
/** Holds if CSRF protection is enabled by this setting */
956+
abstract predicate csrfEnabled();
957+
}
958+
}
959959
}
960960

961961
/** Provides classes for modeling HTTP clients. */

python/ql/lib/semmle/python/frameworks/Django.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ module PrivateDjango {
23202320
/**
23212321
* A custom middleware stack
23222322
*/
2323-
private class DjangoSettingsMiddlewareStack extends CsrfProtectionSetting::Range {
2323+
private class DjangoSettingsMiddlewareStack extends HTTP::Server::CsrfProtectionSetting::Range {
23242324
List list;
23252325

23262326
DjangoSettingsMiddlewareStack() {
@@ -2356,7 +2356,7 @@ module PrivateDjango {
23562356
}
23572357
}
23582358

2359-
private class DjangoCsrfDecorator extends CsrfLocalProtectionSetting::Range {
2359+
private class DjangoCsrfDecorator extends HTTP::Server::CsrfLocalProtectionSetting::Range {
23602360
string decoratorName;
23612361
Function function;
23622362

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

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

17-
from CsrfProtectionSetting s
17+
from HTTP::Server::CsrfProtectionSetting s
1818
where
1919
s.getVerificationSetting() = false and
20-
not exists(CsrfLocalProtectionSetting p | p.csrfEnabled()) and
20+
not exists(HTTP::Server::CsrfLocalProtectionSetting p | p.csrfEnabled()) and
2121
// rule out test code as this is a common place to turn off CSRF protection
2222
not s.getLocation().getFile().getAbsolutePath().matches("%test%")
2323
select s, "Potential CSRF vulnerability due to forgery protection being disabled or weakened."

python/ql/test/experimental/meta/ConceptsTest.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ class CsrfProtectionSettingTest extends InlineExpectationsTest {
511511

512512
override predicate hasActualResult(Location location, string element, string tag, string value) {
513513
exists(location.getFile().getRelativePath()) and
514-
exists(CsrfProtectionSetting setting |
514+
exists(HTTP::Server::CsrfProtectionSetting setting |
515515
location = setting.getLocation() and
516516
element = setting.toString() and
517517
value = setting.getVerificationSetting().toString() and
@@ -527,7 +527,7 @@ class CsrfLocalProtectionSettingTest extends InlineExpectationsTest {
527527

528528
override predicate hasActualResult(Location location, string element, string tag, string value) {
529529
exists(location.getFile().getRelativePath()) and
530-
exists(CsrfLocalProtectionSetting p |
530+
exists(HTTP::Server::CsrfLocalProtectionSetting p |
531531
location = p.getLocation() and
532532
element = p.toString() and
533533
value = p.getRequestHandler().getName().toString() and

0 commit comments

Comments
 (0)