Skip to content

Commit

Permalink
Add canDownload API
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Dec 22, 2024
1 parent 9e6993d commit 1a578aa
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/extension-support/extension-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Object.assign(global.Scratch, ScratchCommon, {
canNotify: () => Promise.resolve(false),
canGeolocate: () => Promise.resolve(false),
canEmbed: () => Promise.resolve(false),
canDownload: () => Promise.resolve(false),
translate
});

Expand Down
9 changes: 9 additions & 0 deletions src/extension-support/tw-security-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ class SecurityManager {
canEmbed (documentURL) {
return Promise.resolve(true);
}

/**
* Determine whether an extension is allowed to download a file with a given name.
* @param {string} filename The name of the file
* @returns {Promise<boolean>|boolean}
*/
canDownload (filename) {
return Promise.resolve(true);
}
}

module.exports = SecurityManager;
2 changes: 2 additions & 0 deletions src/extension-support/tw-unsandboxed-extension-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const setupUnsandboxedExtensionAPI = vm => new Promise(resolve => {
return vm.securityManager.canEmbed(parsed.href);
};

Scratch.canDownload = async name => vm.securityManager.canDownload(name);

Scratch.fetch = async (url, options) => {
const actualURL = url instanceof Request ? url.url : url;

Expand Down
20 changes: 20 additions & 0 deletions test/integration/tw_security_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,23 @@ test('canEmbed', async t => {

t.end();
});

test('canDownload', async t => {
const vm = new VirtualMachine();
setupUnsandboxedExtensionAPI(vm);

const calledWithNames = [];
vm.securityManager.canDownload = async name => {
calledWithNames.push(name);
return name.includes('safe');
};

t.equal(await global.Scratch.canDownload('safe.html'), true);
t.equal(await global.Scratch.canDownload('dangerous.html'), false);
t.same(calledWithNames, [
'safe.html',
'dangerous.html'
]);

t.end();
});
5 changes: 5 additions & 0 deletions test/unit/tw_sandboxed_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ test('canEmbed', async t => {
t.equal(await global.Scratch.canEmbed('https://example.com/'), false);
t.end();
});

test('canDownload', async t => {
t.equal(await global.Scratch.canDownload('test.sb3'), false);
t.end();
});
11 changes: 11 additions & 0 deletions test/unit/tw_unsandboxed_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,17 @@ test('canEmbed', async t => {
t.end();
});

test('canDownload', async t => {
const vm = new VirtualMachine();
UnsandboxedExtensionRunner.setupUnsandboxedExtensionAPI(vm);

vm.securityManager.canDownload = name => name === 'safe.txt';
t.ok(await global.Scratch.canDownload('safe.txt'));
t.notOk(await global.Scratch.canDownload('unsafe.txt'));

t.end();
});

test('CREATE_UNSANDBOXED_EXTENSION_API', t => {
const vm = new VirtualMachine();
vm.on('CREATE_UNSANDBOXED_EXTENSION_API', api => {
Expand Down

0 comments on commit 1a578aa

Please sign in to comment.