Skip to content

Commit dbf2c5c

Browse files
GrinZerobugyaluwang
authored andcommitted
inspector: tighten createInitiator coverage
1 parent 6213aa0 commit dbf2c5c

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/inspector/network_agent.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,13 @@ static std::unique_ptr<protocol::Value> V8ToProtocolValue(
8686
std::unique_ptr<protocol::DictionaryValue> dict =
8787
protocol::DictionaryValue::create();
8888
for (uint32_t i = 0; i < property_names->Length(); i++) {
89-
Local<Value> key;
89+
// `property_names` is a JSArray returned from GetOwnPropertyNames, so
90+
// indexed access always succeeds. User-defined getters can still throw
91+
// when reading the property value, which is what we guard against.
92+
Local<Value> key =
93+
property_names->Get(context, i).ToLocalChecked();
9094
Local<Value> property;
91-
if (!property_names->Get(context, i).ToLocal(&key) ||
92-
!GetProperty(context, object, key).ToLocal(&property)) {
95+
if (!GetProperty(context, object, key).ToLocal(&property)) {
9396
return nullptr;
9497
}
9598
std::unique_ptr<protocol::Value> protocol_value =

test/parallel/test-inspector-emit-protocol-event.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ for (const [domain, events] of Object.entries(EXPECTED_EVENTS)) {
195195
}
196196

197197
// Verify a user-supplied initiator (with stack) is preserved end-to-end.
198-
// Covers the success body of `if (ObjectGetObject(... "stack"))` in
199-
// NetworkAgent::createInitiatorFromObject (network_agent.cc L159).
198+
// Covers the true branch of `if (ObjectGetObject(... "stack"))` in
199+
// NetworkAgent::createInitiatorFromObject.
200200
session.removeAllListeners('Network.requestWillBeSent');
201201
const userInitiator = {
202202
type: 'script',
@@ -221,8 +221,34 @@ for (const [domain, events] of Object.entries(EXPECTED_EVENTS)) {
221221
initiator: userInitiator,
222222
});
223223

224+
// Verify a user-supplied initiator without `stack` is forwarded as-is.
225+
// Covers the false branch of `if (ObjectGetObject(... "stack"))` in
226+
// NetworkAgent::createInitiatorFromObject.
227+
session.removeAllListeners('Network.requestWillBeSent');
228+
const initiatorWithoutStack = {
229+
type: 'script',
230+
url: 'https://nodejs.org/no-stack.js',
231+
lineNumber: 7,
232+
columnNumber: 8,
233+
};
234+
session.on('Network.requestWillBeSent', common.mustCall(({ params }) => {
235+
assert.strictEqual(params.requestId, 'request-without-initiator-stack');
236+
assert.deepStrictEqual(params.initiator, initiatorWithoutStack);
237+
}));
238+
inspector.Network.requestWillBeSent({
239+
requestId: 'request-without-initiator-stack',
240+
request: {
241+
url: 'https://nodejs.org/en',
242+
method: 'GET',
243+
headers: {},
244+
},
245+
timestamp: 1000,
246+
wallTime: 1000,
247+
initiator: initiatorWithoutStack,
248+
});
249+
224250
// Verify a duplicate requestId is silently ignored. Covers the early
225-
// return when `requests_.contains(request_id)` (network_agent.cc L610).
251+
// return when `requests_.contains(request_id)` in NetworkAgent::requestWillBeSent.
226252
session.removeAllListeners('Network.requestWillBeSent');
227253
const duplicateId = 'duplicate-request-id';
228254
const duplicateParams = {

0 commit comments

Comments
 (0)