File size: 3,845 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const ArgumentType = require('../extension-support/argument-type');
const BlockType = require('../extension-support/block-type');
const TargetType = require('../extension-support/target-type');
const Cast = require('./cast');
const Clone = require('./clone');
const Color = require('./color');

/**
 * Parse a URL object or return null.
 * @param {string} url
 * @returns {URL|null}
 */
const parseURL = url => {
    try {
        return new URL(url, location.href);
    } catch (e) {
        return null;
    }
};

class CustomExtensionApi {
    constructor (followLimitations) {
        this.limited = followLimitations === true;
    }

    get ArgumentType () {
        return ArgumentType;
    }
    get BlockType () {
        return BlockType;
    }
    get TargetType () {
        return TargetType;
    }
    get Cast () {
        return Cast;
    }
    get Clone () {
        return Clone;
    }
    get Color () {
        return Color;
    }
    
    get vm () {
        return vm;
    }
    get renderer () {
        return CustomExtensionApi.vm.runtime.renderer;
    }
    
    async canFetch (url) {
        if (this.limited) {
            const parsed = parseURL(url);
            if (!parsed) {
                return false;
            }
            // Always allow protocols that don't involve a remote request.
            if (parsed.protocol === 'blob:' || parsed.protocol === 'data:') {
                return true;
            }
            return true;
        }
        return true;
    }
    async canOpenWindow (url) {
        if (this.limited) {
            const parsed = parseURL(url);
            if (!parsed) {
                return false;
            }
            // Always reject protocols that would allow code execution.
            // eslint-disable-next-line no-script-url
            if (parsed.protocol === 'javascript:') {
                return false;
            }
            return true;
        }
        return true;
    }
    async canRedirect (url) {
        if (this.limited) {
            const parsed = parseURL(url);
            if (!parsed) {
                return false;
            }
            // Always reject protocols that would allow code execution.
            // eslint-disable-next-line no-script-url
            if (parsed.protocol === 'javascript:') {
                return false;
            }
            return true;
        }
        return true;
    }

    async fetch (url, options) {
        if (this.limited) {
            const actualURL = url instanceof Request ? url.url : url;
            if (!await global.Scratch.canFetch(actualURL)) {
                throw new Error(`Permission to fetch ${actualURL} rejected.`);
            }
            return fetch(url, {
                ...options,
                redirect: 'error'
            });
        }
        return fetch(url, {
            ...options,
            redirect: 'error'
        });
    }
    async openWindow (url, features) {
        if (this.limited) {
            if (!await global.Scratch.canOpenWindow(url)) {
                throw new Error(`Permission to open tab ${url} rejected.`);
            }
            return window.open(url, '_blank', features);
        }
        return window.open(url, '_blank', features);
    }
    async redirect (url) {
        if (this.limited) {
            if (!await global.Scratch.canRedirect(url)) {
                throw new Error(`Permission to redirect to ${url} rejected.`);
            }
            location.href = url;
            return;
        }
        location.href = url;
    }

    get extensions () {
        return {
            unsandboxed: true,
            register: () => {
                throw new Error("Register cannot be replicated in custom-ext-api-to-core");
            }
        };
    }
};

module.exports = CustomExtensionApi;