| 1 |
// a query |
| 2 |
|
| 3 |
/** |
| 4 |
* @name LDAP query built from user-controlled sources |
| 5 |
* @description Building an LDAP query from user-controlled sources is vulnerable to insertion of |
| 6 |
* malicious LDAP code by the user. |
| 7 |
* @kind path-problem |
| 8 |
* @problem.severity error |
| 9 |
* @id py/ldap-injection |
| 10 |
* @tags experimental |
| 11 |
* security |
| 12 |
* external/cwe/cwe-090 |
| 13 |
*/ |
| 14 |
|
| 15 |
import python |
| 16 |
import experimental.semmle.python.security.injection.LDAP |
| 17 |
import DataFlow::PathGraph |
| 18 |
|
| 19 |
from LDAPInjectionFlowConfig config, DataFlow::PathNode source, DataFlow::PathNode sink |
| 20 |
where config.hasFlowPath(source, sink) |
| 21 |
select sink.getNode(), source, sink, "$@ LDAP query parameter comes from $@.", sink.getNode(), |
| 22 |
"This", source.getNode(), "a user-provided value" |
| 23 |
|
| 24 |
// a concept |
| 25 |
|
| 26 |
module LDAPEscape { |
| 27 |
abstract class Range extends DataFlow::Node { |
| 28 |
abstract DataFlow::Node getAnInput(); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
class LDAPEscape extends DataFlow::Node { |
| 33 |
LDAPEscape::Range range; |
| 34 |
|
| 35 |
LDAPEscape() { this = range } |
| 36 |
|
| 37 |
DataFlow::Node getAnInput() { result = range.getAnInput() } |
| 38 |
} |
| 39 |
|
| 40 |
// a library modeling |
| 41 |
|
| 42 |
private module LDAP2 { |
| 43 |
private class LDAP2QueryMethods extends string { |
| 44 |
LDAP2QueryMethods() { |
| 45 |
this in ["search", "search_s", "search_st", "search_ext", "search_ext_s"] |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
private class LDAP2Query extends DataFlow::CallCfgNode, LDAPQuery::Range { |
| 50 |
DataFlow::Node ldapQuery; |
| 51 |
|
| 52 |
LDAP2Query() { |
| 53 |
exists(DataFlow::AttrRead searchMethod | |
| 54 |
this.getFunction() = searchMethod and |
| 55 |
API::moduleImport("ldap").getMember("initialize").getACall() = |
| 56 |
searchMethod.getObject().getALocalSource() and |
| 57 |
searchMethod.getAttributeName() instanceof LDAP2QueryMethods and |
| 58 |
( |
| 59 |
ldapQuery = this.getArg(0) |
| 60 |
or |
| 61 |
( |
| 62 |
ldapQuery = this.getArg(2) or |
| 63 |
ldapQuery = this.getArgByName("filterstr") |
| 64 |
) |
| 65 |
) |
| 66 |
) |
| 67 |
} |
| 68 |
|
| 69 |
override DataFlow::Node getQuery() { result = ldapQuery } |
| 70 |
} |
| 71 |
|
| 72 |
private class LDAP2EscapeDNCall extends DataFlow::CallCfgNode, LDAPEscape::Range { |
| 73 |
LDAP2EscapeDNCall() { |
| 74 |
this = API::moduleImport("ldap").getMember("dn").getMember("escape_dn_chars").getACall() |
| 75 |
} |
| 76 |
|
| 77 |
override DataFlow::Node getAnInput() { result = this.getArg(0) } |
| 78 |
} |
| 79 |
|
| 80 |
private class LDAP2EscapeFilterCall extends DataFlow::CallCfgNode, LDAPEscape::Range { |
| 81 |
LDAP2EscapeFilterCall() { |
| 82 |
this = |
| 83 |
API::moduleImport("ldap").getMember("filter").getMember("escape_filter_chars").getACall() |
| 84 |
} |
| 85 |
|
| 86 |
override DataFlow::Node getAnInput() { result = this.getArg(0) } |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
// a taint flow config |
| 91 |
|
| 92 |
class LDAPInjectionFlowConfig extends TaintTracking::Configuration { |
| 93 |
LDAPInjectionFlowConfig() { this = "LDAPInjectionFlowConfig" } |
| 94 |
|
| 95 |
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 96 |
|
| 97 |
override predicate isSink(DataFlow::Node sink) { sink = any(LDAPQuery ldapQuery).getQuery() } |
| 98 |
|
| 99 |
override predicate isSanitizer(DataFlow::Node sanitizer) { |
| 100 |
sanitizer = any(LDAPEscape ldapEsc).getAnInput() |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// From https://github.com/github/codeql/pull/5443/files |