Skip to content

Commit c396194

Browse files
authored
Merge branch 'TurboWarp:develop' into feat-fps
2 parents 0fd961b + b900a37 commit c396194

File tree

12 files changed

+501
-34
lines changed

12 files changed

+501
-34
lines changed

.github/workflows/deploy.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ jobs:
1313
build:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v4
16+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
17+
with:
18+
persist-credentials: false
1719
- name: Install Node.js
18-
uses: actions/setup-node@v4
20+
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af
1921
with:
2022
node-version: 20
2123
cache: npm
@@ -29,7 +31,7 @@ jobs:
2931
# It will still generate what it can, so it's safe to ignore the error
3032
continue-on-error: true
3133
- name: Upload artifact
32-
uses: actions/upload-pages-artifact@v3
34+
uses: actions/upload-pages-artifact@56afc609e74202658d3ffba0e8f6dda462b719fa
3335
with:
3436
path: ./playground/
3537

@@ -45,4 +47,4 @@ jobs:
4547
steps:
4648
- name: Deploy to GitHub Pages
4749
id: deployment
48-
uses: actions/deploy-pages@v4
50+
uses: actions/deploy-pages@d6db90164ac5ed86f2b6aed7e0febac5b3c0c03e

.github/workflows/node.js.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ jobs:
88
build:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v4
11+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
12+
with:
13+
persist-credentials: false
1214
- name: Install Node.js
13-
uses: actions/setup-node@v4
15+
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af
1416
with:
1517
node-version: 20
1618
cache: npm

src/engine/tw-font-manager.js

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ const AssetUtil = require('../util/tw-asset-util');
33
const StringUtil = require('../util/string-util');
44
const log = require('../util/log');
55

6+
/*
7+
* In general in this file, note that font names in browsers are case-insensitive
8+
* but are whitespace-sensitive.
9+
*/
10+
611
/**
712
* @typedef InternalFont
813
* @property {boolean} system True if the font is built in to the system
@@ -11,40 +16,121 @@ const log = require('../util/log');
1116
* @property {Asset} [asset] scratch-storage asset if system: false
1217
*/
1318

19+
/**
20+
* @param {string} font
21+
* @returns {string}
22+
*/
23+
const removeInvalidCharacters = font => font.replace(/[^-\w ]/g, '');
24+
25+
/**
26+
* @param {InternalFont[]} fonts Modified in-place
27+
* @param {InternalFont} newFont
28+
* @returns {InternalFont|null}
29+
*/
30+
const addOrUpdateFont = (fonts, newFont) => {
31+
let oldFont;
32+
const oldIndex = fonts.findIndex(i => i.family.toLowerCase() === newFont.family.toLowerCase());
33+
if (oldIndex !== -1) {
34+
oldFont = fonts[oldIndex];
35+
fonts.splice(oldIndex, 1);
36+
}
37+
fonts.push(newFont);
38+
return oldFont;
39+
};
40+
1441
class FontManager extends EventEmitter {
1542
/**
1643
* @param {Runtime} runtime
1744
*/
1845
constructor (runtime) {
1946
super();
47+
48+
/** @type {Runtime} */
2049
this.runtime = runtime;
50+
2151
/** @type {Array<InternalFont>} */
2252
this.fonts = [];
53+
54+
/**
55+
* All entries should be lowercase.
56+
* @type {Set<string>}
57+
*/
58+
this.restrictedFonts = new Set();
2359
}
2460

2561
/**
26-
* @param {string} family An unknown font family
27-
* @returns {boolean} true if the family is valid
62+
* Prevents a family from being overridden by a custom font. The project may still use it as a system font.
63+
* @param {string} family
2864
*/
29-
isValidFamily (family) {
65+
restrictFont (family) {
66+
if (!this.isValidSystemFont(family)) {
67+
throw new Error('Invalid font');
68+
}
69+
70+
this.restrictedFonts.add(family.toLowerCase());
71+
72+
const oldLength = this.fonts.length;
73+
this.fonts = this.fonts.filter(font => font.system || this.isValidCustomFont(font.family));
74+
if (this.fonts.length !== oldLength) {
75+
this.updateRenderer();
76+
this.changed();
77+
}
78+
}
79+
80+
/**
81+
* @param {string} family Untrusted font name input
82+
* @returns {boolean} true if the family is valid for a system font
83+
*/
84+
isValidSystemFont (family) {
3085
return /^[-\w ]+$/.test(family);
3186
}
3287

3388
/**
34-
* @param {string} family
35-
* @returns {boolean}
89+
* @param {string} family Untrusted font name input
90+
* @returns {boolean} true if the family is valid for a custom font
3691
*/
37-
hasFont (family) {
38-
return !!this.fonts.find(i => i.family === family);
92+
isValidCustomFont (family) {
93+
return /^[-\w ]+$/.test(family) && !this.restrictedFonts.has(family.toLowerCase());
94+
}
95+
96+
/**
97+
* @deprecated only exists for extension compatibility, use isValidSystemFont or isValidCustomFont instead
98+
*/
99+
isValidFamily (family) {
100+
return this.isValidSystemFont(family) && this.isValidCustomFont(family);
101+
}
102+
103+
/**
104+
* @param {string} family Untrusted font name input
105+
* @returns {string}
106+
*/
107+
getUnusedSystemFont (family) {
108+
return StringUtil.caseInsensitiveUnusedName(
109+
removeInvalidCharacters(family),
110+
this.fonts.map(i => i.family)
111+
);
112+
}
113+
114+
/**
115+
* @param {string} family Untrusted font name input
116+
* @returns {string}
117+
*/
118+
getUnusedCustomFont (family) {
119+
return StringUtil.caseInsensitiveUnusedName(
120+
removeInvalidCharacters(family),
121+
[
122+
...this.fonts.map(i => i.family),
123+
...this.restrictedFonts
124+
]
125+
);
39126
}
40127

41128
/**
42129
* @param {string} family
43130
* @returns {boolean}
44131
*/
45-
getSafeName (family) {
46-
family = family.replace(/[^-\w ]/g, '');
47-
return StringUtil.unusedName(family, this.fonts.map(i => i.family));
132+
hasFont (family) {
133+
return !!this.fonts.find(i => i.family.toLowerCase() === family.toLowerCase());
48134
}
49135

50136
changed () {
@@ -56,14 +142,17 @@ class FontManager extends EventEmitter {
56142
* @param {string} fallback
57143
*/
58144
addSystemFont (family, fallback) {
59-
if (!this.isValidFamily(family)) {
60-
throw new Error('Invalid family');
145+
if (!this.isValidSystemFont(family)) {
146+
throw new Error('Invalid system font family');
61147
}
62-
this.fonts.push({
148+
const oldFont = addOrUpdateFont(this.fonts, {
63149
system: true,
64150
family,
65151
fallback
66152
});
153+
if (oldFont && !oldFont.system) {
154+
this.updateRenderer();
155+
}
67156
this.changed();
68157
}
69158

@@ -73,17 +162,15 @@ class FontManager extends EventEmitter {
73162
* @param {Asset} asset scratch-storage asset
74163
*/
75164
addCustomFont (family, fallback, asset) {
76-
if (!this.isValidFamily(family)) {
77-
throw new Error('Invalid family');
165+
if (!this.isValidCustomFont(family)) {
166+
throw new Error('Invalid custom font family');
78167
}
79-
80-
this.fonts.push({
168+
addOrUpdateFont(this.fonts, {
81169
system: false,
82170
family,
83171
fallback,
84172
asset
85173
});
86-
87174
this.updateRenderer();
88175
this.changed();
89176
}

src/extension-support/extension-worker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ 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),
90+
download: () => Promise.reject(new Error('Scratch.download not supported in sandboxed extensions')),
8991
translate
9092
});
9193

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

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

157167
module.exports = SecurityManager;

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

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

99+
Scratch.canDownload = async (url, name) => {
100+
const parsed = parseURL(url);
101+
if (!parsed) {
102+
return false;
103+
}
104+
// Always reject protocols that would allow code execution.
105+
// eslint-disable-next-line no-script-url
106+
if (parsed.protocol === 'javascript:') {
107+
return false;
108+
}
109+
return vm.securityManager.canDownload(url, name);
110+
};
111+
99112
Scratch.fetch = async (url, options) => {
100113
const actualURL = url instanceof Request ? url.url : url;
101114

@@ -127,6 +140,18 @@ const setupUnsandboxedExtensionAPI = vm => new Promise(resolve => {
127140
location.href = url;
128141
};
129142

143+
Scratch.download = async (url, name) => {
144+
if (!await Scratch.canDownload(url, name)) {
145+
throw new Error(`Permission to download ${name} rejected.`);
146+
}
147+
const link = document.createElement('a');
148+
link.href = url;
149+
link.download = name;
150+
document.body.appendChild(link);
151+
link.click();
152+
link.remove();
153+
};
154+
130155
Scratch.translate = createTranslate(vm);
131156

132157
global.Scratch = Scratch;

src/util/string-util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ class StringUtil {
1515
return name + i;
1616
}
1717

18+
/**
19+
* @param {string} name
20+
* @param {string[]} existingNames
21+
* @returns {string}
22+
*/
23+
static caseInsensitiveUnusedName (name, existingNames) {
24+
const exists = needle => existingNames.some(i => i.toLowerCase() === needle.toLowerCase());
25+
if (!exists(name)) return name;
26+
name = StringUtil.withoutTrailingDigits(name);
27+
let i = 2;
28+
while (exists(`${name}${i}`)) i++;
29+
return `${name}${i}`;
30+
}
31+
1832
/**
1933
* Split a string on the first occurrence of a split character.
2034
* @param {string} text - the string to split.

0 commit comments

Comments
 (0)