From 62a321262901f4c41dd5b8b419f78be7ea521516 Mon Sep 17 00:00:00 2001 From: acalcutt Date: Sun, 5 Jan 2025 02:27:26 -0500 Subject: [PATCH] simplify input checking --- src/utils.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/utils.js b/src/utils.js index b4ede16..2e30abf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -215,31 +215,22 @@ export function readFile(filename) { */ async function getFontPbf(allowedFonts, fontPath, name, range, fallbacks) { if (!allowedFonts || (allowedFonts[name] && fallbacks)) { - const fontMatch = name?.match(/^[\w\s-]+$/); - const sanitizedName = fontMatch?.[0] || 'invalid'; - if (!name || typeof name !== 'string' || name.trim() === '' || !fontMatch) { - console.error( - 'ERROR: Invalid font name: %s', - sanitizedName.replace(/\n|\r/g, ''), - ); + const sRange = String(range).replace(/\n|\r/g, ''); + const sFontStack = String(name).replace(/\n|\r/g, ''); + if (!sFontStack || name.trim() === '') { + console.error('ERROR: Invalid font name'); throw new Error('Invalid font name'); } - const rangeMatch = range?.match(/^[\d-]+$/); - const sanitizedRange = rangeMatch?.[0] || 'invalid'; - if (!/^\d+-\d+$/.test(range)) { + if (!/^\d+-\d+$/.test(sRange)) { console.error( 'ERROR: Invalid range: %s', sanitizedRange.replace(/\n|\r/g, ''), ); throw new Error('Invalid range'); } - const filename = path.join( - fontPath, - sanitizedName, - `${sanitizedRange}.pbf`, - ); + const filename = path.join(fontPath, sFontStack, `${sRange}.pbf`); if (!fallbacks) { fallbacks = clone(allowedFonts || {}); }