|
| 1 | +private import semmle.code.cpp.models.interfaces.Sql |
| 2 | +private import semmle.code.cpp.models.interfaces.FunctionInputsAndOutputs |
| 3 | + |
| 4 | +private predicate pqxxTransactionSqlArgument(string function, int arg) { |
| 5 | + function = "exec" and arg = 0 |
| 6 | + or |
| 7 | + function = "exec0" and arg = 0 |
| 8 | + or |
| 9 | + function = "exec1" and arg = 0 |
| 10 | + or |
| 11 | + function = "exec_n" and arg = 1 |
| 12 | + or |
| 13 | + function = "exec_params" and arg = 0 |
| 14 | + or |
| 15 | + function = "exec_params0" and arg = 0 |
| 16 | + or |
| 17 | + function = "exec_params1" and arg = 0 |
| 18 | + or |
| 19 | + function = "exec_params_n" and arg = 1 |
| 20 | + or |
| 21 | + function = "query_value" and arg = 0 |
| 22 | + or |
| 23 | + function = "stream" and arg = 0 |
| 24 | +} |
| 25 | + |
| 26 | +private predicate pqxxConnectionSqlArgument(string function, int arg) { |
| 27 | + function = "prepare" and arg = 1 |
| 28 | +} |
| 29 | + |
| 30 | +private predicate pqxxTransationClassNames(string className, string namespace) { |
| 31 | + namespace = "pqxx" and |
| 32 | + className in [ |
| 33 | + "dbtransaction", "nontransaction", "basic_robusttransaction", "robusttransaction", |
| 34 | + "subtransaction", "transaction", "basic_transaction", "transaction_base", "work" |
| 35 | + ] |
| 36 | +} |
| 37 | + |
| 38 | +private predicate pqxxConnectionClassNames(string className, string namespace) { |
| 39 | + namespace = "pqxx" and |
| 40 | + className in ["connection_base", "basic_connection", "connection"] |
| 41 | +} |
| 42 | + |
| 43 | +private predicate pqxxEscapeArgument(string function, int arg) { |
| 44 | + arg = 0 and |
| 45 | + function in ["esc", "esc_raw", "quote", "quote_raw", "quote_name", "quote_table", "esc_like"] |
| 46 | +} |
| 47 | + |
| 48 | +private class PostgreSqlSink extends SqlSink { |
| 49 | + PostgreSqlSink() { |
| 50 | + exists(Class c | |
| 51 | + this.getDeclaringType() = c and |
| 52 | + // transaction exec and connection prepare variations |
| 53 | + ( |
| 54 | + pqxxTransationClassNames(c.getName(), c.getNamespace().getName()) and |
| 55 | + pqxxTransactionSqlArgument(this.getName(), _) |
| 56 | + or |
| 57 | + pqxxConnectionSqlArgument(this.getName(), _) and |
| 58 | + pqxxConnectionClassNames(c.getName(), c.getNamespace().getName()) |
| 59 | + ) |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + override predicate getAnSqlParameter(FunctionInput input) { |
| 64 | + exists(int argIndex | |
| 65 | + pqxxTransactionSqlArgument(this.getName(), argIndex) |
| 66 | + or |
| 67 | + pqxxConnectionSqlArgument(this.getName(), argIndex) |
| 68 | + | |
| 69 | + input.isParameterDeref(argIndex) |
| 70 | + ) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +private class PostgreSqlBarrier extends SqlBarrier { |
| 75 | + PostgreSqlBarrier() { |
| 76 | + exists(Class c | |
| 77 | + this.getDeclaringType() = c and |
| 78 | + // transaction and connection escape functions |
| 79 | + ( |
| 80 | + pqxxTransationClassNames(c.getName(), c.getNamespace().getName()) or |
| 81 | + pqxxConnectionClassNames(c.getName(), c.getNamespace().getName()) |
| 82 | + ) and |
| 83 | + pqxxEscapeArgument(this.getName(), _) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + override predicate getAnEscapedParameter(FunctionInput input, FunctionOutput output) { |
| 88 | + exists(int argIndex | |
| 89 | + input.isParameterDeref(argIndex) and |
| 90 | + output.isReturnValueDeref() |
| 91 | + ) |
| 92 | + } |
| 93 | +} |
0 commit comments