Prevent propagation and editing of 'json_data' field in character data (#4690)

133ad15a83a0b8c3ad5362281fb9cce4f207bf5c

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

Signed
1 files changed, +15 -0Ignore whitespace
src/endpoints/characters.js+15 -0
@@ -507,6 +507,9 @@ function readFromV2(char) {
507507 return char;
508508 }
509509
510+ // If 'json_data' was already saved, don't let it propagate
511+ _.unset(char, 'json_data');
512+
510513 const fieldMappings = {
511514 name: 'name',
512515 description: 'description',
@@ -563,6 +566,9 @@ function charaFormatData(data, directories) {
563566 // This is supposed to save all the foreign keys that ST doesn't care about
564567 const char = tryParse(data.json_data) || {};
565568
569+ // Prevent erroneous 'json_data' recursive saving
570+ _.unset(char, 'json_data');
571+
566572 // Checks if data.alternate_greetings is an array, a string, or neither, and acts accordingly. (expected to be an array of strings)
567573 const getAlternateGreetings = data => {
568574 if (Array.isArray(data.alternate_greetings)) return data.alternate_greetings;
@@ -1092,6 +1098,11 @@ router.post('/edit-attribute', validateAvatarUrlMiddleware, async function (requ
10921098 return response.status(400).send('Error: invalid name.');
10931099 }
10941100
1101+ if (request.body.field === 'json_data') {
1102+ console.warn('Error: cannot edit json_data field.');
1103+ return response.status(400).send('Error: cannot edit json_data field.');
1104+ }
1105+
10951106 try {
10961107 const avatarPath = path.join(request.user.directories.characters, request.body.avatar_url);
10971108 const charJSON = await readCharacterData(avatarPath);
@@ -1139,6 +1150,10 @@ router.post('/merge-attributes', getFileNameValidationFunction('avatar'), async
11391150 }
11401151
11411152 let character = JSON.parse(pngStringData);
1153+
1154+ _.unset(update, 'json_data');
1155+ _.unset(character, 'json_data');
1156+
11421157 character = deepMerge(character, update);
11431158
11441159 const validator = new TavernCardValidator(character);