|
| 1 | +/** |
| 2 | + * Provides modeling for `sqlite3`, a library that allows Ruby programs to use the SQLite3 database engine. |
| 3 | + * Version: 1.6.2 |
| 4 | + * https://github.com/sparklemotion/sqlite3-ruby |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.ApiGraphs |
| 9 | +private import codeql.ruby.dataflow.FlowSummary |
| 10 | +private import codeql.ruby.Concepts |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides modeling for `sqlite3`, a library that allows Ruby programs to use the SQLite3 database engine. |
| 14 | + * Version: 1.6.2 |
| 15 | + * https://github.com/sparklemotion/sqlite3-ruby |
| 16 | + */ |
| 17 | +module Sqlite3 { |
| 18 | + /** Gets a method call with a receiver that is a database instance. */ |
| 19 | + private DataFlow::CallNode getADatabaseMethodCall(string methodName) { |
| 20 | + exists(API::Node dbInstance | |
| 21 | + dbInstance = API::getTopLevelMember("SQLite3").getMember("Database").getInstance() and |
| 22 | + ( |
| 23 | + result = dbInstance.getAMethodCall(methodName) |
| 24 | + or |
| 25 | + // e.g. SQLite3::Database.new("foo.db") |db| { db.some_method } |
| 26 | + exists(DataFlow::BlockNode block | |
| 27 | + result.getMethodName() = methodName and |
| 28 | + block = dbInstance.getAValueReachableFromSource().(DataFlow::CallNode).getBlock() and |
| 29 | + block.getParameter(0).flowsTo(result.getReceiver()) |
| 30 | + ) |
| 31 | + ) |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + /** A prepared but unexecuted SQL statement. */ |
| 36 | + private class PreparedStatement extends SqlConstruction::Range, DataFlow::CallNode { |
| 37 | + PreparedStatement() { this = getADatabaseMethodCall("prepare") } |
| 38 | + |
| 39 | + override DataFlow::Node getSql() { result = this.getArgument(0) } |
| 40 | + } |
| 41 | + |
| 42 | + /** Execution of a prepared SQL statement. */ |
| 43 | + private class PreparedStatementExecution extends SqlExecution::Range, DataFlow::CallNode { |
| 44 | + private PreparedStatement stmt; |
| 45 | + |
| 46 | + PreparedStatementExecution() { |
| 47 | + stmt.flowsTo(this.getReceiver()) and |
| 48 | + this.getMethodName() = ["columns", "execute", "execute!", "get_metadata", "types"] |
| 49 | + } |
| 50 | + |
| 51 | + override DataFlow::Node getSql() { result = stmt.getReceiver() } |
| 52 | + } |
| 53 | + |
| 54 | + /** Gets the name of a method called against a database that executes an SQL statement. */ |
| 55 | + private string getAnExecutionMethodName() { |
| 56 | + result = |
| 57 | + [ |
| 58 | + "execute", "execute2", "execute_batch", "execute_batch2", "get_first_row", |
| 59 | + "get_first_value", "query" |
| 60 | + ] |
| 61 | + } |
| 62 | + |
| 63 | + /** A method call against a database that constructs an SQL query. */ |
| 64 | + private class DatabaseMethodCallSqlConstruction extends SqlConstruction::Range, DataFlow::CallNode |
| 65 | + { |
| 66 | + // Database query execution methods also construct an SQL query |
| 67 | + DatabaseMethodCallSqlConstruction() { |
| 68 | + this = getADatabaseMethodCall(getAnExecutionMethodName()) |
| 69 | + } |
| 70 | + |
| 71 | + override DataFlow::Node getSql() { result = this.getArgument(0) } |
| 72 | + } |
| 73 | + |
| 74 | + /** A method call against a database that executes an SQL query. */ |
| 75 | + private class DatabaseMethodCallSqlExecution extends SqlExecution::Range, DataFlow::CallNode { |
| 76 | + DatabaseMethodCallSqlExecution() { this = getADatabaseMethodCall(getAnExecutionMethodName()) } |
| 77 | + |
| 78 | + override DataFlow::Node getSql() { result = this.getArgument(0) } |
| 79 | + } |
| 80 | +} |
0 commit comments