Skip to content

Commit 267c4c0

Browse files
committed
refactor EventEmitter model to use the ::Range pattern
1 parent c4fd80d commit 267c4c0

File tree

2 files changed

+160
-128
lines changed

2 files changed

+160
-128
lines changed

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module Electron {
8989
/**
9090
* A model for the Main and Renderer process in an Electron app.
9191
*/
92-
abstract class Process extends EventEmitter::EventEmitter { }
92+
abstract class Process extends EventEmitter::EventEmitterRange::Range { }
9393

9494
/**
9595
* An instance of the Main process of an Electron app.
@@ -100,83 +100,78 @@ module Electron {
100100
}
101101

102102
/**
103-
* An instance of the renderer process of an Electron app.
103+
* An instance of the renderer process of an Electron app.
104104
*/
105105
class RendererProcess extends Process {
106106
RendererProcess() { this = renderer() }
107107
}
108-
108+
109109
/**
110-
* The `sender` property of the event in an IPC event handler.
110+
* The `sender` property of the event in an IPC event handler.
111111
* This sender is used to send a response back from the main process to the renderer.
112112
*/
113113
class ProcessSender extends Process {
114114
ProcessSender() {
115-
exists(IPCSendRegistration reg | reg.getEmitter() instanceof MainProcess |
116-
this = reg.getABoundCallbackParameter(1, 0).getAPropertyRead("sender")
115+
exists(IPCSendRegistration reg | reg.getEmitter() instanceof MainProcess |
116+
this = reg.getABoundCallbackParameter(1, 0).getAPropertyRead("sender")
117117
)
118118
}
119119
}
120-
120+
121121
/**
122122
* A registration of an Electron IPC event handler.
123-
* Does mostly the same as an EventEmitter event handler,
124-
* except that values can be returned through the `event.returnValue` property.
123+
* Does mostly the same as an EventEmitter event handler,
124+
* except that values can be returned through the `event.returnValue` property.
125125
*/
126-
class IPCSendRegistration extends EventEmitter::EventRegistration, DataFlow::MethodCallNode {
126+
class IPCSendRegistration extends EventEmitter::EventRegistration::Range,
127+
DataFlow::MethodCallNode {
127128
override Process emitter;
128-
129-
IPCSendRegistration() {
130-
this = emitter.ref().getAMethodCall("on")
131-
}
132-
133-
override string getChannel() {
134-
this.getArgument(0).mayHaveStringValue(result)
135-
}
136-
129+
130+
IPCSendRegistration() { this = emitter.ref().getAMethodCall("on") }
131+
132+
override string getChannel() { this.getArgument(0).mayHaveStringValue(result) }
133+
137134
override DataFlow::Node getEventHandlerParameter(int i) {
138-
result = this.getABoundCallbackParameter(1, i + 1)
135+
result = this.getABoundCallbackParameter(1, i + 1)
139136
}
140-
137+
141138
override DataFlow::Node getAReturnedValue() {
142139
result = this.getABoundCallbackParameter(1, 0).getAPropertyWrite("returnValue").getRhs()
143140
}
144-
141+
145142
override predicate canReturnTo(EventEmitter::EventDispatch dispatch) {
146143
dispatch.(DataFlow::InvokeNode).getCalleeName() = "sendSync"
147144
}
148145
}
149-
146+
150147
/**
151-
* A dispatch of an IPC event.
148+
* A dispatch of an IPC event.
152149
* An IPC event is sent from the Renderer to the Main process.
153-
* And a value can be returned through the `returnValue` property of the event (first parameter in the callback).
150+
* And a value can be returned through the `returnValue` property of the event (first parameter in the callback).
154151
*/
155-
class IPCDispatch extends EventEmitter::EventDispatch, DataFlow::InvokeNode {
152+
class IPCDispatch extends EventEmitter::EventDispatch::Range, DataFlow::InvokeNode {
156153
override Process emitter;
157-
154+
158155
IPCDispatch() {
159-
exists(string methodName | methodName = "sendSync" or methodName = "send" |
156+
exists(string methodName | methodName = "sendSync" or methodName = "send" |
160157
this = emitter.ref().getAMemberCall(methodName)
161158
)
162159
}
163-
164-
override string getChannel() {
165-
this.getArgument(0).mayHaveStringValue(result)
166-
}
167-
160+
161+
override string getChannel() { this.getArgument(0).mayHaveStringValue(result) }
162+
168163
/**
169-
* Gets the `i`th dispatched argument to the event handler.
170-
* The 0th parameter in the callback is a event generated by the IPC system,
171-
* therefore these arguments start at 1.
164+
* Gets the `i`th dispatched argument to the event handler.
165+
* The 0th parameter in the callback is a event generated by the IPC system,
166+
* therefore these arguments start at 1.
172167
*/
173168
override DataFlow::Node getDispatchedArgument(int i) {
174-
i >= 1 and
169+
i >= 1 and
175170
result = getArgument(i)
176171
}
177-
172+
178173
/**
179-
* Holds if this dispatch can send an event to the given EventRegistration destination.
174+
* Holds if this dispatch can send an event to the given EventRegistration destination.
180175
*/
181176
override predicate canSendTo(EventEmitter::EventRegistration destination) {
182177
this.getEmitter() instanceof RendererProcess and

0 commit comments

Comments
 (0)