Skip to content

Commit cb5ef7d

Browse files
committed
add basic support for jqXHR with ajax calls
1 parent b987f2c commit cb5ef7d

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,26 @@ private class JQueryAjaxCall extends ClientRequest::Range {
284284
not exists(getResponseType()) and responseType = ""
285285
) and
286286
promise = false and
287-
result =
288-
getOptionArgument([0 .. 1], "success")
289-
.getALocalSource()
290-
.(DataFlow::FunctionNode)
291-
.getParameter(0)
287+
(
288+
result =
289+
getOptionArgument([0 .. 1], "success")
290+
.getALocalSource()
291+
.(DataFlow::FunctionNode)
292+
.getParameter(0)
293+
or
294+
result = getAnAjaxCallbackDataNode(this)
295+
)
292296
}
293297
}
294298

299+
/**
300+
* Gets the response data node from a call to a jqXHR Object.
301+
*/
302+
DataFlow::Node getAnAjaxCallbackDataNode(ClientRequest::Range request) {
303+
result =
304+
request.getAMemberCall(any(string s | s = "done" or s = "then")).getCallback(0).getParameter(0)
305+
}
306+
295307
/**
296308
* A model of a URL request made using a `jQuery.ajax` shorthand.
297309
* E.g. `jQuery.getJSON`, `jQuery.post` etc.
@@ -332,7 +344,8 @@ private class JQueryAjaxShortHand extends ClientRequest::Range {
332344

333345
string getResponseType() {
334346
(name = "get" or name = "post") and
335-
getLastArgument().mayHaveStringValue(result)
347+
getLastArgument().mayHaveStringValue(result) and
348+
getNumArgument() > 1
336349
or
337350
name = "getJSON" and result = "json"
338351
or
@@ -348,7 +361,11 @@ private class JQueryAjaxShortHand extends ClientRequest::Range {
348361
) and
349362
promise = false and
350363
// one of the two last arguments
351-
result = getCallback([getNumArgument() - 2 .. getNumArgument() - 1]).getParameter(0)
364+
(
365+
result = getCallback([getNumArgument() - 2 .. getNumArgument() - 1]).getParameter(0)
366+
or
367+
result = getAnAjaxCallbackDataNode(this)
368+
)
352369
}
353370
}
354371

javascript/ql/test/library-tests/frameworks/ClientRequests/ClientRequests.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ test_ClientRequest
6262
| tst.js:185:2:185:60 | $( "#re ... lt) {}) |
6363
| tst.js:187:2:193:3 | $.ajax( ... on"\\n\\t}) |
6464
| tst.js:195:2:195:54 | $.get( ... "json") |
65+
| tst.js:197:2:197:45 | $.ajax( ... blob"}) |
66+
| tst.js:200:2:200:21 | $.get("example.php") |
6567
test_getADataNode
6668
| tst.js:53:5:53:23 | axios({data: data}) | tst.js:53:18:53:21 | data |
6769
| tst.js:57:5:57:39 | axios.p ... data2}) | tst.js:57:19:57:23 | data1 |
@@ -165,6 +167,8 @@ test_getUrl
165167
| tst.js:185:2:185:60 | $( "#re ... lt) {}) | tst.js:185:23:185:38 | "ajax/test.html" |
166168
| tst.js:187:2:193:3 | $.ajax( ... on"\\n\\t}) | tst.js:189:8:189:27 | "http://example.org" |
167169
| tst.js:195:2:195:54 | $.get( ... "json") | tst.js:195:9:195:24 | "ajax/test.json" |
170+
| tst.js:197:2:197:45 | $.ajax( ... blob"}) | tst.js:197:15:197:25 | "ajax/blob" |
171+
| tst.js:200:2:200:21 | $.get("example.php") | tst.js:200:8:200:20 | "example.php" |
168172
test_getAResponseDataNode
169173
| tst.js:19:5:19:23 | requestPromise(url) | tst.js:19:5:19:23 | requestPromise(url) | text | true |
170174
| tst.js:21:5:21:23 | superagent.get(url) | tst.js:21:5:21:23 | superagent.get(url) | stream | true |
@@ -221,3 +225,5 @@ test_getAResponseDataNode
221225
| tst.js:185:2:185:60 | $( "#re ... lt) {}) | tst.js:185:50:185:55 | result | text | false |
222226
| tst.js:187:2:193:3 | $.ajax( ... on"\\n\\t}) | tst.js:191:15:191:22 | ajaxData | json | false |
223227
| tst.js:195:2:195:54 | $.get( ... "json") | tst.js:195:37:195:40 | data | json | false |
228+
| tst.js:197:2:197:45 | $.ajax( ... blob"}) | tst.js:198:23:198:26 | data | blob | false |
229+
| tst.js:200:2:200:21 | $.get("example.php") | tst.js:200:37:200:44 | response | | false |

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,9 @@ import {ClientRequest, net} from 'electron';
193193
});
194194

195195
$.get( "ajax/test.json", function( data ) {}, "json");
196+
197+
$.ajax({url: "ajax/blob", dataType: "blob"})
198+
.done(function( data ) {});
199+
200+
$.get("example.php").done(function(response) {})
196201
});

0 commit comments

Comments
 (0)