simplify input checking

This commit is contained in:
acalcutt 2025-01-05 02:27:26 -05:00
parent 7dddbf77d4
commit 62a3212629

View file

@ -215,31 +215,22 @@ export function readFile(filename) {
*/ */
async function getFontPbf(allowedFonts, fontPath, name, range, fallbacks) { async function getFontPbf(allowedFonts, fontPath, name, range, fallbacks) {
if (!allowedFonts || (allowedFonts[name] && fallbacks)) { if (!allowedFonts || (allowedFonts[name] && fallbacks)) {
const fontMatch = name?.match(/^[\w\s-]+$/); const sRange = String(range).replace(/\n|\r/g, '');
const sanitizedName = fontMatch?.[0] || 'invalid'; const sFontStack = String(name).replace(/\n|\r/g, '');
if (!name || typeof name !== 'string' || name.trim() === '' || !fontMatch) { if (!sFontStack || name.trim() === '') {
console.error( console.error('ERROR: Invalid font name');
'ERROR: Invalid font name: %s',
sanitizedName.replace(/\n|\r/g, ''),
);
throw new Error('Invalid font name'); throw new Error('Invalid font name');
} }
const rangeMatch = range?.match(/^[\d-]+$/); if (!/^\d+-\d+$/.test(sRange)) {
const sanitizedRange = rangeMatch?.[0] || 'invalid';
if (!/^\d+-\d+$/.test(range)) {
console.error( console.error(
'ERROR: Invalid range: %s', 'ERROR: Invalid range: %s',
sanitizedRange.replace(/\n|\r/g, ''), sanitizedRange.replace(/\n|\r/g, ''),
); );
throw new Error('Invalid range'); throw new Error('Invalid range');
} }
const filename = path.join(
fontPath,
sanitizedName,
`${sanitizedRange}.pbf`,
);
const filename = path.join(fontPath, sFontStack, `${sRange}.pbf`);
if (!fallbacks) { if (!fallbacks) {
fallbacks = clone(allowedFonts || {}); fallbacks = clone(allowedFonts || {});
} }