Merge pull request #3205 from SillyTavern/forbid-empty-varnames Do not allow empty or undefined variable names.

f3c12fb926207116465095b1610f828a0ff548e8

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +12 -0Showing whitespace changes
public/scripts/macros.js+4 -0
@@ -516,7 +516,11 @@ export function evaluateMacros(content, env, postProcessFn) {
516516 break;
517517 }
518518
519+ try {
519520 content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args)));
521+ } catch (e) {
522+ console.warn(`Macro content can't be replaced: ${macro.regex} in ${content}`, e);
523+ }
520524 }
521525
522526 return content;
public/scripts/variables.js+8 -0
@@ -46,6 +46,10 @@ function getLocalVariable(name, args = {}) {
4646}
4747
4848function setLocalVariable(name, value, args = {}) {
49+ if (!name) {
50+ throw new Error('Variable name cannot be empty or undefined.');
51+ }
52+
4953 if (!chat_metadata.variables) {
5054 chat_metadata.variables = {};
5155 }
@@ -99,6 +103,10 @@ function getGlobalVariable(name, args = {}) {
99103}
100104
101105function setGlobalVariable(name, value, args = {}) {
106+ if (!name) {
107+ throw new Error('Variable name cannot be empty or undefined.');
108+ }
109+
102110 if (args.index !== undefined) {
103111 try {
104112 let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');