|
| 1 | +/** |
| 2 | + * Contains an extension of `GraphExport` that relies on API graph specific functionality. |
| 3 | + */ |
| 4 | + |
| 5 | +private import ApiGraphModels as Shared |
| 6 | +private import codeql.mad.dynamic.GraphExport |
| 7 | +private import ApiGraphModelsSpecific as Specific |
| 8 | + |
| 9 | +private module API = Specific::API; |
| 10 | + |
| 11 | +private import Shared |
| 12 | + |
| 13 | +/** |
| 14 | + * Holds if some proper prefix of `(type, path)` evaluated to `node`, where `remainingPath` |
| 15 | + * is bound to the suffix of `path` that was not evaluated yet. |
| 16 | + * |
| 17 | + * See concrete examples in `TypeGraphExport`. |
| 18 | + */ |
| 19 | +bindingset[type, path] |
| 20 | +private predicate partiallyEvaluatedModel( |
| 21 | + string type, AccessPath path, API::Node node, string remainingPath |
| 22 | +) { |
| 23 | + exists(int n | |
| 24 | + getNodeFromPath(type, path, n) = node and |
| 25 | + n > 0 and |
| 26 | + // Note that `n < path.getNumToken()` is implied by the use of strictconcat() |
| 27 | + remainingPath = |
| 28 | + strictconcat(int k | k = [n .. path.getNumToken() - 1] | path.getToken(k), "." order by k) |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Holds if `type` and all types leading to `type` should be re-exported. |
| 34 | + */ |
| 35 | +signature predicate shouldContainTypeSig(string type); |
| 36 | + |
| 37 | +/** |
| 38 | + * Wrapper around `GraphExport` that also exports information about re-exported types. |
| 39 | + * |
| 40 | + * ### JavaScript example 1 |
| 41 | + * For example, suppose `shouldContainType("foo")` holds, and the following is the entry point for a package `bar`: |
| 42 | + * ```js |
| 43 | + * // bar.js |
| 44 | + * module.exports.xxx = require('foo'); |
| 45 | + * ``` |
| 46 | + * then this would generate the following type model: |
| 47 | + * ``` |
| 48 | + * foo; bar; Member[xxx] |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * ### JavaScript example 2 |
| 52 | + * For a more complex case, suppose the following type model exists: |
| 53 | + * ``` |
| 54 | + * foo.XYZ; foo; Member[x].Member[y].Member[z] |
| 55 | + * ``` |
| 56 | + * And the package exports something that matches a prefix of the access path above: |
| 57 | + * ```js |
| 58 | + * module.exports.blah = require('foo').x.y; |
| 59 | + * ``` |
| 60 | + * This would result in the following type model: |
| 61 | + * ``` |
| 62 | + * foo.XYZ; bar; Member[blah].Member[z] |
| 63 | + * ``` |
| 64 | + * Notice that the access path `Member[blah].Member[z]` consists of an access path generated from the API |
| 65 | + * graph, with pieces of the access path from the original type model appended to it. |
| 66 | + */ |
| 67 | +module TypeGraphExport< |
| 68 | + GraphExportSig<Specific::Location, API::Node> S, shouldContainTypeSig/1 shouldContainType> |
| 69 | +{ |
| 70 | + /** Like `shouldContainType` but includes types that lead to `type` via type models. */ |
| 71 | + private predicate shouldContainTypeEx(string type) { |
| 72 | + shouldContainType(type) |
| 73 | + or |
| 74 | + exists(string prevType | |
| 75 | + shouldContainType(prevType) and |
| 76 | + Shared::typeModel(prevType, type, _) |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + private module Config implements GraphExportSig<Specific::Location, API::Node> { |
| 81 | + import S |
| 82 | + |
| 83 | + predicate shouldContain(API::Node node) { |
| 84 | + S::shouldContain(node) |
| 85 | + or |
| 86 | + exists(string type1 | shouldContainTypeEx(type1) | |
| 87 | + ModelOutput::getATypeNode(type1).getAValueReachableFromSource() = node.asSink() |
| 88 | + or |
| 89 | + exists(string type2, string path | |
| 90 | + Shared::typeModel(type1, type2, path) and |
| 91 | + getNodeFromPath(type2, path, _).getAValueReachableFromSource() = node.asSink() |
| 92 | + ) |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private module ExportedGraph = GraphExport<Specific::Location, API::Node, Config>; |
| 98 | + |
| 99 | + import ExportedGraph |
| 100 | + |
| 101 | + /** |
| 102 | + * Holds if `type1, type2, path` should be emitted as a type model, that is `(type2, path)` leads to an instance of `type1`. |
| 103 | + */ |
| 104 | + predicate typeModel(string type1, string type2, string path) { |
| 105 | + ExportedGraph::typeModel(type1, type2, path) |
| 106 | + or |
| 107 | + shouldContainTypeEx(type1) and |
| 108 | + exists(API::Node node | |
| 109 | + // A relevant type is exported directly |
| 110 | + Specific::sourceFlowsToSink(ModelOutput::getATypeNode(type1), node) and |
| 111 | + ExportedGraph::pathToNode(type2, path, node) |
| 112 | + or |
| 113 | + // Something that leads to a relevant type, but didn't finish its access path, is exported |
| 114 | + exists(string midType, string midPath, string remainingPath, string prefix, API::Node source | |
| 115 | + Shared::typeModel(type1, midType, midPath) and |
| 116 | + partiallyEvaluatedModel(midType, midPath, source, remainingPath) and |
| 117 | + Specific::sourceFlowsToSink(source, node) and |
| 118 | + ExportedGraph::pathToNode(type2, prefix, node) and |
| 119 | + path = join(prefix, remainingPath) |
| 120 | + ) |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
0 commit comments