Skip to content

Commit d428eae

Browse files
authored
Merge pull request #19655 from GeekMasher/js-clientrests-axios
JS: ClientRequests Axios Instance support
2 parents 676289e + 9d23677 commit d428eae

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added support for Axios instances in the `axios` module.

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

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ module ClientRequest {
254254
method = "request" and
255255
result = this.getOptionArgument(0, "data")
256256
or
257-
method = ["post", "put"] and
257+
method = ["post", "put", "patch"] and
258258
result = [this.getArgument(1), this.getOptionArgument(2, "data")]
259259
or
260260
method = ["postForm", "putForm", "patchForm"] and result = this.getArgument(1)
@@ -289,6 +289,69 @@ module ClientRequest {
289289
}
290290
}
291291

292+
/**
293+
* A model of a `axios` instance request.
294+
*/
295+
class AxiosInstanceRequest extends ClientRequest::Range, API::CallNode {
296+
string method;
297+
API::CallNode instance;
298+
299+
// Instances of axios, e.g. `axios.create({ ... })`
300+
AxiosInstanceRequest() {
301+
instance = axios().getMember(["create", "createInstance"]).getACall() and
302+
method = [httpMethodName(), "request", "postForm", "putForm", "patchForm", "getUri"] and
303+
this = instance.getReturn().getMember(method).getACall()
304+
}
305+
306+
private int getOptionsArgIndex() {
307+
(method = "get" or method = "delete" or method = "head") and
308+
result = 0
309+
or
310+
(method = "post" or method = "put" or method = "patch") and
311+
result = 1
312+
}
313+
314+
private DataFlow::Node getOptionArgument(string name) {
315+
result = this.getOptionArgument(this.getOptionsArgIndex(), name)
316+
}
317+
318+
override DataFlow::Node getUrl() {
319+
result = this.getArgument(0) or
320+
result = this.getOptionArgument(urlPropertyName())
321+
}
322+
323+
override DataFlow::Node getHost() { result = instance.getOptionArgument(0, "baseURL") }
324+
325+
override DataFlow::Node getADataNode() {
326+
method = ["post", "put", "patch"] and
327+
result = [this.getArgument(1), this.getOptionArgument(2, "data")]
328+
or
329+
method = ["postForm", "putForm", "patchForm"] and result = this.getArgument(1)
330+
or
331+
result = this.getOptionArgument([0 .. 2], ["headers", "params"])
332+
}
333+
334+
/** Gets the response type from the options passed in. */
335+
string getResponseType() {
336+
exists(DataFlow::Node option | option = instance.getOptionArgument(0, "responseType") |
337+
option.mayHaveStringValue(result)
338+
)
339+
or
340+
not exists(this.getOptionArgument("responseType")) and
341+
result = "json"
342+
}
343+
344+
override DataFlow::Node getAResponseDataNode(string responseType, boolean promise) {
345+
responseType = this.getResponseType() and
346+
promise = true and
347+
result = this
348+
or
349+
responseType = this.getResponseType() and
350+
promise = false and
351+
result = this.getReturn().getPromisedError().getMember("response").asSource()
352+
}
353+
}
354+
292355
/** An expression that is used as a credential in a request. */
293356
private class AuthorizationHeader extends CredentialsNode {
294357
AuthorizationHeader() {

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

Lines changed: 9 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+
| axios.ts:14:32:14:65 | api.get ... repo}`) |
9+
| axios.ts:25:32:25:73 | api.pat ... , data) |
810
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) |
911
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) |
1012
| puppeteer.ts:6:11:6:42 | page.go ... e.com') |
@@ -111,6 +113,7 @@ test_ClientRequest
111113
| tst.js:349:5:349:30 | axios.g ... url }) |
112114
| tst.js:352:5:352:66 | axiosIn ... text"}) |
113115
test_getADataNode
116+
| axios.ts:25:32:25:73 | api.pat ... , data) | axios.ts:25:69:25:72 | data |
114117
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:15:18:15:55 | { 'Cont ... json' } |
115118
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:16:15:16:35 | {x: 'te ... 'test'} |
116119
| superagent.js:6:5:6:32 | superag ... st(url) | superagent.js:6:39:6:42 | data |
@@ -159,6 +162,8 @@ test_getADataNode
159162
| tst.js:347:5:347:30 | axios.p ... , data) | tst.js:347:26:347:29 | data |
160163
| tst.js:348:5:348:38 | axios.p ... config) | tst.js:348:26:348:29 | data |
161164
test_getHost
165+
| axios.ts:14:32:14:65 | api.get ... repo}`) | axios.ts:4:14:4:37 | "https: ... ub.com" |
166+
| axios.ts:25:32:25:73 | api.pat ... , data) | axios.ts:4:14:4:37 | "https: ... ub.com" |
162167
| tst.js:87:5:87:39 | http.ge ... host}) | tst.js:87:34:87:37 | host |
163168
| tst.js:89:5:89:23 | axios({host: host}) | tst.js:89:18:89:21 | host |
164169
| tst.js:91:5:91:34 | got(rel ... host}) | tst.js:91:29:91:32 | host |
@@ -173,6 +178,8 @@ test_getUrl
173178
| apollo.js:17:1:17:34 | new Pre ... yurl"}) | apollo.js:17:26:17:32 | "myurl" |
174179
| apollo.js:20:1:20:77 | createN ... phql'}) | apollo.js:20:30:20:75 | 'https: ... raphql' |
175180
| apollo.js:23:1:23:31 | new Web ... wsUri}) | apollo.js:23:25:23:29 | wsUri |
181+
| axios.ts:14:32:14:65 | api.get ... repo}`) | axios.ts:14:40:14:64 | `/repos ... {repo}` |
182+
| axios.ts:25:32:25:73 | api.pat ... , data) | axios.ts:25:42:25:66 | `/repos ... {repo}` |
176183
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:4:11:7:5 | {\\n ... ,\\n } |
177184
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:6:14:6:16 | url |
178185
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:12:11:17:5 | {\\n ... }\\n } |
@@ -289,6 +296,8 @@ test_getUrl
289296
| tst.js:352:5:352:66 | axiosIn ... text"}) | tst.js:352:19:352:65 | {method ... "text"} |
290297
| tst.js:352:5:352:66 | axiosIn ... text"}) | tst.js:352:40:352:42 | url |
291298
test_getAResponseDataNode
299+
| axios.ts:14:32:14:65 | api.get ... repo}`) | axios.ts:14:32:14:65 | api.get ... repo}`) | json | true |
300+
| axios.ts:25:32:25:73 | api.pat ... , data) | axios.ts:25:32:25:73 | api.pat ... , data) | json | true |
292301
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:4:5:7:6 | axios({ ... \\n }) | json | true |
293302
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:12:5:17:6 | axios({ ... \\n }) | json | true |
294303
| superagent.js:4:5:4:26 | superag ... ', url) | superagent.js:4:5:4:26 | superag ... ', url) | stream | true |
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import axios from "axios";
2+
3+
let api = axios.create({
4+
baseURL: "https://api.github.com",
5+
timeout: 1000,
6+
responseType: "json",
7+
headers: { "X-Custom-Header": "foobar" }
8+
});
9+
10+
export default api;
11+
12+
export async function getRepo(owner: string, repo: string) {
13+
try {
14+
const response = await api.get(`/repos/${owner}/${repo}`);
15+
console.log("Repository data:", response.data);
16+
return response.data;
17+
} catch (error) {
18+
console.error("Error fetching repo:", error);
19+
throw error;
20+
}
21+
}
22+
23+
export async function updateUser(owner: string, repo: string, data: any) {
24+
try {
25+
const response = await api.patch(`/repos/${owner}/${repo}`, data);
26+
console.log("User updated:", response.data);
27+
return response.data;
28+
} catch (error) {
29+
console.error("Error updating user:", error);
30+
throw error;
31+
}
32+
}

0 commit comments

Comments
 (0)