diff --git a/cli/tests/unit/permissions_test.ts b/cli/tests/unit/permissions_test.ts index 006bad2494..458ef2f28e 100644 --- a/cli/tests/unit/permissions_test.ts +++ b/cli/tests/unit/permissions_test.ts @@ -71,3 +71,18 @@ Deno.test(async function permissionURL() { command: new URL(".", import.meta.url), }); }); + +Deno.test(async function permissionDescriptorValidation() { + for (const value of [undefined, null, {}]) { + for (const method of ["query", "request", "revoke"]) { + await assertRejects( + async () => { + // deno-lint-ignore no-explicit-any + await (Deno.permissions as any)[method](value as any); + }, + TypeError, + '"undefined" is not a valid permission name', + ); + } + } +}); diff --git a/runtime/js/10_permissions.js b/runtime/js/10_permissions.js index 1a9be1f275..66c68bbf0a 100644 --- a/runtime/js/10_permissions.js +++ b/runtime/js/10_permissions.js @@ -149,7 +149,7 @@ * @returns {desc is Deno.PermissionDescriptor} */ function isValidDescriptor(desc) { - return desc && desc !== null && + return typeof desc === "object" && desc !== null && ArrayPrototypeIncludes(permissionNames, desc.name); } @@ -164,7 +164,8 @@ if (!isValidDescriptor(desc)) { return PromiseReject( new TypeError( - `The provided value "${desc.name}" is not a valid permission name.`, + `The provided value "${desc + ?.name}" is not a valid permission name.`, ), ); } @@ -185,7 +186,8 @@ if (!isValidDescriptor(desc)) { return PromiseReject( new TypeError( - `The provided value "${desc.name}" is not a valid permission name.`, + `The provided value "${desc + ?.name}" is not a valid permission name.`, ), ); } @@ -204,7 +206,8 @@ if (!isValidDescriptor(desc)) { return PromiseReject( new TypeError( - `The provided value "${desc.name}" is not a valid permission name.`, + `The provided value "${desc + ?.name}" is not a valid permission name.`, ), ); }