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) {
516 break;516 break;
517 }517 }
518518
519 try {
519 content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args)));520 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 }
520 }524 }
521525
522 return content;526 return content;
public/scripts/variables.js+8 -0
@@ -46,6 +46,10 @@ function getLocalVariable(name, args = {}) {
46}46}
4747
48function setLocalVariable(name, value, args = {}) {48function setLocalVariable(name, value, args = {}) {
49 if (!name) {
50 throw new Error('Variable name cannot be empty or undefined.');
51 }
52
49 if (!chat_metadata.variables) {53 if (!chat_metadata.variables) {
50 chat_metadata.variables = {};54 chat_metadata.variables = {};
51 }55 }
@@ -99,6 +103,10 @@ function getGlobalVariable(name, args = {}) {
99}103}
100104
101function setGlobalVariable(name, value, args = {}) {105function setGlobalVariable(name, value, args = {}) {
106 if (!name) {
107 throw new Error('Variable name cannot be empty or undefined.');
108 }
109
102 if (args.index !== undefined) {110 if (args.index !== undefined) {
103 try {111 try {
104 let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');112 let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');