Skip to content

Commit 2f644ba

Browse files
committed
Add Tests and update library
1 parent 677b404 commit 2f644ba

File tree

6 files changed

+59
-29
lines changed

6 files changed

+59
-29
lines changed

python/lib/github/cryptography/RandomNumberGenerator.qll

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@ private import semmle.python.ApiGraphs
22
private import semmle.python.Concepts
33
private import semmle.python.dataflow.new.DataFlow
44

5-
abstract class RandomNumberGeneratorSinks extends DataFlow::Node { }
5+
module RandomNumberGenerator {
6+
abstract class Sinks extends DataFlow::Node { }
67

7-
class OsRandom extends RandomNumberGeneratorSinks {
8-
OsRandom() {
9-
exists(DataFlow::Node call |
10-
// https://docs.python.org/3/library/os.html#os.getrandom
11-
call = API::moduleImport("os").getMember("getrandom").getACall() and
12-
this = call
13-
)
8+
class OsRandom extends Sinks {
9+
OsRandom() {
10+
exists(DataFlow::Node call |
11+
// https://docs.python.org/3/library/os.html#os.getrandom
12+
call = API::moduleImport("os").getMember("getrandom").getACall() and
13+
this = call
14+
)
15+
}
1416
}
15-
}
1617

17-
class PyRandom extends RandomNumberGeneratorSinks {
18-
PyRandom() {
19-
exists(DataFlow::Node call |
20-
(
18+
class PyRandom extends Sinks {
19+
PyRandom() {
20+
exists(DataFlow::Node call |
21+
// TODO: does `random.seed(_)` need to be static?
2122
// https://docs.python.org/3/library/random.html#random.random
22-
call = API::moduleImport("random").getMember("random").getACall()
23-
or
24-
// https://docs.python.org/3/library/random.html#random.randbytes
25-
call = API::moduleImport("random").getMember("randbytes").getACall()
26-
) and
27-
this = call
28-
)
23+
call =
24+
API::moduleImport("random")
25+
.getMember(["random", "randrange", "randint", "randbytes"])
26+
.getACall() and
27+
this = call
28+
)
29+
}
2930
}
30-
}
3131

32-
class PyUuid extends RandomNumberGeneratorSinks {
32+
class PyUuid extends Sinks {
3333
PyUuid() {
34-
exists(DataFlow::Node call |
35-
call = API::moduleImport("uuid").getMember("uuid1").getACall() or
36-
call = API::moduleImport("uuid").getMember("uuid3").getACall() and
37-
this = call
38-
)
34+
exists(DataFlow::Node call |
35+
call = API::moduleImport("uuid").getMember(["uuid1", "uuid3"]).getACall() and
36+
this = call
37+
)
3938
}
39+
}
4040
}

python/src/security/CWE-338/WeakPRNG.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
import python
1515
import github.cryptography.RandomNumberGenerator
1616

17-
from RandomNumberGeneratorSinks rngs
18-
select rngs.asExpr(), "Using weak PRNG"
17+
from RandomNumberGenerator::Sinks rngs
18+
select rngs, "Using weak PRNG"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| app.py:6:1:6:16 | ControlFlowNode for Attribute() | Using weak PRNG |
2+
| app.py:11:1:11:15 | ControlFlowNode for Attribute() | Using weak PRNG |
3+
| app.py:12:1:12:23 | ControlFlowNode for Attribute() | Using weak PRNG |
4+
| app.py:13:1:13:21 | ControlFlowNode for Attribute() | Using weak PRNG |
5+
| app.py:15:1:15:20 | ControlFlowNode for Attribute() | Using weak PRNG |
6+
| app.py:18:1:18:12 | ControlFlowNode for Attribute() | Using weak PRNG |
7+
| app.py:19:1:19:44 | ControlFlowNode for Attribute() | Using weak PRNG |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
security/CWE-338/WeakPRNG.ql
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import os
2+
import random
3+
import uuid
4+
5+
# os module
6+
os.getrandom(10)
7+
8+
# random module
9+
random.seed("8")
10+
11+
random.random()
12+
random.randrange(0, 10)
13+
random.randint(0, 10)
14+
15+
random.randbytes(10)
16+
17+
# uuid module
18+
uuid.uuid1()
19+
uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
20+
uuid.uuid4()
21+
uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=0

0 commit comments

Comments
 (0)