Skip to content

Commit 3fe9a3d

Browse files
committed
Python: Add modeling of simplejson PyPI package
I noticed that we don't handle PostUpdateNote very well in the concept tests, for exmaple for `json.dump(...)` there _should_ have been an `encodeOutput` as part of the inline expectations. I'll work on fixing that up in a separate PR, to keep things clean.
1 parent 8afdf26 commit 3fe9a3d

File tree

9 files changed

+140
-0
lines changed

9 files changed

+140
-0
lines changed

docs/codeql/support/reusables/frameworks.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Python built-in support
156156
Tornado, Web framework
157157
PyYAML, Serialization
158158
dill, Serialization
159+
simplejson, Serialization
159160
fabric, Utility library
160161
invoke, Utility library
161162
idna, Utility library
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added modeling of the PyPI package `simplejson`.

python/ql/src/semmle/python/Frameworks.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private import semmle.python.frameworks.MysqlConnectorPython
1616
private import semmle.python.frameworks.MySQLdb
1717
private import semmle.python.frameworks.Psycopg2
1818
private import semmle.python.frameworks.PyMySQL
19+
private import semmle.python.frameworks.Simplejson
1920
private import semmle.python.frameworks.Stdlib
2021
private import semmle.python.frameworks.Tornado
2122
private import semmle.python.frameworks.Yaml
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Provides classes modeling security-relevant aspects of the `simplejson` PyPI package.
3+
* See https://simplejson.readthedocs.io/en/latest/.
4+
*/
5+
6+
private import python
7+
private import semmle.python.dataflow.new.DataFlow
8+
private import semmle.python.dataflow.new.TaintTracking
9+
private import semmle.python.Concepts
10+
private import semmle.python.ApiGraphs
11+
12+
/**
13+
* Provides models for the `simplejson` PyPI package.
14+
* See https://simplejson.readthedocs.io/en/latest/.
15+
*/
16+
private module SimplejsonModel {
17+
/**
18+
* A call to `simplejson.dumps`.
19+
*
20+
* See https://simplejson.readthedocs.io/en/latest/#simplejson.dumps
21+
*/
22+
private class SimplejsonDumpsCall extends Encoding::Range, DataFlow::CallCfgNode {
23+
SimplejsonDumpsCall() { this = API::moduleImport("simplejson").getMember("dumps").getACall() }
24+
25+
override DataFlow::Node getAnInput() { result in [this.getArg(0), this.getArgByName("obj")] }
26+
27+
override DataFlow::Node getOutput() { result = this }
28+
29+
override string getFormat() { result = "JSON" }
30+
}
31+
32+
/**
33+
* A call to `simplejson.dump`.
34+
*
35+
* See https://simplejson.readthedocs.io/en/latest/#simplejson.dump
36+
*/
37+
private class SimplejsonDumpCall extends Encoding::Range, DataFlow::CallCfgNode {
38+
SimplejsonDumpCall() { this = API::moduleImport("simplejson").getMember("dump").getACall() }
39+
40+
override DataFlow::Node getAnInput() { result in [this.getArg(0), this.getArgByName("obj")] }
41+
42+
override DataFlow::Node getOutput() {
43+
result.(DataFlow::PostUpdateNode).getPreUpdateNode() in [
44+
this.getArg(1), this.getArgByName("fp")
45+
]
46+
}
47+
48+
override string getFormat() { result = "JSON" }
49+
}
50+
51+
/**
52+
* A call to `simplejson.loads`.
53+
*
54+
* See https://simplejson.readthedocs.io/en/latest/#simplejson.loads
55+
*/
56+
private class SimplejsonLoadsCall extends Decoding::Range, DataFlow::CallCfgNode {
57+
SimplejsonLoadsCall() { this = API::moduleImport("simplejson").getMember("loads").getACall() }
58+
59+
override DataFlow::Node getAnInput() { result in [this.getArg(0), this.getArgByName("s")] }
60+
61+
override DataFlow::Node getOutput() { result = this }
62+
63+
override string getFormat() { result = "JSON" }
64+
65+
override predicate mayExecuteInput() { none() }
66+
}
67+
68+
/**
69+
* A call to `simplejson.load`.
70+
*
71+
* See https://simplejson.readthedocs.io/en/latest/#simplejson.load
72+
*/
73+
private class SimplejsonLoadCall extends Decoding::Range, DataFlow::CallCfgNode {
74+
SimplejsonLoadCall() { this = API::moduleImport("simplejson").getMember("load").getACall() }
75+
76+
override DataFlow::Node getAnInput() { result in [this.getArg(0), this.getArgByName("fp")] }
77+
78+
override DataFlow::Node getOutput() { result = this }
79+
80+
override string getFormat() { result = "JSON" }
81+
82+
override predicate mayExecuteInput() { none() }
83+
}
84+
}

python/ql/test/library-tests/frameworks/simplejson/ConceptsTest.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import python
2+
import experimental.meta.ConceptsTest
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
argumentToEnsureNotTaintedNotMarkedAsSpurious
2+
untaintedArgumentToEnsureTaintedNotMarkedAsMissing
3+
failures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import experimental.meta.InlineTaintTest
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import simplejson
2+
from io import StringIO
3+
4+
def test():
5+
ts = TAINTED_STRING
6+
tainted_obj = {"foo": ts}
7+
8+
encoded = simplejson.dumps(tainted_obj) # $ encodeOutput=Attribute() encodeFormat=JSON encodeInput=tainted_obj
9+
10+
ensure_tainted(
11+
encoded, # $ tainted
12+
simplejson.dumps(tainted_obj), # $ tainted encodeOutput=Attribute() encodeFormat=JSON encodeInput=tainted_obj
13+
simplejson.dumps(obj=tainted_obj), # $ tainted encodeOutput=Attribute() encodeFormat=JSON encodeInput=tainted_obj
14+
simplejson.loads(encoded), # $ tainted decodeOutput=Attribute() decodeFormat=JSON decodeInput=encoded
15+
simplejson.loads(s=encoded), # $ tainted decodeOutput=Attribute() decodeFormat=JSON decodeInput=encoded
16+
)
17+
18+
# load/dump with file-like
19+
tainted_filelike = StringIO()
20+
simplejson.dump(tainted_obj, tainted_filelike) # $ encodeFormat=JSON encodeInput=tainted_obj
21+
22+
tainted_filelike.seek(0)
23+
ensure_tainted(
24+
tainted_filelike, # $ tainted
25+
simplejson.load(tainted_filelike), # $ tainted decodeOutput=Attribute() decodeFormat=JSON decodeInput=tainted_filelike
26+
)
27+
28+
# load/dump with file-like using keyword-args
29+
tainted_filelike = StringIO()
30+
simplejson.dump(obj=tainted_obj, fp=tainted_filelike) # $ encodeFormat=JSON encodeInput=tainted_obj
31+
32+
tainted_filelike.seek(0)
33+
ensure_tainted(
34+
tainted_filelike, # $ tainted
35+
simplejson.load(fp=tainted_filelike), # $ tainted decodeOutput=Attribute() decodeFormat=JSON decodeInput=tainted_filelike
36+
)
37+
38+
# To make things runable
39+
40+
TAINTED_STRING = "TAINTED_STRING"
41+
def ensure_tainted(*args):
42+
print("- ensure_tainted")
43+
for i, arg in enumerate(args):
44+
print("arg {}: {!r}".format(i, arg))
45+
46+
test()

0 commit comments

Comments
 (0)