fix(editor): prototype polluting in gfx util (#11831)

This fixes the security edge case where the font key happens to be `__proto__` or `constructor`
This commit is contained in:
doodlewind
2025-04-21 04:00:59 +00:00
parent 6d7cd6dd99
commit 1146f4d5a9
@@ -399,23 +399,27 @@ export function parseTokens(text: string): string[] {
} }
export const charWidth = (() => { export const charWidth = (() => {
const cachedCharWidth: Record<string, Array<number>> = {}; const cachedCharWidth = new Map<string, Array<number>>();
const calculate = (char: string, font: string) => { const calculate = (char: string, font: string) => {
const ascii = char.charCodeAt(0); const ascii = char.charCodeAt(0);
if (!cachedCharWidth[font]) {
cachedCharWidth[font] = []; let fontCache = cachedCharWidth.get(font);
} if (!fontCache) {
if (!cachedCharWidth[font][ascii]) { fontCache = [];
const width = getLineWidth(char, font); cachedCharWidth.set(font, fontCache);
cachedCharWidth[font][ascii] = width;
} }
return cachedCharWidth[font][ascii]; if (fontCache[ascii] === undefined) {
const width = getLineWidth(char, font);
fontCache[ascii] = width;
}
return fontCache[ascii];
}; };
const getCache = (font: string) => { const getCache = (font: string) => {
return cachedCharWidth[font]; return cachedCharWidth.get(font);
}; };
return { return {
calculate, calculate,