Skip to content

Commit

Permalink
simplify input checking
Browse files Browse the repository at this point in the history
  • Loading branch information
acalcutt committed Jan 5, 2025
1 parent 7dddbf7 commit 62a3212
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {});
}
Expand Down

0 comments on commit 62a3212

Please sign in to comment.