-
Notifications
You must be signed in to change notification settings - Fork 1.9k
JS: add method name injection query #503
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
xiemaisi
merged 18 commits into
github:rc/1.19
from
asger-semmle:unsafe-global-object-access
Nov 26, 2018
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
bc3b983
JS: move CodeInjection tests into subfolder
asger-semmle 2239f86
JS: add query MethodNameInjection
asger-semmle a2e5003
JS: add to security suite
asger-semmle 8aff666
JS: suppress similar alerts from RemotePropertyInjection
asger-semmle 1c06f45
JS: address some comments
asger-semmle 49cd287
JS: use StringConcatenation library in ConcatSanitizer
asger-semmle b16072a
JS: share ConcatSanitizer in common module
asger-semmle 3902f75
JS: share detection of objects with unsafe methods
asger-semmle 260ae36
JS: document the shared module
asger-semmle 4138f81
JS: expand example
asger-semmle 7d80847
JS: add qhelp example to test suite
asger-semmle fa761c0
Update javascript/ql/src/Security/CWE-094/MethodNameInjection.ql
84d6426
JS: more comments
asger-semmle cb832b1
Merge branch 'unsafe-global-object-access' of github.com:asger-semmle…
asger-semmle 4ae2493
JS: rename query to Unsafe Dynamic Method Access
asger-semmle 8c7e195
JS: fix string value of taint configuration
asger-semmle 27c9326
JS: address doc review
asger-semmle 61ef655
JS: handle both data() and taint() source labels
asger-semmle 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
53 changes: 53 additions & 0 deletions
53
javascript/ql/src/Security/CWE-094/UnsafeDynamicMethodAccess.qhelp
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,53 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
|
|
||
| <overview> | ||
| <p> | ||
| Calling a user-controlled method on certain objects can lead to invocation of unsafe functions, | ||
| such as <code>eval</code> or the <code>Function</code> constructor. In particular, the global object | ||
| contains the <code>eval</code> function, and any function object contains the <code>Function</code> constructor | ||
| in its <code>constructor</code> property. | ||
| </p> | ||
| </overview> | ||
|
|
||
| <recommendation> | ||
| <p> | ||
| Avoid invoking user-controlled methods on the global object or on any function object. | ||
| Whitelist the permitted method names or change the type of object the methods are stored on. | ||
| </p> | ||
| </recommendation> | ||
|
|
||
| <example> | ||
| <p> | ||
| In the following example, a message from the document's parent frame can invoke the <code>play</code> | ||
| or <code>pause</code> method. However, it can also invoke <code>eval</code>. | ||
| A malicious website could embed the page in an iframe and execute arbitrary code by sending a message | ||
| with the name <code>eval</code>. | ||
| </p> | ||
|
|
||
| <sample src="examples/UnsafeDynamicMethodAccess.js" /> | ||
|
|
||
| <p> | ||
| Instead of storing the API methods in the global scope, put them in an API object or Map. It is also good | ||
| practice to prevent invocation of inherited methods like <code>toString</code> and <code>valueOf</code>. | ||
| </p> | ||
|
|
||
| <sample src="examples/UnsafeDynamicMethodAccessGood.js" /> | ||
|
|
||
| </example> | ||
|
|
||
| <references> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://www.owasp.org/index.php/Code_Injection">Code Injection</a>. | ||
| </li> | ||
| <li> | ||
| MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects#Function_properties">Global functions</a>. | ||
| </li> | ||
| <li> | ||
| MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function constructor</a>. | ||
| </li> | ||
| </references> | ||
| </qhelp> | ||
17 changes: 17 additions & 0 deletions
17
javascript/ql/src/Security/CWE-094/UnsafeDynamicMethodAccess.ql
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,17 @@ | ||
| /** | ||
| * @name Unsafe dynamic method access | ||
| * @description Invoking user-controlled methods on certain objects can lead to remote code execution. | ||
| * @kind path-problem | ||
| * @problem.severity error | ||
| * @precision high | ||
| * @id js/unsafe-dynamic-method-access | ||
| * @tags security | ||
| * external/cwe/cwe-094 | ||
| */ | ||
| import javascript | ||
| import semmle.javascript.security.dataflow.UnsafeDynamicMethodAccess::UnsafeDynamicMethodAccess | ||
| import DataFlow::PathGraph | ||
|
|
||
| from Configuration cfg, DataFlow::PathNode source, DataFlow::PathNode sink | ||
| where cfg.hasFlowPath(source, sink) | ||
| select sink, source, sink, "Invocation of method derived from $@ may lead to remote code execution.", source.getNode(), "user-controlled value" |
14 changes: 14 additions & 0 deletions
14
javascript/ql/src/Security/CWE-094/examples/UnsafeDynamicMethodAccess.js
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,14 @@ | ||
| // API methods | ||
| function play(data) { | ||
| // ... | ||
| } | ||
| function pause(data) { | ||
| // ... | ||
| } | ||
|
|
||
| window.addEventListener("message", (ev) => { | ||
| let message = JSON.parse(ev.data); | ||
|
|
||
| // Let the parent frame call the 'play' or 'pause' function | ||
| window[message.name](message.payload); | ||
| }); |
19 changes: 19 additions & 0 deletions
19
javascript/ql/src/Security/CWE-094/examples/UnsafeDynamicMethodAccessGood.js
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,19 @@ | ||
| // API methods | ||
| let api = { | ||
| play: function(data) { | ||
| // ... | ||
| }, | ||
| pause: function(data) { | ||
| // ... | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener("message", (ev) => { | ||
| let message = JSON.parse(ev.data); | ||
|
|
||
| // Let the parent frame call the 'play' or 'pause' function | ||
| if (!api.hasOwnProperty(message.name)) { | ||
| return; | ||
| } | ||
| api[message.name](message.payload); | ||
| }); |
39 changes: 39 additions & 0 deletions
39
javascript/ql/src/semmle/javascript/security/dataflow/PropertyInjectionShared.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,39 @@ | ||
| /** | ||
| * Provides predicates for reasoning about flow of user-controlled values that are used | ||
| * as property names. | ||
| */ | ||
| import javascript | ||
|
|
||
| module PropertyInjection { | ||
| /** | ||
| * A data-flow node that sanitizes user-controlled property names that flow through it. | ||
| */ | ||
| abstract class Sanitizer extends DataFlow::Node { | ||
| } | ||
|
|
||
| /** | ||
| * Concatenation with a constant, acting as a sanitizer. | ||
| */ | ||
| private class ConcatSanitizer extends Sanitizer { | ||
| ConcatSanitizer() { | ||
| StringConcatenation::getAnOperand(this).asExpr() instanceof ConstantString | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the methods of the given value are unsafe, such as `eval`. | ||
| */ | ||
| predicate hasUnsafeMethods(DataFlow::SourceNode node) { | ||
| // eval and friends can be accessed from the global object. | ||
| node = DataFlow::globalObjectRef() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
| or | ||
| // document.write can be accessed | ||
| isDocument(node.asExpr()) | ||
| or | ||
| // 'constructor' property leads to the Function constructor. | ||
| node.analyze().getAValue() instanceof AbstractCallable | ||
| or | ||
| // Assume that a value that is invoked can refer to a function. | ||
| exists (node.getAnInvocation()) | ||
| } | ||
| } | ||
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
129 changes: 129 additions & 0 deletions
129
javascript/ql/src/semmle/javascript/security/dataflow/UnsafeDynamicMethodAccess.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,129 @@ | ||
| /** | ||
| * Provides a taint-tracking configuration for reasoning about method invocations | ||
| * with a user-controlled method name on objects with unsafe methods. | ||
| */ | ||
|
|
||
| import javascript | ||
| import semmle.javascript.frameworks.Express | ||
| import PropertyInjectionShared | ||
|
|
||
| module UnsafeDynamicMethodAccess { | ||
| private import DataFlow::FlowLabel | ||
|
|
||
| /** | ||
| * A data flow source for unsafe dynamic method access. | ||
| */ | ||
| abstract class Source extends DataFlow::Node { | ||
| /** | ||
| * Gets the flow label relevant for this source. | ||
| */ | ||
| DataFlow::FlowLabel getFlowLabel() { | ||
| result = data() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A data flow sink for unsafe dynamic method access. | ||
| */ | ||
| abstract class Sink extends DataFlow::Node { | ||
| /** | ||
| * Gets the flow label relevant for this sink | ||
| */ | ||
| abstract DataFlow::FlowLabel getFlowLabel(); | ||
| } | ||
|
|
||
| /** | ||
| * A sanitizer for unsafe dynamic method access. | ||
| */ | ||
| abstract class Sanitizer extends DataFlow::Node { } | ||
|
|
||
| /** | ||
| * Gets the flow label describing values that may refer to an unsafe | ||
| * function as a result of an attacker-controlled property name. | ||
| */ | ||
| UnsafeFunction unsafeFunction() { any() } | ||
| private class UnsafeFunction extends DataFlow::FlowLabel { | ||
| UnsafeFunction() { this = "UnsafeFunction" } | ||
| } | ||
|
|
||
| /** | ||
| * A taint-tracking configuration for reasoning about unsafe dynamic method access. | ||
| */ | ||
| class Configuration extends TaintTracking::Configuration { | ||
| Configuration() { this = "UnsafeDynamicMethodAccess" } | ||
|
|
||
| override predicate isSource(DataFlow::Node source, DataFlow::FlowLabel label) { | ||
| source.(Source).getFlowLabel() = label | ||
| } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink, DataFlow::FlowLabel label) { | ||
| sink.(Sink).getFlowLabel() = label | ||
| } | ||
|
|
||
| override predicate isSanitizer(DataFlow::Node node) { | ||
| super.isSanitizer(node) or | ||
| node instanceof Sanitizer or | ||
| node instanceof PropertyInjection::Sanitizer | ||
| } | ||
|
|
||
| /** | ||
| * Holds if a property of the given object is an unsafe function. | ||
| */ | ||
| predicate hasUnsafeMethods(DataFlow::SourceNode node) { | ||
| PropertyInjection::hasUnsafeMethods(node) // Redefined here so custom queries can override it | ||
| } | ||
|
|
||
| /** | ||
| * Holds if the `node` is of form `Object.create(null)` and so it has no prototype. | ||
| */ | ||
| predicate isPrototypeLessObject(DataFlow::MethodCallNode node) { | ||
| node = DataFlow::globalVarRef("Object").getAMethodCall("create") and | ||
| node.getArgument(0).asExpr() instanceof NullLiteral | ||
| } | ||
|
|
||
| override predicate isAdditionalFlowStep(DataFlow::Node src, DataFlow::Node dst, DataFlow::FlowLabel srclabel, DataFlow::FlowLabel dstlabel) { | ||
| // Reading a property of the global object or of a function | ||
| exists (DataFlow::PropRead read | | ||
| hasUnsafeMethods(read.getBase().getALocalSource()) and | ||
| src = read.getPropertyNameExpr().flow() and | ||
| dst = read and | ||
| (srclabel = data() or srclabel = taint()) and | ||
| dstlabel = unsafeFunction()) | ||
| or | ||
| // Reading a chain of properties from any object with a prototype can lead to Function | ||
| exists (PropertyProjection proj | | ||
| not isPrototypeLessObject(proj.getObject().getALocalSource()) and | ||
| src = proj.getASelector() and | ||
| dst = proj and | ||
| (srclabel = data() or srclabel = taint()) and | ||
| dstlabel = unsafeFunction()) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A source of remote user input, considered as a source for unsafe dynamic method access. | ||
| */ | ||
| class RemoteFlowSourceAsSource extends Source { | ||
| RemoteFlowSourceAsSource() { this instanceof RemoteFlowSource } | ||
| } | ||
|
|
||
| /** | ||
| * The page URL considered as a flow source for unsafe dynamic method access. | ||
| */ | ||
| class DocumentUrlAsSource extends Source { | ||
| DocumentUrlAsSource() { isDocumentURL(asExpr()) } | ||
| } | ||
|
|
||
| /** | ||
| * A function invocation of an unsafe function, as a sink for remote unsafe dynamic method access. | ||
| */ | ||
| class CalleeAsSink extends Sink { | ||
| CalleeAsSink() { | ||
| this = any(DataFlow::InvokeNode node).getCalleeNode() | ||
| } | ||
|
|
||
| override DataFlow::FlowLabel getFlowLabel() { | ||
| result = unsafeFunction() | ||
| } | ||
| } | ||
| } |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.
Sorry I don't understand this sentence (
In particular, the global object contains the <code>eval</code> function, and any function object contains the <code>Function</code> constructor in its <code>constructor</code> property.) and how it links to what you've said previously. I am not a developer so feel free to ignore if you think this is clear enough to the intended audience.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.
It's a technical explanation of what was meant by "certain objects" in the previous sentence. I think it would make sense to most JavaScript developers.