Skip to content

Commit d28e800

Browse files
Add sensitive data heuristic
1 parent 6eac6b7 commit d28e800

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

python/ql/lib/semmle/python/Concepts.qll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ private import semmle.python.dataflow.new.TaintTracking
1212
private import semmle.python.Files
1313
private import semmle.python.Frameworks
1414
private import semmle.python.security.internal.EncryptionKeySizes
15+
private import semmle.python.dataflow.new.SensitiveDataSources
1516
private import codeql.threatmodels.ThreatModels
1617
private import codeql.concepts.ConceptsShared
1718

@@ -1290,6 +1291,18 @@ module Http {
12901291
*/
12911292
DataFlow::Node getValueArg() { result = super.getValueArg() }
12921293

1294+
/** Holds if the name of this cookie indicates it may contain sensitive information. */
1295+
predicate isSensitive() {
1296+
exists(DataFlow::Node name |
1297+
name = [this.getNameArg(), this.getHeaderArg()] and
1298+
(
1299+
name instanceof SensitiveDataSource
1300+
or
1301+
name = sensitiveLookupStringConst(_)
1302+
)
1303+
)
1304+
}
1305+
12931306
/**
12941307
* Holds if the `Secure` flag of the cookie is known to have a value of `b`.
12951308
*/

python/ql/lib/semmle/python/dataflow/new/SensitiveDataSources.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,5 @@ private module SensitiveDataModeling {
334334
}
335335

336336
predicate sensitiveDataExtraStepForCalls = SensitiveDataModeling::extraStepForCalls/2;
337+
338+
predicate sensitiveLookupStringConst = SensitiveDataModeling::sensitiveLookupStringConst/1;

python/ql/src/Security/CWE-614/InsecureCookie.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ import semmle.python.dataflow.new.DataFlow
1616
import semmle.python.Concepts
1717

1818
from Http::Server::CookieWrite cookie
19-
where cookie.hasSecureFlag(false)
19+
where cookie.hasSecureFlag(false) //and
20+
//cookie.isSensitive()
2021
select cookie, "Cookie is added without the Secure attribute properly set."
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
| test.py:8:5:8:37 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
2-
| test.py:10:5:10:52 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
3-
| test.py:11:5:11:56 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
4-
| test.py:12:5:12:53 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
5-
| test.py:13:5:13:54 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
6-
| test.py:15:5:15:71 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
7-
| test.py:17:5:17:69 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
1+
| test.py:8:5:8:40 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
2+
| test.py:10:5:10:57 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |
3+
| test.py:11:5:11:60 | ControlFlowNode for Attribute() | Cookie is added without the Secure attribute properly set. |

python/ql/test/query-tests/Security/CWE-614-InsecureCookie/test.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
@app.route("/test")
66
def test():
77
resp = make_response()
8-
resp.set_cookie("key1", "value1") # $Alert[py/insecure-cookie]
9-
resp.set_cookie("key2", "value2", secure=True)
10-
resp.set_cookie("key2", "value2", httponly=True) # $Alert[py/insecure-cookie]
11-
resp.set_cookie("key2", "value2", samesite="Strict") # $Alert[py/insecure-cookie]
12-
resp.set_cookie("key2", "value2", samesite="Lax") # $Alert[py/insecure-cookie]
13-
resp.set_cookie("key2", "value2", samesite="None") # $Alert[py/insecure-cookie]
14-
resp.set_cookie("key2", "value2", secure=True, samesite="Strict")
15-
resp.set_cookie("key2", "value2", httponly=True, samesite="Strict") # $Alert[py/insecure-cookie]
16-
resp.set_cookie("key2", "value2", secure=True, samesite="None")
17-
resp.set_cookie("key2", "value2", httponly=True, samesite="None") # $Alert[py/insecure-cookie]
18-
resp.set_cookie("key2", "value2", secure=True, httponly=True, samesite="Strict")
8+
resp.set_cookie("authKey", "value1") # $Alert[py/insecure-cookie]
9+
resp.set_cookie("authKey", "value2", secure=True)
10+
resp.set_cookie("sessionID", "value2", httponly=True) # $Alert[py/insecure-cookie]
11+
resp.set_cookie("password", "value2", samesite="Strict") # $Alert[py/insecure-cookie]
12+
resp.set_cookie("notSensitive", "value3")

0 commit comments

Comments
 (0)