|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with [knex](https://knexjs.org). |
| 3 | + */ |
| 4 | + |
| 5 | +private import javascript |
| 6 | + |
| 7 | +/** |
| 8 | + * Provides classes and predicates for working with [knex](https://knexjs.org). |
| 9 | + */ |
| 10 | +module Knex { |
| 11 | + /** Gets an API node referring to the `knex` library. */ |
| 12 | + API::Node knexLibrary() { result = API::moduleImport("knex") } |
| 13 | + |
| 14 | + /** Gets a method name on Knex objects which return a Knex object. */ |
| 15 | + bindingset[result] |
| 16 | + private string chainableKnexMethod() { |
| 17 | + not result in [ |
| 18 | + "toString", "valueOf", "then", "catch", "finally", "toSQL", "asCallback", "stream" |
| 19 | + ] |
| 20 | + } |
| 21 | + |
| 22 | + /** Gets an API node referring to a `knex` object, such as `knex.from('foo')`. */ |
| 23 | + API::Node knexObject() { |
| 24 | + result = knexLibrary().getReturn() |
| 25 | + or |
| 26 | + result = knexObject().getReturn() |
| 27 | + or |
| 28 | + result = knexObject().getMember("schema") |
| 29 | + or |
| 30 | + result = knexObject().getMember(chainableKnexMethod()).getReturn() |
| 31 | + or |
| 32 | + // callback for building inner queries, such as `knex.join(function() { this.on('blah') })` |
| 33 | + result = knexObject().getMember(chainableKnexMethod()).getParameter(0).getReceiver() |
| 34 | + or |
| 35 | + // knex.transaction(trx => { ... }) |
| 36 | + result = knexObject().getMember("transaction").getParameter(0).getParameter(0) |
| 37 | + } |
| 38 | + |
| 39 | + /** A call to a Knex method that takes a raw SQL string as input. */ |
| 40 | + class RawKnexCall extends DataFlow::CallNode { |
| 41 | + RawKnexCall() { this = knexObject().getMember(["raw", any(string s) + "Raw"]).getACall() } |
| 42 | + } |
| 43 | + |
| 44 | + /** A SQL string passed to a raw Knex method. */ |
| 45 | + private class RawKnexSqlString extends SQL::SqlString { |
| 46 | + RawKnexSqlString() { this = any(RawKnexCall call).getArgument(0).asExpr() } |
| 47 | + } |
| 48 | + |
| 49 | + /** A call that triggers a SQL query submission. */ |
| 50 | + private class KnexDatabaseAccess extends DatabaseAccess { |
| 51 | + KnexDatabaseAccess() { |
| 52 | + this = knexObject().getMember(["then", "stream", "asCallback"]).getACall() |
| 53 | + or |
| 54 | + exists(AwaitExpr await | |
| 55 | + this = await.flow() and |
| 56 | + await.getOperand() = knexObject().getAUse().asExpr() |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + override DataFlow::Node getAQueryArgument() { none() } |
| 61 | + } |
| 62 | +} |
0 commit comments