|
| 1 | +/** |
| 2 | + * @name Failure to use secure cookies |
| 3 | + * @description Insecure cookies may be sent in cleartext, which makes them vulnerable to |
| 4 | + * interception. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity error |
| 7 | + * @security-severity 5.0 |
| 8 | + * @precision high |
| 9 | + * @id py/insecure-cookie |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-614 |
| 12 | + */ |
| 13 | + |
| 14 | +import python |
| 15 | +import semmle.python.dataflow.new.DataFlow |
| 16 | +import semmle.python.Concepts |
| 17 | + |
| 18 | +predicate hasProblem(Http::Server::CookieWrite cookie, string alert, int idx) { |
| 19 | + cookie.hasSecureFlag(false) and |
| 20 | + alert = "Secure" and |
| 21 | + idx = 0 |
| 22 | + or |
| 23 | + cookie.hasHttpOnlyFlag(false) and |
| 24 | + alert = "HttpOnly" and |
| 25 | + idx = 1 |
| 26 | + or |
| 27 | + cookie.hasSameSiteFlag(false) and |
| 28 | + alert = "SameSite" and |
| 29 | + idx = 2 |
| 30 | +} |
| 31 | + |
| 32 | +predicate hasAlert(Http::Server::CookieWrite cookie, string alert) { |
| 33 | + exists(int numProblems | numProblems = strictcount(string p | hasProblem(cookie, p, _)) | |
| 34 | + numProblems = 1 and |
| 35 | + alert = any(string prob | hasProblem(cookie, prob, _)) + " attribute" |
| 36 | + or |
| 37 | + numProblems = 2 and |
| 38 | + alert = |
| 39 | + strictconcat(string prob, int idx | hasProblem(cookie, prob, idx) | " and ", prob order by idx) |
| 40 | + + " attributes" |
| 41 | + or |
| 42 | + numProblems = 3 and |
| 43 | + alert = "Secure, HttpOnly, and SameSite attributes" |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +from Http::Server::CookieWrite cookie, string alert |
| 48 | +where hasAlert(cookie, alert) |
| 49 | +select cookie, "Cookie is added without the " + alert + " properly set." |
0 commit comments