Scratch.canFetchResource -> Scratch.canFetch
This commit is contained in:
1 parent
795ceb1fb3
commit
5ccaee81d3
5 files changed
+35
-35
No files matched your search
@@ -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 */
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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/',
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user