Skip to content

Commit 862948f

Browse files
authored
Merge pull request github#12014 from erik-krogh/axios
JS: add support for axios used as a global variable
2 parents 25e703e + e3455a9 commit 862948f

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

javascript/ql/lib/semmle/javascript/frameworks/ClientRequests.qll

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,31 @@ module ClientRequest {
197197
/** Gets the string `url` or `uri`. */
198198
private string urlPropertyName() { result = "url" or result = "uri" }
199199

200+
/** An API entry-point for the global variable `axios`. */
201+
private class AxiosGlobalEntryPoint extends API::EntryPoint {
202+
AxiosGlobalEntryPoint() { this = "axiosGlobal" }
203+
204+
override DataFlow::SourceNode getASource() { result = DataFlow::globalVarRef("axios") }
205+
}
206+
207+
/** Gets a reference to the `axios` library. */
208+
private API::Node axios() {
209+
result = API::moduleImport("axios")
210+
or
211+
result = API::root().getASuccessor(API::Label::entryPoint(any(AxiosGlobalEntryPoint entry)))
212+
}
213+
200214
/**
201215
* A model of a URL request made using the `axios` library.
202216
*/
203217
class AxiosUrlRequest extends ClientRequest::Range, API::CallNode {
204218
string method;
205219

206220
AxiosUrlRequest() {
207-
this = API::moduleImport("axios").getACall() and method = "request"
221+
this = axios().getACall() and
222+
method = "request"
208223
or
209-
this = API::moduleImport("axios").getMember(method).getACall() and
224+
this = axios().getMember(method).getACall() and
210225
method = [httpMethodName(), "request"]
211226
}
212227

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ test_ClientRequest
55
| apollo.js:17:1:17:34 | new Pre ... yurl"}) |
66
| apollo.js:20:1:20:77 | createN ... phql'}) |
77
| apollo.js:23:1:23:31 | new Web ... wsUri}) |
8+
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) |
9+
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) |
810
| puppeteer.ts:6:11:6:42 | page.go ... e.com') |
911
| puppeteer.ts:8:5:8:61 | page.ad ... css" }) |
1012
| puppeteer.ts:18:30:18:50 | page.go ... estUrl) |
@@ -90,6 +92,8 @@ test_ClientRequest
9092
| tst.js:296:5:299:6 | axios({ ... \\n }) |
9193
| tst.js:312:12:312:36 | fetchPo ... o/bar') |
9294
test_getADataNode
95+
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:15:18:15:55 | { 'Cont ... json' } |
96+
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:16:15:16:35 | {x: 'te ... 'test'} |
9397
| tst.js:53:5:53:23 | axios({data: data}) | tst.js:53:18:53:21 | data |
9498
| tst.js:57:5:57:39 | axios.p ... data2}) | tst.js:57:19:57:23 | data1 |
9599
| tst.js:57:5:57:39 | axios.p ... data2}) | tst.js:57:33:57:37 | data2 |
@@ -143,6 +147,10 @@ test_getUrl
143147
| apollo.js:17:1:17:34 | new Pre ... yurl"}) | apollo.js:17:26:17:32 | "myurl" |
144148
| apollo.js:20:1:20:77 | createN ... phql'}) | apollo.js:20:30:20:75 | 'https: ... raphql' |
145149
| apollo.js:23:1:23:31 | new Web ... wsUri}) | apollo.js:23:25:23:29 | wsUri |
150+
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:4:11:7:5 | {\\n ... ,\\n } |
151+
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:6:14:6:16 | url |
152+
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:12:11:17:5 | {\\n ... }\\n } |
153+
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:14:14:14:16 | url |
146154
| puppeteer.ts:6:11:6:42 | page.go ... e.com') | puppeteer.ts:6:21:6:41 | 'https: ... le.com' |
147155
| puppeteer.ts:8:5:8:61 | page.ad ... css" }) | puppeteer.ts:8:29:8:58 | "http:/ ... le.css" |
148156
| puppeteer.ts:18:30:18:50 | page.go ... estUrl) | puppeteer.ts:18:40:18:49 | requestUrl |
@@ -233,6 +241,8 @@ test_getUrl
233241
| tst.js:296:5:299:6 | axios({ ... \\n }) | tst.js:298:14:298:44 | "http:/ ... -axios" |
234242
| tst.js:312:12:312:36 | fetchPo ... o/bar') | tst.js:312:26:312:35 | '/foo/bar' |
235243
test_getAResponseDataNode
244+
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:4:5:7:6 | axios({ ... \\n }) | json | true |
245+
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:12:5:17:6 | axios({ ... \\n }) | json | true |
236246
| tst.js:19:5:19:23 | requestPromise(url) | tst.js:19:5:19:23 | requestPromise(url) | text | true |
237247
| tst.js:21:5:21:23 | superagent.get(url) | tst.js:21:5:21:23 | superagent.get(url) | stream | true |
238248
| tst.js:25:5:25:14 | axios(url) | tst.js:25:5:25:14 | axios(url) | | true |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
//Use of axios as a global variable instead of an imported module to make Ajax calls
3+
var testvar = function () {
4+
axios({
5+
method: 'get',
6+
url: url,
7+
}).then(function (response) {
8+
console.log(response.data) })
9+
10+
11+
12+
axios({
13+
method: 'post',
14+
url: url,
15+
headers: { 'Content-Type': 'application/json' },
16+
data: {x: 'test', y:'test'}
17+
}).then(function (response) {
18+
console.log(response.data) })
19+
}
20+
21+

0 commit comments

Comments
 (0)