Skip to content

Commit 7247ee7

Browse files
fix: logs for testing
1 parent 3ec5a14 commit 7247ee7

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

packages/scratch-svg-renderer/src/sanitize-svg.js

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ const DOMPurify = require('isomorphic-dompurify');
88

99
const sanitizeSvg = {};
1010

11+
const isInternalRef = ref => ref.startsWith('#') || ref.startsWith('data:');
12+
1113
DOMPurify.addHook(
1214
'beforeSanitizeAttributes',
1315
currentNode => {
1416

1517
if (currentNode && currentNode.href && currentNode.href.baseVal) {
1618
const href = currentNode.href.baseVal.replace(/\s/g, '');
1719
// "data:" and "#" are valid hrefs
18-
if ((href.slice(0, 5) !== 'data:') && (href.slice(0, 1) !== '#')) {
19-
20+
if (!isInternalRef(href)) {
21+
// TODO: Those can be in different namespaces than `xlink:`
2022
if (currentNode.attributes.getNamedItem('xlink:href')) {
2123
currentNode.attributes.removeNamedItem('xlink:href');
2224
delete currentNode['xlink:href'];
@@ -27,6 +29,24 @@ DOMPurify.addHook(
2729
}
2830
}
2931
}
32+
33+
// Remove url(...) usages with external references
34+
if (currentNode && currentNode.attributes) {
35+
for (let i = currentNode.attributes.length - 1; i >= 0; i--) {
36+
const attr = currentNode.attributes[i];
37+
const rawValue = attr.value || '';
38+
const value = rawValue.toLowerCase().replace(/\s/g, '');
39+
40+
const urlMatch = value.match(/url\((.+?)\)/);
41+
if (urlMatch) {
42+
const ref = urlMatch[1].replace(/['"]/g, '');
43+
if (!isInternalRef(ref)) {
44+
currentNode.removeAttribute(attr.name);
45+
}
46+
}
47+
}
48+
}
49+
3050
return currentNode;
3151
}
3252
);
@@ -37,13 +57,34 @@ DOMPurify.addHook(
3757
if (data.tagName === 'style') {
3858
const ast = parse(node.textContent);
3959
let isModified = false;
40-
// Remove any @import rules as it could leak HTTP requests
60+
4161
walk(ast, (astNode, item, list) => {
42-
if (astNode.type === 'Atrule' && astNode.name === 'import') {
62+
// @import rules
63+
if (astNode.type === 'Atrule' && astNode.name.toLowerCase() === 'import') {
4364
list.remove(item);
4465
isModified = true;
4566
}
67+
68+
// Elements using url(...) for external resources
69+
if (astNode.type === 'Declaration' && astNode.value) {
70+
let shouldRemove = false;
71+
walk(astNode.value, valueNode => {
72+
if (valueNode.type === 'Url') {
73+
const urlValue = (valueNode.value.value || '').trim().replace(/['"]/g, '');
74+
75+
if (!isInternalRef(urlValue)) {
76+
shouldRemove = true;
77+
}
78+
}
79+
});
80+
81+
if (shouldRemove) {
82+
list.remove(item);
83+
isModified = true;
84+
}
85+
}
4686
});
87+
4788
if (isModified) {
4889
node.textContent = generate(ast);
4990
}

packages/scratch-vm/src/import/load-costume.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ const loadCostumeFromAsset = function (costume, runtime, optVersion) {
356356
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
357357
*/
358358
const loadCostume = function (md5ext, costume, runtime, optVersion) {
359+
console.log('CCCCCCCCCCCCCCCCCCCCCCCCCCCC');
360+
console.log(md5ext);
359361
const idParts = StringUtil.splitFirst(md5ext, '.');
360362
const md5 = idParts[0];
361363
const ext = idParts[1].toLowerCase();
@@ -394,6 +396,8 @@ const loadCostume = function (md5ext, costume, runtime, optVersion) {
394396
if (assetArray[0]) {
395397
costume.asset = assetArray[0];
396398
} else {
399+
console.log('BBBBBBBBBBBBBBBBBBBBBBBBBBB');
400+
console.log(md5ext);
397401
return handleCostumeLoadError(costume, runtime);
398402
}
399403

@@ -405,6 +409,8 @@ const loadCostume = function (md5ext, costume, runtime, optVersion) {
405409
.catch(error => {
406410
// Handle case where storage.load rejects with errors
407411
// instead of resolving null
412+
console.log('AAAAAAAAAAAAAAAAAAAAAAAA');
413+
console.log(md5ext);
408414
log.warn('Error loading costume: ', error);
409415
return handleCostumeLoadError(costume, runtime);
410416
});

packages/scratch-vm/src/serialization/sb2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ const parseScratchAssets = function (object, runtime, topLevel, zip) {
452452
if (costumeSource.textLayerMD5) {
453453
costume.textLayerMD5 = StringUtil.splitFirst(costumeSource.textLayerMD5, '.')[0];
454454
}
455+
console.log('////////////////////////////');
456+
console.log('sb2: old md5');
457+
console.log(costume.md5);
458+
console.log('////////////////////////////');
455459
// If there is no internet connection, or if the asset is not in storage
456460
// for some reason, and we are doing a local .sb2 import, (e.g. zip is provided)
457461
// the file name of the costume should be the baseLayerID followed by the file ext

packages/scratch-vm/src/serialization/sb3.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,13 +894,17 @@ const parseScratchAssets = function (object, runtime, zip) {
894894
costumeSource.md5ext : `${costumeSource.assetId}.${dataFormat}`;
895895
costume.md5 = costumeMd5Ext;
896896
costume.dataFormat = dataFormat;
897+
console.log('////////////////////////////');
898+
console.log('sb3: old md5');
899+
console.log(costumeMd5Ext);
900+
console.log('////////////////////////////');
897901
// deserializeCostume should be called on the costume object we're
898902
// creating above instead of the source costume object, because this way
899903
// we're always loading the 'sb3' representation of the costume
900904
// any translation that needs to happen will happen in the process
901905
// of building up the costume object into an sb3 format
902906
return deserializeCostume(costume, runtime, zip)
903-
.then(() => loadCostume(costumeMd5Ext, costume, runtime));
907+
.then(() => loadCostume(costume.md5, costume, runtime));
904908
// Only attempt to load the costume after the deserialization
905909
// process has been completed
906910
});

0 commit comments

Comments
 (0)