Skip to content

Commit 6588606

Browse files
committed
Python: Account for other 'falsey' values in query.
1 parent 06e5bc8 commit 6588606

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

python/ql/src/Security/CWE-295/RequestWithoutValidation.qhelp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Never use <code>verify=False</code> when making a request.
2121

2222
<example>
2323
<p>
24-
The example shows an unsafe call to <a href="https://semmle.com">semmle.com</a>, followed by various safe alternatives.
24+
The example shows two unsafe calls to <a href="https://semmle.com">semmle.com</a>, followed by various safe alternatives.
2525
</p>
2626

2727
<sample src="examples/make_request.py" />

python/ql/src/Security/CWE-295/RequestWithoutValidation.ql

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ FunctionObject requestFunction() {
2121
)
2222
}
2323

24-
from CallNode call, FunctionObject func, ControlFlowNode false_
24+
/** requests treats None as the default and all other "falsey" values as False */
25+
predicate falseNotNone(Object o) {
26+
o.booleanValue() = false and not o = theNoneObject()
27+
}
28+
29+
from CallNode call, FunctionObject func, Object falsey, ControlFlowNode origin
2530
where
2631
func = requestFunction() and
2732
func.getACall() = call and
28-
call.getArgByName("verify").refersTo(theFalseObject(), false_)
33+
falseNotNone(falsey) and
34+
call.getArgByName("verify").refersTo(falsey, origin)
2935

30-
select call, "Call to $@ with verify=$@", func, "requests." + func.getName(), false_, "False"
36+
select call, "Call to $@ with verify=$@", func, "requests." + func.getName(), origin, "False"

python/ql/src/Security/CWE-295/examples/make_request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import requests
22

3-
#An unsafe request
3+
#Unsafe requests
44

55
requests.get('https://semmle.com', verify=False) # UNSAFE
6+
requests.get('https://semmle.com', verify=0) # UNSAFE
67

78
#Various safe options
89

python/ql/test/query-tests/Security/CWE-295/RequestWithoutValidation.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
| make_request.py:7:1:7:49 | ControlFlowNode for Attribute() | Call to $@ with verify=$@ | ../lib/requests.py:11:1:11:46 | Function post | requests.post | make_request.py:7:44:7:48 | ControlFlowNode for False | False |
33
| make_request.py:12:1:12:39 | ControlFlowNode for put() | Call to $@ with verify=$@ | ../lib/requests.py:14:1:14:34 | Function put | requests.put | make_request.py:12:34:12:38 | ControlFlowNode for False | False |
44
| make_request.py:28:5:28:46 | ControlFlowNode for patch() | Call to $@ with verify=$@ | ../lib/requests.py:17:1:17:36 | Function patch | requests.patch | make_request.py:30:6:30:10 | ControlFlowNode for False | False |
5+
| make_request.py:34:1:34:45 | ControlFlowNode for Attribute() | Call to $@ with verify=$@ | ../lib/requests.py:11:1:11:46 | Function post | requests.post | make_request.py:34:44:34:44 | ControlFlowNode for IntegerLiteral | False |

python/ql/test/query-tests/Security/CWE-295/make_request.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ def req2(verify):
3030
req2(False) # BAD (at line 28)
3131
req2("/path/to/cert/") # GOOD
3232

33+
#Falsey value
34+
requests.post('https://semmle.com', verify=0) # BAD

0 commit comments

Comments
 (0)