-
Notifications
You must be signed in to change notification settings - Fork 1.8k
JS: Recognize lazy-cache and property enumeration through libraries #2768
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
418f841
JS: Handle imports through lazy-cache
asgerf 34a9dce
JS: Detect property enumeration through for-own
asgerf c559ab1
JS: Add test and handle parameter with source object
asgerf f84af74
JS: Handle more libraries
asgerf a628f78
JS: Fix qldoc comment
asgerf 91a5385
JS: Add libraries to change note
asgerf 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 hidden or 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 hidden or 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 hidden or 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
55 changes: 55 additions & 0 deletions
55
javascript/ql/src/semmle/javascript/frameworks/LazyCache.qll
This file contains hidden or 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,55 @@ | ||
| /** | ||
| * Models imports through the NPM `lazy-cache` package. | ||
| */ | ||
|
|
||
| import javascript | ||
|
|
||
| module LazyCache { | ||
| /** | ||
| * A lazy-cache object, usually created through an expression of form `require('lazy-cache')(require)`. | ||
asgerf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| class LazyCacheObject extends DataFlow::SourceNode { | ||
| LazyCacheObject() { | ||
| // Use `require` directly instead of `moduleImport` to avoid recursion. | ||
| // For the same reason, avoid `Import.getImportedPath`. | ||
| exists(Require req | | ||
| req.getArgument(0).getStringValue() = "lazy-cache" and | ||
esbena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this = req.flow().(DataFlow::SourceNode).getAnInvocation() | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * An import through `lazy-cache`. | ||
| */ | ||
| class LazyCacheImport extends CallExpr, Import { | ||
| LazyCacheObject cache; | ||
|
|
||
| LazyCacheImport() { this = cache.getACall().asExpr() } | ||
esbena marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** Gets the name of the package as it's exposed on the lazy-cache object. */ | ||
| string getLocalAlias() { | ||
| result = getArgument(1).getStringValue() | ||
| or | ||
| not exists(getArgument(1)) and | ||
| result = getArgument(0).getStringValue() | ||
| } | ||
|
|
||
| override Module getEnclosingModule() { result = getTopLevel() } | ||
|
|
||
| override PathExpr getImportedPath() { result = getArgument(0) } | ||
|
|
||
| override DataFlow::Node getImportedModuleNode() { | ||
| result = this.flow() | ||
| or | ||
| result = cache.getAPropertyRead(getLocalAlias()) | ||
| } | ||
| } | ||
|
|
||
| /** A constant path element appearing in a call to a lazy-cache object. */ | ||
| private class LazyCachePathExpr extends PathExprInModule, ConstantString { | ||
| LazyCachePathExpr() { this = any(LazyCacheImport rp).getArgument(0) } | ||
|
|
||
| override string getValue() { result = getStringValue() } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the evaluation turns out fine, we should consider adding jQuery's forEach..
This particular predicate can probably be extended with a thousand more cases though...