Skip to content

Commit eecd2de

Browse files
authored
util: guard callbackify against deleted length and name properties
Fixes: #67745 Signed-off-by: bunnysayzz <stfuazzo@gmail.com> PR-URL: #67754 Fixes: #67745 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 1611823 commit eecd2de

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎lib/util.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,10 @@ function callbackify(original) {
419419
const descriptors = ObjectGetOwnPropertyDescriptors(original);
420420
// It is possible to manipulate a functions `length` or `name` property. This
421421
// guards against the manipulation.
422-
if (typeof descriptors.length.value === 'number') {
422+
if (typeof descriptors.length?.value === 'number') {
423423
descriptors.length.value++;
424424
}
425-
if (typeof descriptors.name.value === 'string') {
425+
if (typeof descriptors.name?.value === 'string') {
426426
descriptors.name.value += 'Callbackified';
427427
}
428428
const propertiesValues = ObjectValues(descriptors);

‎test/parallel/test-util-callbackify.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,47 @@ const values = [
299299
assert.match(stack[1], /at process\.processTicksAndRejections/);
300300
}));
301301
}
302+
303+
{
304+
// Test that deleting the `length` or `name` property of the original
305+
// function does not throw (https://github.com/nodejs/node/issues/67745).
306+
async function asyncFn() {
307+
return 42;
308+
}
309+
delete asyncFn.length;
310+
delete asyncFn.name;
311+
const cbAsyncFn = callbackify(asyncFn);
312+
cbAsyncFn(common.mustSucceed((ret) => {
313+
assert.strictEqual(ret, 42);
314+
}));
315+
316+
function promiseFn() {
317+
return Promise.resolve(42);
318+
}
319+
delete promiseFn.length;
320+
const cbPromiseFn = callbackify(promiseFn);
321+
cbPromiseFn(common.mustSucceed((ret) => {
322+
assert.strictEqual(ret, 42);
323+
}));
324+
}
325+
326+
{
327+
// Test that a `length` or `name` descriptor with a null value does not
328+
// throw (https://github.com/nodejs/node/pull/67754#pullrequestreview-6063276391).
329+
async function asyncFn() {
330+
return 42;
331+
}
332+
const proxyFn = new Proxy(asyncFn, {
333+
getOwnPropertyDescriptor(target, property) {
334+
const descriptor = Reflect.getOwnPropertyDescriptor(target, property);
335+
if (property === 'length' || property === 'name') {
336+
return { ...descriptor, value: null };
337+
}
338+
return descriptor;
339+
},
340+
});
341+
const cbProxyFn = callbackify(proxyFn);
342+
cbProxyFn(common.mustSucceed((ret) => {
343+
assert.strictEqual(ret, 42);
344+
}));
345+
}

0 commit comments

Comments
 (0)