Skip to content

Commit 1a578aa

Browse files
committed
Add canDownload API
TurboWarp/scratch-gui#969
1 parent 9e6993d commit 1a578aa

File tree

6 files changed

+48
-0
lines changed

6 files changed

+48
-0
lines changed

src/extension-support/extension-worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Object.assign(global.Scratch, ScratchCommon, {
8686
canNotify: () => Promise.resolve(false),
8787
canGeolocate: () => Promise.resolve(false),
8888
canEmbed: () => Promise.resolve(false),
89+
canDownload: () => Promise.resolve(false),
8990
translate
9091
});
9192

src/extension-support/tw-security-manager.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ class SecurityManager {
152152
canEmbed (documentURL) {
153153
return Promise.resolve(true);
154154
}
155+
156+
/**
157+
* Determine whether an extension is allowed to download a file with a given name.
158+
* @param {string} filename The name of the file
159+
* @returns {Promise<boolean>|boolean}
160+
*/
161+
canDownload (filename) {
162+
return Promise.resolve(true);
163+
}
155164
}
156165

157166
module.exports = SecurityManager;

src/extension-support/tw-unsandboxed-extension-runner.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const setupUnsandboxedExtensionAPI = vm => new Promise(resolve => {
9696
return vm.securityManager.canEmbed(parsed.href);
9797
};
9898

99+
Scratch.canDownload = async name => vm.securityManager.canDownload(name);
100+
99101
Scratch.fetch = async (url, options) => {
100102
const actualURL = url instanceof Request ? url.url : url;
101103

test/integration/tw_security_manager.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,23 @@ test('canEmbed', async t => {
266266

267267
t.end();
268268
});
269+
270+
test('canDownload', async t => {
271+
const vm = new VirtualMachine();
272+
setupUnsandboxedExtensionAPI(vm);
273+
274+
const calledWithNames = [];
275+
vm.securityManager.canDownload = async name => {
276+
calledWithNames.push(name);
277+
return name.includes('safe');
278+
};
279+
280+
t.equal(await global.Scratch.canDownload('safe.html'), true);
281+
t.equal(await global.Scratch.canDownload('dangerous.html'), false);
282+
t.same(calledWithNames, [
283+
'safe.html',
284+
'dangerous.html'
285+
]);
286+
287+
t.end();
288+
});

test/unit/tw_sandboxed_extensions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,8 @@ test('canEmbed', async t => {
141141
t.equal(await global.Scratch.canEmbed('https://example.com/'), false);
142142
t.end();
143143
});
144+
145+
test('canDownload', async t => {
146+
t.equal(await global.Scratch.canDownload('test.sb3'), false);
147+
t.end();
148+
});

test/unit/tw_unsandboxed_extensions.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,17 @@ test('canEmbed', async t => {
418418
t.end();
419419
});
420420

421+
test('canDownload', async t => {
422+
const vm = new VirtualMachine();
423+
UnsandboxedExtensionRunner.setupUnsandboxedExtensionAPI(vm);
424+
425+
vm.securityManager.canDownload = name => name === 'safe.txt';
426+
t.ok(await global.Scratch.canDownload('safe.txt'));
427+
t.notOk(await global.Scratch.canDownload('unsafe.txt'));
428+
429+
t.end();
430+
});
431+
421432
test('CREATE_UNSANDBOXED_EXTENSION_API', t => {
422433
const vm = new VirtualMachine();
423434
vm.on('CREATE_UNSANDBOXED_EXTENSION_API', api => {

0 commit comments

Comments
 (0)