-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ruby: prototype instantiation of graph export #16165
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
ruby/ql/lib/codeql/ruby/frameworks/data/internal/ApiGraphModelsExport.qll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/** | ||
* Contains an extension of `GraphExport` that relies on API graph specific functionality. | ||
*/ | ||
|
||
private import ApiGraphModels as Shared | ||
private import codeql.mad.dynamic.GraphExport | ||
private import ApiGraphModelsSpecific as Specific | ||
|
||
private module API = Specific::API; | ||
|
||
private import Shared | ||
|
||
/** | ||
* Holds if some proper prefix of `(type, path)` evaluated to `node`, where `remainingPath` | ||
* is bound to the suffix of `path` that was not evaluated yet. | ||
*/ | ||
bindingset[type, path] | ||
predicate partiallyEvaluatedModel(string type, string path, API::Node node, string remainingPath) { | ||
exists(int n, AccessPath accessPath | | ||
accessPath = path and | ||
getNodeFromPath(type, accessPath, n) = node and | ||
n > 0 and | ||
// Note that `n < accessPath.getNumToken()` is implied by the use of strictconcat() | ||
remainingPath = | ||
strictconcat(int k | | ||
k = [n .. accessPath.getNumToken() - 1] | ||
| | ||
accessPath.getToken(k), "." order by k | ||
) | ||
) | ||
} | ||
|
||
/** | ||
* Holds if `type` and all types leading to `type` should be re-exported. | ||
*/ | ||
signature predicate shouldContainTypeSig(string type); | ||
|
||
/** | ||
* Wrapper around `GraphExport` that also exports information about re-exported types. | ||
* | ||
* ### JavaScript example 1 | ||
* For example, suppose `shouldContainType("foo")` holds, and the following is the entry point for a package `bar`: | ||
* ```js | ||
* // bar.js | ||
* module.exports.xxx = require('foo'); | ||
* ``` | ||
* then this would generate the following type model: | ||
* ``` | ||
* foo; bar; Member[xxx] | ||
* ``` | ||
* | ||
* ### JavaScript example 2 | ||
* For a more complex case, suppose the following type model exists: | ||
* ``` | ||
* foo.XYZ; foo; Member[x].Member[y].Member[z] | ||
* ``` | ||
* And the package exports something that matches a prefix of the access path above: | ||
* ```js | ||
* module.exports.blah = require('foo').x.y; | ||
* ``` | ||
* This would result in the following type model: | ||
* ``` | ||
* foo.XYZ; bar; Member[blah].Member[z] | ||
* ``` | ||
* Notice that the access path `Member[blah].Member[z]` consists of an access path generated from the API | ||
* graph, with pieces of the access path from the original type model appended to it. | ||
*/ | ||
module TypeGraphExport< | ||
GraphExportSig<Specific::Location, API::Node> S, shouldContainTypeSig/1 shouldContainType> | ||
{ | ||
/** Like `shouldContainType` but includes types that lead to `type` via type models. */ | ||
private predicate shouldContainTypeEx(string type) { | ||
shouldContainType(type) | ||
or | ||
exists(string prevType | | ||
shouldContainType(prevType) and | ||
Shared::typeModel(prevType, type, _) | ||
) | ||
} | ||
|
||
private module Config implements GraphExportSig<Specific::Location, API::Node> { | ||
import S | ||
|
||
predicate shouldContain(API::Node node) { | ||
S::shouldContain(node) | ||
or | ||
exists(string type1 | shouldContainTypeEx(type1) | | ||
ModelOutput::getATypeNode(type1).getAValueReachableFromSource() = node.asSink() | ||
or | ||
exists(string type2, string path | | ||
Shared::typeModel(type1, type2, path) and | ||
getNodeFromPath(type2, path, _).getAValueReachableFromSource() = node.asSink() | ||
) | ||
) | ||
} | ||
} | ||
|
||
private module ExportedGraph = GraphExport<Specific::Location, API::Node, Config>; | ||
|
||
import ExportedGraph | ||
|
||
/** | ||
* Holds if `type1, type2, path` should be emitted as a type model, that is `(type2, path)` leads to an instance of `type1`. | ||
*/ | ||
predicate typeModel(string type1, string type2, string path) { | ||
ExportedGraph::typeModel(type1, type2, path) | ||
or | ||
shouldContainTypeEx(type1) and | ||
exists(API::Node node | | ||
// A relevant type is exported directly | ||
Specific::sourceFlowsToSink(ModelOutput::getATypeNode(type1), node) and | ||
ExportedGraph::pathToNode(type2, path, node) | ||
or | ||
// Something that leads to a relevant type, but didn't finish its access path, is exported | ||
exists(string midType, string midPath, string remainingPath, string prefix, API::Node source | | ||
Shared::typeModel(type1, midType, midPath) and | ||
partiallyEvaluatedModel(midType, midPath, source, remainingPath) and | ||
Specific::sourceFlowsToSink(source, node) and | ||
ExportedGraph::pathToNode(type2, prefix, node) and | ||
path = join(prefix, remainingPath) | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Dead code Warning