Skip to content

Commit 2d6f5d1

Browse files
committed
Refactor ClientRequest to introduce GotInstance classes for improved handling of got instances and options retrieval.
1 parent f43510c commit 2d6f5d1

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,20 +415,51 @@ module ClientRequest {
415415
}
416416

417417
/**
418-
* Gets a reference to an instance of the `got` library, including instances
419-
* created through chained `extend` calls.
418+
* Represents an instance of the `got` HTTP client library.
420419
*/
421-
private API::Node getAGotInstance() {
422-
result = [API::moduleImport("got"), getAGotInstance().getMember("extend").getReturn()]
420+
abstract private class GotInstance extends API::Node {
421+
/**
422+
* Gets the options object associated with this instance of `got`.
423+
*/
424+
API::Node getOptions() { none() }
425+
}
426+
427+
/**
428+
* Represents the root `got` module import.
429+
* For example: `const got = require('got')`.
430+
*/
431+
private class RootGotInstance extends GotInstance {
432+
RootGotInstance() { this = API::moduleImport("got") }
433+
}
434+
435+
/**
436+
* Represents an instance of `got` created by calling the `extend()` method.
437+
* It may also be chained with multiple calls to `extend()`.
438+
*
439+
* For example: `const client = got.extend({ prefixUrl: 'https://example.com' })`.
440+
*/
441+
private class ExtendGotInstance extends GotInstance {
442+
private GotInstance base;
443+
private API::CallNode extendCall;
444+
445+
ExtendGotInstance() {
446+
extendCall = base.getMember("extend").getACall() and
447+
this = extendCall.getReturn()
448+
}
449+
450+
override API::Node getOptions() {
451+
result = extendCall.getParameter(0) or result = base.getOptions()
452+
}
423453
}
424454

425455
/**
426456
* A model of a URL request made using the `got` library.
427457
*/
428458
class GotUrlRequest extends ClientRequest::Range {
459+
GotInstance got;
460+
429461
GotUrlRequest() {
430-
exists(API::Node callee, API::Node got | this = callee.getACall() |
431-
got = getAGotInstance() and
462+
exists(API::Node callee | this = callee.getACall() |
432463
callee =
433464
[
434465
got,
@@ -442,11 +473,8 @@ module ClientRequest {
442473
not exists(this.getOptionArgument(1, "baseUrl"))
443474
or
444475
// Handle URL from options passed to extend()
445-
exists(API::CallNode extendCall |
446-
extendCall = API::moduleImport("got").getMember("extend").getACall() and
447-
result = extendCall.getParameter(0).getMember("url").asSink() and
448-
not exists(this.getArgument(0))
449-
)
476+
result = got.getOptions().getMember("url").asSink() and
477+
not exists(this.getArgument(0))
450478
or
451479
// Handle URL from options passed as third argument when first arg is undefined/missing
452480
exists(API::InvokeNode optionsCall |

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,10 @@ test_getUrl
264264
| tst.js:328:5:328:38 | got(und ... ptions) | tst.js:328:9:328:17 | undefined |
265265
| tst.js:329:5:329:45 | got(und ... {url})) | tst.js:329:9:329:17 | undefined |
266266
| tst.js:329:5:329:45 | got(und ... {url})) | tst.js:329:40:329:42 | url |
267-
| tst.js:332:5:332:46 | got.ext ... ).get() | tst.js:336:41:336:43 | url |
268-
| tst.js:332:5:332:46 | got.ext ... ).get() | tst.js:339:42:339:44 | url |
269267
| tst.js:334:5:334:25 | got.pag ... rl, {}) | tst.js:334:18:334:20 | url |
270268
| tst.js:337:5:337:20 | jsonClient.get() | tst.js:336:41:336:43 | url |
271-
| tst.js:337:5:337:20 | jsonClient.get() | tst.js:339:42:339:44 | url |
272-
| tst.js:340:5:340:21 | jsonClient2.get() | tst.js:336:41:336:43 | url |
273269
| tst.js:340:5:340:21 | jsonClient2.get() | tst.js:339:42:339:44 | url |
270+
| tst.js:340:5:340:21 | jsonClient2.get() | tst.js:339:61:339:63 | url |
274271
test_getAResponseDataNode
275272
| axiosTest.js:4:5:7:6 | axios({ ... \\n }) | axiosTest.js:4:5:7:6 | axios({ ... \\n }) | json | true |
276273
| axiosTest.js:12:5:17:6 | axios({ ... \\n }) | axiosTest.js:12:5:17:6 | axios({ ... \\n }) | json | true |

0 commit comments

Comments
 (0)