|
| 1 | +/** |
| 2 | + * Temporary: provides a class to extend current cookies to header declarations |
| 3 | + */ |
| 4 | + |
| 5 | +import python |
| 6 | +import semmle.python.dataflow.new.DataFlow |
| 7 | +import semmle.python.dataflow.new.TaintTracking |
| 8 | +import experimental.semmle.python.Concepts |
| 9 | + |
| 10 | +/** |
| 11 | + * Gets a header setting a cookie. |
| 12 | + * |
| 13 | + * Given the following example: |
| 14 | + * |
| 15 | + * ```py |
| 16 | + * @app.route("/") |
| 17 | + * def flask_make_response(): |
| 18 | + * resp = make_response("") |
| 19 | + * resp.headers['Set-Cookie'] = "name=value; Secure;" |
| 20 | + * return resp |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * * `this` would be `resp.headers['Set-Cookie'] = "name=value; Secure;"`. |
| 24 | + * * `isSecure()` predicate would succeed. |
| 25 | + * * `isHttpOnly()` predicate would fail. |
| 26 | + * * `isSameSite()` predicate would fail. |
| 27 | + * * `getName()` and `getValue()` results would be `"name=value; Secure;"`. |
| 28 | + */ |
| 29 | +class CookieHeader extends Cookie::Range instanceof HeaderDeclaration { |
| 30 | + CookieHeader() { |
| 31 | + this instanceof HeaderDeclaration and |
| 32 | + exists(StrConst str | |
| 33 | + str.getText() = "Set-Cookie" and |
| 34 | + DataFlow::exprNode(str) |
| 35 | + .(DataFlow::LocalSourceNode) |
| 36 | + .flowsTo(this.(HeaderDeclaration).getNameArg()) |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + override predicate isSecure() { |
| 41 | + exists(StrConst str | |
| 42 | + str.getText().regexpMatch(".*; *Secure;.*") and |
| 43 | + DataFlow::exprNode(str) |
| 44 | + .(DataFlow::LocalSourceNode) |
| 45 | + .flowsTo(this.(HeaderDeclaration).getValueArg()) |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + override predicate isHttpOnly() { |
| 50 | + exists(StrConst str | |
| 51 | + str.getText().regexpMatch(".*; *HttpOnly;.*") and |
| 52 | + DataFlow::exprNode(str) |
| 53 | + .(DataFlow::LocalSourceNode) |
| 54 | + .flowsTo(this.(HeaderDeclaration).getValueArg()) |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + override predicate isSameSite() { |
| 59 | + exists(StrConst str | |
| 60 | + str.getText().regexpMatch(".*; *SameSite=(Strict|Lax);.*") and |
| 61 | + DataFlow::exprNode(str) |
| 62 | + .(DataFlow::LocalSourceNode) |
| 63 | + .flowsTo(this.(HeaderDeclaration).getValueArg()) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + override DataFlow::Node getNameArg() { result = this.(HeaderDeclaration).getValueArg() } |
| 68 | + |
| 69 | + override DataFlow::Node getValueArg() { result = this.(HeaderDeclaration).getValueArg() } |
| 70 | + |
| 71 | + override DataFlow::Node getHeaderArg() { none() } |
| 72 | +} |
0 commit comments