1+ /**
2+ * Provides default sources, sinks and sanitizers for detecting
3+ * "NoSql injection"
4+ * vulnerabilities, as well as extension points for adding your own.
5+ */
6+
17import python
28import semmle.python.dataflow.new.DataFlow
39import semmle.python.dataflow.new.RemoteFlowSources
410import semmle.python.Concepts
511
12+ /**
13+ * Provides default sources, sinks and sanitizers for detecting
14+ * "NoSql injection"
15+ * vulnerabilities, as well as extension points for adding your own.
16+ */
617module NoSqlInjection {
718 private newtype TFlowState =
819 TStringInput ( ) or
920 TDictInput ( )
1021
22+ /** A flow state, tracking the structure of the input. */
1123 abstract class FlowState extends TFlowState {
24+ /** Gets a textual representation of this element. */
1225 abstract string toString ( ) ;
1326 }
1427
28+ /** A state where input is only a string. */
1529 class StringInput extends FlowState , TStringInput {
1630 override string toString ( ) { result = "StringInput" }
1731 }
1832
33+ /** A state where input is a dictionary. */
1934 class DictInput extends FlowState , TDictInput {
2035 override string toString ( ) { result = "DictInput" }
2136 }
2237
38+ /** A source allowing string inputs. */
2339 abstract class StringSource extends DataFlow:: Node { }
2440
41+ /** A source allowing dictionary inputs. */
2542 abstract class DictSource extends DataFlow:: Node { }
2643
44+ /** A sink vulnerable to user controlled strings. */
2745 abstract class StringSink extends DataFlow:: Node { }
2846
47+ /** A sink vulnerable to user controlled dictionaries. */
2948 abstract class DictSink extends DataFlow:: Node { }
3049
50+ /** A data flow node where a string is converted into a dictionary. */
3151 abstract class StringToDictConversion extends DataFlow:: Node {
52+ /** Gets the argument that specifies the string to be converted. */
3253 abstract DataFlow:: Node getAnInput ( ) ;
3354
55+ /** Gets the resulting dictionary. */
3456 abstract DataFlow:: Node getOutput ( ) ;
3557 }
3658
59+ /** A remote flow source considered a source of user controlled strings. */
3760 class RemoteFlowSourceAsStringSource extends RemoteFlowSource , StringSource { }
3861
62+ /** A NoSQL query that is vulnerable to user controlled strings. */
3963 class NoSqlQueryAsStringSink extends StringSink {
4064 NoSqlQueryAsStringSink ( ) {
4165 exists ( NoSqlQuery noSqlQuery | this = noSqlQuery .getQuery ( ) |
@@ -44,10 +68,12 @@ module NoSqlInjection {
4468 }
4569 }
4670
71+ /** A NoSQL query that is vulnerable to user controlled dictionaries. */
4772 class NoSqlQueryAsDictSink extends DictSink {
4873 NoSqlQueryAsDictSink ( ) { this = any ( NoSqlQuery noSqlQuery ) .getQuery ( ) }
4974 }
5075
76+ /** A JSON decoding converts a string to a dictionary. */
5177 class JsonDecoding extends Decoding , StringToDictConversion {
5278 JsonDecoding ( ) { this .getFormat ( ) = "JSON" }
5379
0 commit comments