diff --git a/src/node_webstorage.cc b/src/node_webstorage.cc index 10c3ccd68e49a1..21f846fbeb6225 100644 --- a/src/node_webstorage.cc +++ b/src/node_webstorage.cc @@ -43,6 +43,7 @@ using v8::PropertyAttribute; using v8::PropertyCallbackInfo; using v8::PropertyDescriptor; using v8::PropertyHandlerFlags; +using v8::Signature; using v8::String; using v8::Value; @@ -737,8 +738,9 @@ static void Initialize(Local target, Local(), PropertyHandlerFlags::kHasNoSideEffect)); - Local length_getter = - FunctionTemplate::New(isolate, StorageLengthGetter); + Local length_signature = Signature::New(isolate, ctor_tmpl); + Local length_getter = FunctionTemplate::New( + isolate, StorageLengthGetter, Local(), length_signature); ctor_tmpl->PrototypeTemplate()->SetAccessorProperty(env->length_string(), length_getter, Local(), diff --git a/test/parallel/test-webstorage.js b/test/parallel/test-webstorage.js index 71e0a095163ced..383e239d7d68d5 100644 --- a/test/parallel/test-webstorage.js +++ b/test/parallel/test-webstorage.js @@ -26,6 +26,14 @@ test('Storage instances cannot be created in userland', async () => { assert.match(cp.stderr, /Error: Illegal constructor/); }); +test('calling "length" getter on invalid this throws', async () => { + assert.throws(() => Storage.prototype.length, TypeError); + const { get } = Object.getOwnPropertyDescriptor(Storage.prototype, 'length'); + for (const thisArg of [null, undefined, 1n, -0, NaN, true, false, '', [], {}, Symbol()]) { + assert.throws(() => get.call(thisArg), TypeError); + } +}); + test('sessionStorage is not persisted', async () => { let cp = await spawnPromisified(process.execPath, [ '-pe', 'sessionStorage.foo = "barbaz"',