-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdangerousCCWData.js
More file actions
264 lines (246 loc) · 7.28 KB
/
Copy pathdangerousCCWData.js
File metadata and controls
264 lines (246 loc) · 7.28 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// https://assets.ccw.site/extension/dangerousCCWData
(function (_Scratch) {
const { ArgumentType, BlockType, TargetType, Cast, translate, extensions, runtime } = _Scratch;
const extensionId = 'dangerousCCWData';
// (并不safe)
function saferEval(code, args = {}) {
// 临时禁用部分危险API
const disabledAPIs = [
'fetch',
'XMLHttpRequest',
'WebSocket',
'EventSource',
'Worker',
'alert',
'confirm',
'prompt',
'setTimeout',
'setInterval',
'Function',
'Image',
'Audio',
'Video',
'open',
];
const origAPIs = {};
const OrigFunction = window.Function;
// 保存并禁用危险API
disabledAPIs.forEach((api) => {
origAPIs[api] = window[api];
window[api] = null;
});
try {
// 提取参数的键和值,用于传递给new Function
const argKeys = Object.keys(args);
const argValues = Object.values(args);
const funcCode = `const globalThis = null;
const window = null;
const document = null;
const alert = null;
const confirm = null;
const prompt = null;
const fetch = null;
const XMLHttpRequest = null;
const localStorage = null;
const sessionStorage = null;
const Image = null;
const Audio = null;
const Video = null;
const Worker = null;
const Function = null;
const open = null;
const history = null;
const location = null;
const navigator = null;
${code}`;
// 创建函数:参数为argKeys,函数体为code
// new Function的作用域是全局,不会访问当前函数作用域
const func = new OrigFunction(...argKeys, funcCode);
// 执行函数并返回结果
return func(...argValues);
} finally {
// 恢复禁用的API
disabledAPIs.forEach((api) => {
window[api] = origAPIs[api];
});
}
}
class CCWData {
constructor(runtime) {
this._formatMessage = runtime.getFormatMessage({
'zh-cn': {
'CCWData.name': '危险云数据',
'CCWData.setValueInJSON': '(⚠️ 危险,仅用于作品兼容)设置[JSON]中的[KEY]的值为[VALUE]',
'CCWData.getValueInJSON': '(⚠️ 危险,仅用于作品兼容)获得[KEY]在[JSON]中的值',
},
en: {
'CCWData.name': 'Dangerous Cloud Data',
'CCWData.getValueInJSON': '(⚠️ dangerous) get [KEY] in [JSON]',
'CCWData.setValueInJSON': '(⚠️ dangerous) set [VALUE] of key [KEY] in [JSON]',
},
});
}
block_getValueInJSON = () => {
return {
opcode: 'getValueInJSON',
text: this._formatMessage({
id: 'CCWData.getValueInJSON',
}),
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
KEY: {
type: ArgumentType.STRING,
defaultValue: 'key',
},
JSON: {
type: ArgumentType.STRING,
defaultValue: '{"key":"value"}',
},
},
};
};
block_setValueInJSON = () => {
return {
opcode: 'setValueInJSON',
text: this._formatMessage({
id: 'CCWData.setValueInJSON',
}),
blockType: BlockType.REPORTER,
disableMonitor: true,
arguments: {
KEY: {
type: ArgumentType.STRING,
defaultValue: 'key',
},
VALUE: {
type: ArgumentType.STRING,
defaultValue: 'new value',
},
JSON: {
type: ArgumentType.STRING,
defaultValue: '{"key":"value"}',
},
},
};
};
/** Ext Info */
getInfo() {
return {
id: extensionId,
name: this._formatMessage({
id: 'CCWData.name',
default: 'CCWData',
}),
color1: '#ED35A3',
// menuIconURI: icon,
// blockIconURI: icon,
blocks: [
// this.setValueBlock(),
// this.getValueBlock(),
this.block_getValueInJSON(),
this.block_setValueInJSON(),
],
menus: {},
};
}
getValueInJSON(args) {
let key = Cast.toString(args.KEY);
const json = Cast.toString(args.JSON);
let jsonObj;
try {
jsonObj = JSON.parse(json);
} catch (e) {
return `error: ${e.message}`;
}
// process key into evaluable string
if (/[()=]/gm.test(key)) {
return `error: invalid key ${key}, cannot contain ()=`;
}
const key2 = `jsonObj[${key}]`;
// process key when json is an array
if (Array.isArray(jsonObj)) {
if (key.startsWith('[')) {
key = `jsonObj${key}`;
} else {
key = `jsonObj[${key}]`;
}
} else if (/\s/gm.test(key)) {
// return `error: invalid key ${key}, cannot contain space`;
console.warn(`[CCW Data] warning: invalid key ${key}, space and dot cannot be used together`);
key = `jsonObj["${key}"]`;
} else {
key = `jsonObj.${key}`;
}
let rtObj;
// try eval
try {
// console.info(key, jsonObj);
rtObj = saferEval(`return ${key}`, { jsonObj });
} catch (e) {
// Try eval with key2
try {
// console.info(key2, jsonObj);
rtObj = saferEval(`return ${key2}`, { jsonObj });
// eslint-disable-next-line no-empty
} catch (e) {
return `error: key or expression invalid`;
}
}
if (typeof rtObj === 'object') {
return JSON.stringify(rtObj);
}
return rtObj;
}
setValueInJSON(args) {
const key = Cast.toString(args.KEY);
const value = Cast.toString(args.VALUE);
const json = Cast.toString(args.JSON);
let jsonObj;
try {
jsonObj = JSON.parse(json);
} catch (e) {
return `error: ${e.message}`;
}
// process key into evaluable string
if (/[()=]/gm.test(key)) {
return `error: invalid key ${key}, cannot contain ()=`;
}
// try convert value to json object
// eslint-disable-next-line no-new-wrappers
let valueObj = value;
if (/^[\[].*?[\]]$/gm.test(value) || /^[\{].*?[\}]$/gm.test(value)) {
// is json object
// console.info('is json object');
try {
valueObj = JSON.parse(value);
} catch (e) {
// do nothing
}
}
// console.info(typeof valueObj);
if (typeof valueObj === 'string') {
// convert to number if possible
if (/^-?\d*\.?\d*$/gm.test(valueObj)) {
valueObj = Number(valueObj);
}
}
// try set
try {
if (Array.isArray(jsonObj)) {
jsonObj[key] = valueObj;
} else if (/[\.\[\]]/gm.test(key)) {
// contains . or [ or ], should eval
// console.info(`jsonObj.${key} = ${valueObj}`);
saferEval(`jsonObj.${key} = valueObj;`, { jsonObj, valueObj });
} else {
jsonObj[key] = valueObj;
}
} catch (e) {
return `error: key or expression invalid`;
}
return JSON.stringify(jsonObj);
}
}
extensions.register(new CCWData(runtime));
})(Scratch);