From 1b001d1b180535ce247b5ebd9d38435384ddf787 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Wed, 4 Jan 2023 07:50:14 +1100 Subject: [PATCH] fix(permissions): process `URL` in `Deno.FfiPermissionDescriptor.path` for `revoke()` and `request()` (#17094) Previously, `Deno.permissions.[revoke|request]()` wouldn't correctly process the `path: URL` when `name` was `ffi`. This change fixes that behaviour and adds a new function, `formDescriptor()`, to ensure `URL` arguments are consistently handled across `Deno.permissions.[query|revoke|request]()`. --- cli/tests/unit/permissions_test.ts | 19 ++++++----------- runtime/js/10_permissions.js | 34 +++++++++++++++--------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/cli/tests/unit/permissions_test.ts b/cli/tests/unit/permissions_test.ts index 9f9b605f9a..193e606896 100644 --- a/cli/tests/unit/permissions_test.ts +++ b/cli/tests/unit/permissions_test.ts @@ -75,19 +75,14 @@ Deno.test(function permissionStatusIllegalConstructor() { assertEquals(Deno.PermissionStatus.length, 0); }); +// Regression test for https://github.com/denoland/deno/issues/17020 Deno.test(async function permissionURL() { - await Deno.permissions.query({ - name: "read", - path: new URL(".", import.meta.url), - }); - await Deno.permissions.query({ - name: "write", - path: new URL(".", import.meta.url), - }); - await Deno.permissions.query({ - name: "run", - command: new URL(".", import.meta.url), - }); + const path = new URL(".", import.meta.url); + + await Deno.permissions.query({ name: "read", path }); + await Deno.permissions.query({ name: "write", path }); + await Deno.permissions.query({ name: "ffi", path }); + await Deno.permissions.query({ name: "run", command: path }); }); Deno.test(async function permissionDescriptorValidation() { diff --git a/runtime/js/10_permissions.js b/runtime/js/10_permissions.js index d6ab33dcfe..c2ce6cef0d 100644 --- a/runtime/js/10_permissions.js +++ b/runtime/js/10_permissions.js @@ -161,6 +161,20 @@ ArrayPrototypeIncludes(permissionNames, desc.name); } + /** + * @param {Deno.PermissionDescriptor} desc + * @returns {desc is Deno.PermissionDescriptor} + */ + function formDescriptor(desc) { + if ( + desc.name === "read" || desc.name === "write" || desc.name === "ffi" + ) { + desc.path = pathFromURL(desc.path); + } else if (desc.name === "run") { + desc.command = pathFromURL(desc.command); + } + } + class Permissions { constructor(key = null) { if (key != illegalConstructorKey) { @@ -177,13 +191,7 @@ ); } - if ( - desc.name === "read" || desc.name === "write" || desc.name === "ffi" - ) { - desc.path = pathFromURL(desc.path); - } else if (desc.name === "run") { - desc.command = pathFromURL(desc.command); - } + formDescriptor(desc); const state = opQuery(desc); return PromiseResolve(cache(desc, state)); @@ -198,11 +206,7 @@ ); } - if (desc.name === "read" || desc.name === "write") { - desc.path = pathFromURL(desc.path); - } else if (desc.name === "run") { - desc.command = pathFromURL(desc.command); - } + formDescriptor(desc); const state = opRevoke(desc); return PromiseResolve(cache(desc, state)); @@ -217,11 +221,7 @@ ); } - if (desc.name === "read" || desc.name === "write") { - desc.path = pathFromURL(desc.path); - } else if (desc.name === "run") { - desc.command = pathFromURL(desc.command); - } + formDescriptor(desc); const state = opRequest(desc); return PromiseResolve(cache(desc, state));