From 01d38f9218d3ed52aac21f107ee8993d847346aa Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:02:05 +0300 Subject: [PATCH] Additional validation of custom macro keys --- public/scripts/macros.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/scripts/macros.js b/public/scripts/macros.js index 23722aaa2..ef6ffec71 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -30,14 +30,25 @@ export class MacrosParser { throw new Error('Macro key must be a string'); } - if (this.#macros.has(key)) { - console.warn(`Macro ${key} is already registered`); + // Allowing surrounding whitespace would just create more confusion... + key = key.trim(); + + if (!key) { + throw new Error('Macro key must not be empty or whitespace only'); + } + + if (key.startsWith('{{') || key.endsWith('}}')) { + throw new Error('Macro key must not include the surrounding braces'); } if (typeof value !== 'string' && typeof value !== 'function') { throw new Error('Macro value must be a string or a function that returns a string'); } + if (this.#macros.has(key)) { + console.warn(`Macro ${key} is already registered`); + } + this.#macros.set(key, value); }