Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b9ea3a

Browse files
yoffRasmusWL
andauthored
Apply suggestions from code review
Co-authored-by: Rasmus Wriedt Larsen <[email protected]>
1 parent b7e8b48 commit 3b9ea3a

5 files changed

Lines changed: 18 additions & 37 deletions

File tree

python/ql/src/experimental/semmle/python/frameworks/Dill.qll

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ private module Dill {
4343
* See https://pypi.org/project/dill/ (which currently refers you
4444
* to https://docs.python.org/3/library/pickle.html#pickle.loads)
4545
*/
46-
private class DillDeserialization extends UnmarshalingFunction::Range {
47-
DillDeserialization() {
46+
private class DillLoadsCall extends UnmarshalingFunction::Range {
47+
DillLoadsCall() {
4848
this.asCfgNode().(CallNode).getFunction() = Dill::dill::loads().asCfgNode()
4949
}
5050

@@ -56,10 +56,5 @@ private class DillDeserialization extends UnmarshalingFunction::Range {
5656

5757
override DataFlow::Node getOutput() { result = this }
5858

59-
override string getFormat() {
60-
result = this.asCfgNode().(CallNode).getArgByName("encoding").(NameNode).getId()
61-
or
62-
not exists(this.asCfgNode().(CallNode).getArgByName("encoding")) and
63-
result = "ASCII"
64-
}
59+
override string getFormat() { result = "dill" }
6560
}

python/ql/src/experimental/semmle/python/frameworks/Stdlib.qll

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ private module Stdlib {
363363
* A call to `marshal.loads`
364364
* See https://docs.python.org/3/library/marshal.html#marshal.loads
365365
*/
366-
private class MarshalDeserialization extends UnmarshalingFunction::Range {
367-
MarshalDeserialization() {
366+
private class MarshalLoadsCall extends UnmarshalingFunction::Range {
367+
MarshalLoadsCall() {
368368
this.asCfgNode().(CallNode).getFunction() = marshal::loads().asCfgNode()
369369
}
370370

@@ -376,13 +376,13 @@ private module Stdlib {
376376

377377
override DataFlow::Node getOutput() { result = this }
378378

379-
override string getFormat() { none() }
379+
override string getFormat() { result = "marshal" }
380380
}
381381

382382
// ---------------------------------------------------------------------------
383383
// pickle
384384
// ---------------------------------------------------------------------------
385-
private string pickleModuleName() { result in ["pickle", "cPickle"] }
385+
private string pickleModuleName() { result in ["pickle", "cPickle", "_pickle"] }
386386

387387
/** Gets a reference to the `pickle` module. */
388388
private DataFlow::Node pickle(DataFlow::TypeTracker t) {
@@ -416,8 +416,8 @@ private module Stdlib {
416416
* A call to `pickle.loads`
417417
* See https://docs.python.org/3/library/pickle.html#pickle.loads
418418
*/
419-
private class PickleDeserialization extends UnmarshalingFunction::Range {
420-
PickleDeserialization() {
419+
private class PickleLoadsCall extends UnmarshalingFunction::Range {
420+
PickleLoadsCall() {
421421
this.asCfgNode().(CallNode).getFunction() = pickle::loads().asCfgNode()
422422
}
423423

@@ -429,11 +429,6 @@ private module Stdlib {
429429

430430
override DataFlow::Node getOutput() { result = this }
431431

432-
override string getFormat() {
433-
result = this.asCfgNode().(CallNode).getArgByName("encoding").(NameNode).getId()
434-
or
435-
not exists(this.asCfgNode().(CallNode).getArgByName("encoding")) and
436-
result = "ASCII"
437-
}
432+
override string getFormat() { result = "pickle" }
438433
}
439434
}

python/ql/src/experimental/semmle/python/frameworks/Yaml.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ private class YamlDeserialization extends UnmarshalingFunction::Range {
6060

6161
override DataFlow::Node getOutput() { result = this }
6262

63-
override string getFormat() { none() }
63+
override string getFormat() { result = "YAML" }
6464
}
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import flask
21
import dill
32

4-
from flask import Flask, request
5-
6-
app = Flask(__name__)
7-
8-
9-
@app.route("/")
10-
def hello():
11-
payload = request.args.get("payload")
12-
dill.loads(payload) # $UNSAFE_getAnInput=payload $UNSAFE_getOutput=Attribute() $UNSAFE_getFormat=ASCII
13-
dill.loads(payload, encoding='latin1') # $UNSAFE_getAnInput=payload $UNSAFE_getOutput=Attribute() $UNSAFE_getFormat=latin1
3+
dill.loads(payload) # $UNSAFE_getAnInput=payload $UNSAFE_getOutput=Attribute() $UNSAFE_getFormat=ASCII
4+
dill.loads(payload, encoding='latin1') # $UNSAFE_getAnInput=payload $UNSAFE_getOutput=Attribute() $UNSAFE_getFormat=latin1

python/ql/test/experimental/query-tests/Security-new-dataflow/CWE-502/unsafe_deserialization.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
@app.route("/")
1313
def hello():
1414
payload = request.args.get("payload")
15-
pickle.loads(payload)
16-
yaml.load(payload)
17-
yaml.load(payload, Loader=SafeLoader)
18-
marshal.loads(payload)
15+
pickle.loads(payload) # NOT OK
16+
yaml.load(payload) # NOT OK
17+
yaml.load(payload, Loader=SafeLoader) # OK
18+
marshal.loads(payload) # NOT OK
1919

2020
import dill
21-
dill.loads(payload)
21+
dill.loads(payload) # NOT OK

0 commit comments

Comments
 (0)