Prevent propagation and editing of 'json_data' field in character data (#4690)
Signed| @@ -507,6 +507,9 @@ function readFromV2(char) { | ||
| 507 | 507 | return char; |
| 508 | 508 | } |
| 509 | 509 | |
| 510 | + // If 'json_data' was already saved, don't let it propagate | |
| 511 | + _.unset(char, 'json_data'); | |
| 512 | + | |
| 510 | 513 | const fieldMappings = { |
| 511 | 514 | name: 'name', |
| 512 | 515 | description: 'description', |
| @@ -563,6 +566,9 @@ function charaFormatData(data, directories) { | ||
| 563 | 566 | // This is supposed to save all the foreign keys that ST doesn't care about |
| 564 | 567 | const char = tryParse(data.json_data) || {}; |
| 565 | 568 | |
| 569 | + // Prevent erroneous 'json_data' recursive saving | |
| 570 | + _.unset(char, 'json_data'); | |
| 571 | + | |
| 566 | 572 | // Checks if data.alternate_greetings is an array, a string, or neither, and acts accordingly. (expected to be an array of strings) |
| 567 | 573 | const getAlternateGreetings = data => { |
| 568 | 574 | if (Array.isArray(data.alternate_greetings)) return data.alternate_greetings; |
| @@ -1092,6 +1098,11 @@ router.post('/edit-attribute', validateAvatarUrlMiddleware, async function (requ | ||
| 1092 | 1098 | return response.status(400).send('Error: invalid name.'); |
| 1093 | 1099 | } |
| 1094 | 1100 | |
| 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 | + | |
| 1095 | 1106 | try { |
| 1096 | 1107 | const avatarPath = path.join(request.user.directories.characters, request.body.avatar_url); |
| 1097 | 1108 | const charJSON = await readCharacterData(avatarPath); |
| @@ -1139,6 +1150,10 @@ router.post('/merge-attributes', getFileNameValidationFunction('avatar'), async | ||
| 1139 | 1150 | } |
| 1140 | 1151 | |
| 1141 | 1152 | let character = JSON.parse(pngStringData); |
| 1153 | + | |
| 1154 | + _.unset(update, 'json_data'); | |
| 1155 | + _.unset(character, 'json_data'); | |
| 1156 | + | |
| 1142 | 1157 | character = deepMerge(character, update); |
| 1143 | 1158 | |
| 1144 | 1159 | const validator = new TavernCardValidator(character); |