From 1146f4d5a98374ea199fdbc9e2ab7b6b825ccbfb Mon Sep 17 00:00:00 2001 From: doodlewind <7312949+doodlewind@users.noreply.github.com> Date: Mon, 21 Apr 2025 04:00:59 +0000 Subject: [PATCH] fix(editor): prototype polluting in gfx util (#11831) This fixes the security edge case where the font key happens to be `__proto__` or `constructor` --- .../gfx/text/src/element-renderer/utils.ts | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/blocksuite/affine/gfx/text/src/element-renderer/utils.ts b/blocksuite/affine/gfx/text/src/element-renderer/utils.ts index 665972995..947ce26eb 100644 --- a/blocksuite/affine/gfx/text/src/element-renderer/utils.ts +++ b/blocksuite/affine/gfx/text/src/element-renderer/utils.ts @@ -399,23 +399,27 @@ export function parseTokens(text: string): string[] { } export const charWidth = (() => { - const cachedCharWidth: Record> = {}; + const cachedCharWidth = new Map>(); const calculate = (char: string, font: string) => { const ascii = char.charCodeAt(0); - if (!cachedCharWidth[font]) { - cachedCharWidth[font] = []; - } - if (!cachedCharWidth[font][ascii]) { - const width = getLineWidth(char, font); - cachedCharWidth[font][ascii] = width; + + let fontCache = cachedCharWidth.get(font); + if (!fontCache) { + fontCache = []; + cachedCharWidth.set(font, fontCache); } - return cachedCharWidth[font][ascii]; + if (fontCache[ascii] === undefined) { + const width = getLineWidth(char, font); + fontCache[ascii] = width; + } + + return fontCache[ascii]; }; const getCache = (font: string) => { - return cachedCharWidth[font]; + return cachedCharWidth.get(font); }; return { calculate,