|
| 1 | +private import python |
| 2 | +private import experimental.semmle.python.Concepts |
| 3 | +private import semmle.python.ApiGraphs |
| 4 | + |
| 5 | +private module Python_JWT { |
| 6 | + /** |
| 7 | + * Gets a call to `python_jwt.process_jwt`. |
| 8 | + * |
| 9 | + * Given the following example: |
| 10 | + * |
| 11 | + * ```py |
| 12 | + * python_jwt.process_jwt(token) |
| 13 | + * python_jwt.verify_jwt(token, "key", "HS256") |
| 14 | + * ``` |
| 15 | + * |
| 16 | + * * `this` would be `jwt.process_jwt(token)`. |
| 17 | + * * `getPayload()`'s result would be `token`. |
| 18 | + * * `getKey()`'s result would be `"key"`. |
| 19 | + * * `getAlgorithm()`'s result would be `"HS256"`. |
| 20 | + * * `getAlgorithmstring()`'s result would be `HS256`. |
| 21 | + * * `getOptions()`'s result would be `none()`. |
| 22 | + * * `verifiesSignature()` predicate would succeed. |
| 23 | + */ |
| 24 | + private class PythonJwtProcessCall extends DataFlow::CallCfgNode, JWTDecoding::Range { |
| 25 | + PythonJwtProcessCall() { |
| 26 | + this = API::moduleImport("python_jwt").getMember("process_jwt").getACall() |
| 27 | + } |
| 28 | + |
| 29 | + private DataFlow::CallCfgNode verifyCall() { |
| 30 | + result = API::moduleImport("python_jwt").getMember("verify_jwt").getACall() and |
| 31 | + this.getPayload().getALocalSource() = result.getArg(0).getALocalSource() |
| 32 | + } |
| 33 | + |
| 34 | + override DataFlow::Node getPayload() { result = this.getArg(0) } |
| 35 | + |
| 36 | + override DataFlow::Node getKey() { result = this.verifyCall().getArg(1) } |
| 37 | + |
| 38 | + override DataFlow::Node getAlgorithm() { result = this.verifyCall().getArg(2) } |
| 39 | + |
| 40 | + override string getAlgorithmString() { |
| 41 | + exists(StrConst str | |
| 42 | + DataFlow::exprNode(str).(DataFlow::LocalSourceNode).flowsTo(this.getAlgorithm()) and |
| 43 | + result = str.getText() |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + override DataFlow::Node getOptions() { none() } |
| 48 | + |
| 49 | + override predicate verifiesSignature() { exists(this.verifyCall()) } |
| 50 | + } |
| 51 | +} |
0 commit comments