|
| 1 | +/** |
| 2 | + * Contains utility methods and classes to assist with generating data extensions models. |
| 3 | + */ |
| 4 | + |
| 5 | +private import ruby |
| 6 | +private import codeql.ruby.ApiGraphs |
| 7 | + |
| 8 | +/** |
| 9 | + * A file that is relevant in the context of library modeling. |
| 10 | + * |
| 11 | + * In practice, this means a file that is not part of test code. |
| 12 | + */ |
| 13 | +class RelevantFile extends File { |
| 14 | + RelevantFile() { not this.getRelativePath().regexpMatch(".*/?test(case)?s?/.*") } |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Gets an access path of an argument corresponding to the given `paramNode`. |
| 19 | + */ |
| 20 | +string getArgumentPath(DataFlow::ParameterNode paramNode) { |
| 21 | + paramNode.getLocation().getFile() instanceof RelevantFile and |
| 22 | + exists(Ast::Parameter param, string paramSpecifier | |
| 23 | + param = paramNode.asParameter() and |
| 24 | + ( |
| 25 | + paramSpecifier = param.getPosition().toString() |
| 26 | + or |
| 27 | + paramSpecifier = param.(Ast::KeywordParameter).getName() + ":" |
| 28 | + or |
| 29 | + param instanceof Ast::BlockParameter and |
| 30 | + paramSpecifier = "block" |
| 31 | + ) |
| 32 | + | |
| 33 | + result = "Argument[" + paramSpecifier + "]" |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Holds if `(type,path)` evaluates to the given method, when evalauted from a client of the current library. |
| 39 | + */ |
| 40 | +predicate pathToMethod(DataFlow::MethodNode method, string type, string path) { |
| 41 | + method.getLocation().getFile() instanceof RelevantFile and |
| 42 | + exists(DataFlow::ModuleNode mod, string methodName | |
| 43 | + method = mod.getOwnInstanceMethod(methodName) and |
| 44 | + if methodName = "initialize" |
| 45 | + then ( |
| 46 | + type = mod.getQualifiedName() + "!" and |
| 47 | + path = "Method[new]" |
| 48 | + ) else ( |
| 49 | + type = mod.getQualifiedName() and |
| 50 | + path = "Method[" + methodName + "]" |
| 51 | + ) |
| 52 | + or |
| 53 | + method = mod.getOwnSingletonMethod(methodName) and |
| 54 | + type = mod.getQualifiedName() + "!" and |
| 55 | + path = "Method[" + methodName + "]" |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +/** Gets any parameter to `method`. This may be a positional, keyword, or block parameter. */ |
| 60 | +private DataFlow::ParameterNode getAnyParameter(DataFlow::MethodNode method) { |
| 61 | + result = [method.getParameter(_), method.getKeywordParameter(_), method.getBlockParameter()] |
| 62 | +} |
| 63 | + |
| 64 | +private predicate pathToNodeBase(API::Node node, string type, string path, boolean isOutput) { |
| 65 | + exists(DataFlow::MethodNode method, string prevPath | pathToMethod(method, type, prevPath) | |
| 66 | + isOutput = true and |
| 67 | + node = method.getAReturnNode().backtrack() and |
| 68 | + path = prevPath + ".ReturnValue" and |
| 69 | + not method.getMethodName() = "initialize" // ignore return value of initialize method |
| 70 | + or |
| 71 | + isOutput = false and |
| 72 | + exists(DataFlow::ParameterNode paramNode | |
| 73 | + paramNode = getAnyParameter(method) and |
| 74 | + node = paramNode.track() |
| 75 | + | |
| 76 | + path = prevPath + "." + getArgumentPath(paramNode) |
| 77 | + ) |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +private predicate pathToNodeRec( |
| 82 | + API::Node node, string type, string path, boolean isOutput, int pathLength |
| 83 | +) { |
| 84 | + pathLength < 8 and |
| 85 | + ( |
| 86 | + pathToNodeBase(node, type, path, isOutput) and |
| 87 | + pathLength = 1 |
| 88 | + or |
| 89 | + exists(API::Node prevNode, string prevPath, boolean prevIsOutput, int prevPathLength | |
| 90 | + pathToNodeRec(prevNode, type, prevPath, prevIsOutput, prevPathLength) and |
| 91 | + pathLength = prevPathLength + 1 |
| 92 | + | |
| 93 | + node = prevNode.getAnElement() and |
| 94 | + path = prevPath + ".Element" and |
| 95 | + isOutput = prevIsOutput |
| 96 | + or |
| 97 | + node = prevNode.getReturn() and |
| 98 | + path = prevPath + ".ReturnValue" and |
| 99 | + isOutput = prevIsOutput |
| 100 | + or |
| 101 | + prevIsOutput = false and |
| 102 | + isOutput = true and |
| 103 | + ( |
| 104 | + exists(int n | |
| 105 | + node = prevNode.getParameter(n) and |
| 106 | + path = prevPath + ".Parameter[" + n + "]" |
| 107 | + ) |
| 108 | + or |
| 109 | + exists(string name | |
| 110 | + node = prevNode.getKeywordParameter(name) and |
| 111 | + path = prevPath + ".Parameter[" + name + ":]" |
| 112 | + ) |
| 113 | + or |
| 114 | + node = prevNode.getBlock() and |
| 115 | + path = prevPath + ".Parameter[block]" |
| 116 | + ) |
| 117 | + ) |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Holds if `(type,path)` evaluates to a value corresponding to `node`, when evaluated from a client of the current library. |
| 123 | + */ |
| 124 | +predicate pathToNode(API::Node node, string type, string path, boolean isOutput) { |
| 125 | + pathToNodeRec(node, type, path, isOutput, _) |
| 126 | +} |
0 commit comments