Skip to content

Commit 90b9da4

Browse files
committed
sanitize extension ID in getExtensionIdForOpcode
1 parent b67ba75 commit 90b9da4

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

docs/extensions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ class SomeBlocks {
310310
return {
311311
// Required: the machine-readable name of this extension.
312312
// Will be used as the extension's namespace.
313+
// Allowed characters are those matching the regular expression [\w-]: A-Z, a-z, 0-9, and hyphen ("-").
313314
id: 'someBlocks',
314315

315316
// Core extensions only: override the default extension block colors.

src/serialization/sb3.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,17 @@ const compressInputTree = function (block, blocks) {
273273
};
274274

275275
/**
276-
* Get non-core extension ID for a given sb3 opcode.
276+
* Get sanitized non-core extension ID for a given sb3 opcode.
277+
* Note that this should never return a URL. If in the future the SB3 loader supports loading extensions by URL, this
278+
* ID should be used to (for example) look up the extension's full URL from a table in the SB3's JSON.
277279
* @param {!string} opcode The opcode to examine for extension.
278280
* @return {?string} The extension ID, if it exists and is not a core extension.
279281
*/
280282
const getExtensionIdForOpcode = function (opcode) {
283+
// Allowed ID characters are those matching the regular expression [\w-]: A-Z, a-z, 0-9, and hyphen ("-").
281284
const index = opcode.indexOf('_');
282-
const prefix = opcode.substring(0, index);
285+
const forbiddenSymbols = /[^\w-]/g;
286+
const prefix = opcode.substring(0, index).replace(forbiddenSymbols, '-');
283287
if (CORE_EXTENSIONS.indexOf(prefix) === -1) {
284288
if (prefix !== '') return prefix;
285289
}

test/unit/serialization_sb3.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ test('getExtensionIdForOpcode', t => {
290290
// does not return anything for opcodes with no extension
291291
t.false(sb3.getExtensionIdForOpcode('hello'));
292292

293+
// forbidden characters must be replaced with '-'
294+
t.equal(sb3.getExtensionIdForOpcode('hi:there/happy_people'), 'hi-there-happy');
295+
293296
t.end();
294297
});
295298

0 commit comments

Comments
 (0)