diff --git a/src/extension-support/tw-extension-api-common.js b/src/extension-support/tw-extension-api-common.js index 4a75f6a9..92009e83 100644 --- a/src/extension-support/tw-extension-api-common.js +++ b/src/extension-support/tw-extension-api-common.js @@ -11,7 +11,7 @@ const Scratch = { // Stubs: /* eslint-disable no-unused-vars */ - canFetchResource: url => Promise.resolve(true), + canFetch: url => Promise.resolve(true), canOpenWindow: url => Promise.resolve(true), canRedirect: url => Promise.resolve(true) /* eslint-enable no-unused-vars */ diff --git a/src/extension-support/tw-unsandboxed-extension-runner.js b/src/extension-support/tw-unsandboxed-extension-runner.js index c3cd5acf..04a7af3a 100644 --- a/src/extension-support/tw-unsandboxed-extension-runner.js +++ b/src/extension-support/tw-unsandboxed-extension-runner.js @@ -36,7 +36,7 @@ const setupUnsandboxedExtensionAPI = vm => new Promise(resolve => { global.Scratch.vm = vm; global.Scratch.renderer = vm.runtime.renderer; - global.Scratch.canFetchResource = async url => { + global.Scratch.canFetch = async url => { const parsed = parseURL(url); if (!parsed) { return false; @@ -45,7 +45,7 @@ const setupUnsandboxedExtensionAPI = vm => new Promise(resolve => { if (parsed.protocol === 'blob:' || parsed.protocol === 'data:') { return true; } - return vm.securityManager.canFetchResource(parsed.href); + return vm.securityManager.canFetch(parsed.href); }; global.Scratch.canOpenWindow = async url => { diff --git a/test/integration/tw_security_manager.js b/test/integration/tw_security_manager.js index 421de02b..19248bac 100644 --- a/test/integration/tw_security_manager.js +++ b/test/integration/tw_security_manager.js @@ -65,7 +65,7 @@ test('Allow both extensions', async t => { t.end(); }); -test('canFetchResource', async t => { +test('canFetch', async t => { const vm = new VirtualMachine(); setupUnsandboxedExtensionAPI(vm); global.location = { @@ -73,40 +73,40 @@ test('canFetchResource', async t => { }; // data: and blob: are always allowed, shouldn't call security manager - vm.securityManager.canFetchResource = () => t.fail('security manager should be ignored for these protocols'); - t.equal(await global.Scratch.canFetchResource('data:text/html,test'), true); - t.equal(await global.Scratch.canFetchResource('blob:https://example.com/8c071bf8-c0b6-4a48-81d7-6413c2adf3dd'), true); + vm.securityManager.canFetch = () => t.fail('security manager should be ignored for these protocols'); + t.equal(await global.Scratch.canFetch('data:text/html,test'), true); + t.equal(await global.Scratch.canFetch('blob:https://example.com/8c071bf8-c0b6-4a48-81d7-6413c2adf3dd'), true); - vm.securityManager.canFetchResource = () => false; - t.equal(await global.Scratch.canFetchResource('file:///etc/hosts'), false); - t.equal(await global.Scratch.canFetchResource('http://example.com/'), false); - t.equal(await global.Scratch.canFetchResource('https://example.com/'), false); - t.equal(await global.Scratch.canFetchResource('special.html'), false); + vm.securityManager.canFetch = () => false; + t.equal(await global.Scratch.canFetch('file:///etc/hosts'), false); + t.equal(await global.Scratch.canFetch('http://example.com/'), false); + t.equal(await global.Scratch.canFetch('https://example.com/'), false); + t.equal(await global.Scratch.canFetch('special.html'), false); - vm.securityManager.canFetchResource = () => Promise.resolve(false); - t.equal(await global.Scratch.canFetchResource('file:///etc/hosts'), false); - t.equal(await global.Scratch.canFetchResource('http://example.com/'), false); - t.equal(await global.Scratch.canFetchResource('https://example.com/'), false); - t.equal(await global.Scratch.canFetchResource('boring.html'), false); - t.equal(await global.Scratch.canFetchResource('special.html'), false); + vm.securityManager.canFetch = () => Promise.resolve(false); + t.equal(await global.Scratch.canFetch('file:///etc/hosts'), false); + t.equal(await global.Scratch.canFetch('http://example.com/'), false); + t.equal(await global.Scratch.canFetch('https://example.com/'), false); + t.equal(await global.Scratch.canFetch('boring.html'), false); + t.equal(await global.Scratch.canFetch('special.html'), false); - vm.securityManager.canFetchResource = () => true; - t.equal(await global.Scratch.canFetchResource('file:///etc/hosts'), true); - t.equal(await global.Scratch.canFetchResource('http://example.com/'), true); - t.equal(await global.Scratch.canFetchResource('https://example.com/'), true); - t.equal(await global.Scratch.canFetchResource('boring.html'), true); - t.equal(await global.Scratch.canFetchResource('special.html'), true); + vm.securityManager.canFetch = () => true; + t.equal(await global.Scratch.canFetch('file:///etc/hosts'), true); + t.equal(await global.Scratch.canFetch('http://example.com/'), true); + t.equal(await global.Scratch.canFetch('https://example.com/'), true); + t.equal(await global.Scratch.canFetch('boring.html'), true); + t.equal(await global.Scratch.canFetch('special.html'), true); const calledWithURLs = []; - vm.securityManager.canFetchResource = async url => { + vm.securityManager.canFetch = async url => { calledWithURLs.push(url); return url === 'https://example.com/special.html'; }; - t.equal(await global.Scratch.canFetchResource('file:///etc/hosts'), false); - t.equal(await global.Scratch.canFetchResource('http://example.com/'), false); - t.equal(await global.Scratch.canFetchResource('https://example.com/special.html'), true); - t.equal(await global.Scratch.canFetchResource('boring.html'), false); - t.equal(await global.Scratch.canFetchResource('special.html'), true); + t.equal(await global.Scratch.canFetch('file:///etc/hosts'), false); + t.equal(await global.Scratch.canFetch('http://example.com/'), false); + t.equal(await global.Scratch.canFetch('https://example.com/special.html'), true); + t.equal(await global.Scratch.canFetch('boring.html'), false); + t.equal(await global.Scratch.canFetch('special.html'), true); t.same(calledWithURLs, [ 'file:///etc/hosts', 'http://example.com/', diff --git a/test/unit/tw_extension_api_common.js b/test/unit/tw_extension_api_common.js index 6e279d1b..1fb279ba 100644 --- a/test/unit/tw_extension_api_common.js +++ b/test/unit/tw_extension_api_common.js @@ -33,9 +33,9 @@ test('Cast', t => { t.end(); }); -test('canFetchResource', async t => { +test('canFetch', async t => { // Default implementation should allow all requuests. - const result = ScratchCommon.canFetchResource('https://example.com/'); + const result = ScratchCommon.canFetch('https://example.com/'); t.type(result, Promise); t.equal(await result, true); t.end(); diff --git a/test/unit/tw_unsandboxed_extensions.js b/test/unit/tw_unsandboxed_extensions.js index 4db845ee..5a745ded 100644 --- a/test/unit/tw_unsandboxed_extensions.js +++ b/test/unit/tw_unsandboxed_extensions.js @@ -165,15 +165,15 @@ test('ScratchX', async t => { t.end(); }); -test('canFetchResource', async t => { +test('canFetch', async t => { // tested thoroughly in tw_security_manager.js const vm = new VirtualMachine(); UnsandboxedExtensionRunner.setupUnsandboxedExtensionAPI(vm); global.location = { href: 'https://turbowarp.org' }; - vm.securityManager.canFetchResource = () => false; - const result = global.Scratch.canFetchResource('https://example.com'); + vm.securityManager.canFetch = () => false; + const result = global.Scratch.canFetch('https://example.com'); t.type(result, Promise); t.end(); });