Skip to content

Commit 8736535

Browse files
committed
remove spurious jQuery objects
1 parent 33f4503 commit 8736535

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

javascript/ql/src/semmle/javascript/frameworks/jQuery.qll

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,45 @@ private class OrdinaryJQueryObject extends JQueryObjectInternal {
3737
OrdinaryJQueryObject() {
3838
exists(JQuery::MethodCall jq |
3939
this.flow().getALocalSource() = jq and
40-
// `jQuery.val()` does _not_ return a jQuery object
41-
jq.getMethodName() != "val"
40+
returnsAJqueryObject(jq, jq.getMethodName())
4241
)
4342
}
4443
}
4544

45+
/**
46+
* Holds if the jQuery method call `call`, with name `methodName`, returns a JQuery object.
47+
*
48+
* The `call` parameter has type `DataFlow::CallNode` instead of `JQuery::MethodCall` to avoid non-monotonic recursion.
49+
* The not is placed inside the predicate to avoid non-monotonic recursion.
50+
*/
51+
bindingset[methodName, call]
52+
private predicate returnsAJqueryObject(DataFlow::CallNode call, string methodName) {
53+
not (
54+
methodName = "val" // `jQuery.val()`
55+
or
56+
methodName = ["html", "text"] and call.getNumArgument() = 0 // `jQuery.html()`/`jQuery.text()`
57+
or
58+
// `jQuery.attr(key)`/`jQuery.prop(key)`
59+
methodName = ["attr", "prop"] and
60+
call.getNumArgument() = 1 and
61+
call.getArgument(0).mayHaveStringValue(_)
62+
or
63+
// `jQuery.data()`
64+
methodName = "data" and call.getNumArgument() = 0
65+
or
66+
// `jQuery.data(key)`
67+
methodName = "data" and call.getNumArgument() = 1 and call.getArgument(0).mayHaveStringValue(_)
68+
or
69+
methodName = ["Event", "Deferred"] // $.Event / $.Deferred
70+
or
71+
methodName = "trim" // $.trim()
72+
or
73+
// `$.ajax`, and related methods.
74+
// note: there are 2 different `get` methods, and none of them return a jQuery object.
75+
methodName = ["ajax", "get", "getJSON", "getScript", "post", "load"]
76+
)
77+
}
78+
4679
/**
4780
* DEPRECATED. Use `JQuery::MethodCall` instead.
4881
*

javascript/ql/test/library-tests/frameworks/jQuery/JQueryMethodCall.expected

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ WARNING: Type JQueryMethodCall has been deprecated and may be removed in future
1717
| tst.js:5:1:5:15 | window.jQuery() | $ |
1818
| tst.js:6:1:6:32 | angular ... .attr() | attr |
1919
| tst.js:7:1:7:14 | $("<br>", doc) | $ |
20+
| tst.js:11:1:11:8 | $("foo") | $ |
21+
| tst.js:11:1:11:15 | $("foo").html() | html |
22+
| tst.js:12:1:12:8 | $("foo") | $ |
23+
| tst.js:12:1:12:20 | $("foo").html("foo") | html |
24+
| tst.js:12:1:12:31 | $("foo" ... query() | isJquery |
25+
| tst.js:13:1:13:8 | $("foo") | $ |
26+
| tst.js:13:1:13:17 | $("foo").data({}) | data |
27+
| tst.js:13:1:13:28 | $("foo" ... query() | isJquery |
28+
| tst.js:15:1:15:8 | $("foo") | $ |
29+
| tst.js:15:1:15:20 | $("foo").attr("bar") | attr |
30+
| tst.js:17:1:17:8 | $.trim() | trim |
31+
| tst.js:18:1:18:10 | $.ajax({}) | ajax |

javascript/ql/test/library-tests/frameworks/jQuery/interpretsArgumentAsHtml.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ WARNING: Type JQueryMethodCall has been deprecated and may be removed in future
88
| tst2.js:2:1:2:7 | jq("a") | tst2.js:2:4:2:6 | "a" |
99
| tst.js:3:1:3:9 | $("<a/>") | tst.js:3:3:3:8 | "<a/>" |
1010
| tst.js:7:1:7:14 | $("<br>", doc) | tst.js:7:3:7:8 | "<br>" |
11+
| tst.js:11:1:11:8 | $("foo") | tst.js:11:3:11:7 | "foo" |
12+
| tst.js:12:1:12:8 | $("foo") | tst.js:12:3:12:7 | "foo" |
13+
| tst.js:12:1:12:20 | $("foo").html("foo") | tst.js:12:15:12:19 | "foo" |
14+
| tst.js:13:1:13:8 | $("foo") | tst.js:13:3:13:7 | "foo" |
15+
| tst.js:15:1:15:8 | $("foo") | tst.js:15:3:15:7 | "foo" |

javascript/ql/test/library-tests/frameworks/jQuery/tst.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ window.$();
55
window.jQuery();
66
angular.element("<div/>").attr()
77
$("<br>", doc);
8+
9+
10+
11+
$("foo").html().doesNotReturnJquery();
12+
$("foo").html("foo").isJquery();
13+
$("foo").data({}).isJquery();
14+
15+
$("foo").attr("bar").doesNotReturnJquery();
16+
17+
$.trim().doesNotReturnJquery();
18+
$.ajax({}).doesNotReturnJquery()

0 commit comments

Comments
 (0)