Skip to content

Commit 7c085ec

Browse files
committed
Python: Add test for map_reduce
Also log requirement for old versions of `pymongo`
1 parent 30c37ca commit 7c085ec

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
flask
2-
pymongo
2+
pymongo==3.9

python/ql/test/query-tests/Security/CWE-943-NoSqlInjection/PoC/server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@ def by_group():
9090
post = posts.aggregate([{ "$group": group }]).next() # $ result=BAD
9191
return show_post(post, author)
9292

93+
# works with pymongo 3.9, `map_reduce` is removed in pymongo 4.0
94+
@app.route('/byMapReduce', methods=['GET'])
95+
def by_map_reduce():
96+
author = request.args['author']
97+
mapper = 'function() { emit(this.author, this.author === "'+author+'") }'
98+
reducer = "function(key, values) { return values.some( x => x ) }"
99+
results = posts.map_reduce(mapper, reducer, "results")
100+
# Use `" | "a" === "a` as author
101+
# making the query `this.author === "" | "a" === "a"`
102+
# Found by http://127.0.0.1:5000/byMapReduce?author=%22%20|%20%22a%22%20===%20%22a
103+
post = results.find_one({'value': True}) # $ MISSING: result=BAD
104+
if(post):
105+
post["author"] = post["_id"]
106+
return show_post(post, author)
107+
93108
@app.route('/', methods=['GET'])
94109
def show_routes():
95110
links = []

0 commit comments

Comments
 (0)