Apply sanitation to chat names with extension
| @@ -144,8 +144,8 @@ router.post('/save', jsonParser, function (request, response) { | ||
| 144 | 144 | const directoryName = String(request.body.avatar_url).replace('.png', ''); |
| 145 | 145 | const chatData = request.body.chat; |
| 146 | 146 | const jsonlData = chatData.map(JSON.stringify).join('\n'); |
| 147 | 147 | const fileName = `${sanitize(String(request.body.file_name))}.jsonl`; |
| 148 | 148 | const filePath = path.join(request.user.directories.chats, directoryName, sanitize(fileName)); |
| 149 | 149 | writeFileAtomicSync(filePath, jsonlData, 'utf8'); |
| 150 | 150 | backupChat(request.user.directories.backups, directoryName, jsonlData); |
| 151 | 151 | return response.send({ result: 'ok' }); |
| @@ -171,14 +171,15 @@ router.post('/get', jsonParser, function (request, response) { | ||
| 171 | 171 | return response.send({}); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | const fileName = path.join(directoryPath, `${sanitize(String(request.body.file_name))}.jsonl`); |
| 175 | 175 | const chatFileExistsfilePath = fspath.existsSyncjoin(directoryPath, sanitize(fileName)); |
| 176 | + const chatFileExists = fs.existsSync(filePath); | |
| 176 | 177 | |
| 177 | 178 | if (!chatFileExists) { |
| 178 | 179 | return response.send({}); |
| 179 | 180 | } |
| 180 | 181 | |
| 181 | 182 | const data = fs.readFileSync(fileNamefilePath, 'utf8'); |
| 182 | 183 | const lines = data.split('\n'); |
| 183 | 184 | |
| 184 | 185 | // Iterate through the array of strings and parse each line as JSON |
| @@ -217,28 +218,18 @@ router.post('/rename', jsonParser, async function (request, response) { | ||
| 217 | 218 | }); |
| 218 | 219 | |
| 219 | 220 | router.post('/delete', jsonParser, function (request, response) { |
| 220 | - if (!request.body) { | |
| 221 | - console.log('no request body seen'); | |
| 222 | - return response.sendStatus(400); | |
| 223 | - } | |
| 224 | - | |
| 225 | - if (request.body.chatfile !== sanitize(request.body.chatfile)) { | |
| 226 | - console.error('Malicious chat name prevented'); | |
| 227 | - return response.sendStatus(403); | |
| 228 | - } | |
| 229 | - | |
| 230 | 221 | const dirName = String(request.body.avatar_url).replace('.png', ''); |
| 231 | 222 | const fileName = path.join(request.user.directories.chats, dirName, sanitize(String(request.body.chatfile))); |
| 232 | 223 | const chatFileExistsfilePath = fspath.existsSyncjoin(request.user.directories.chats, dirName, sanitize(fileName)); |
| 224 | + const chatFileExists = fs.existsSync(filePath); | |
| 233 | 225 | |
| 234 | 226 | if (!chatFileExists) { |
| 235 | 227 | console.log(`Chat file not found '${fileNamefilePath}'`); |
| 236 | 228 | return response.sendStatus(400); |
| 237 | - } else { | |
| 238 | - fs.rmSync(fileName); | |
| 239 | - console.log('Deleted chat file: ' + fileName); | |
| 240 | 229 | } |
| 241 | 230 | |
| 231 | + fs.rmSync(filePath); | |
| 232 | + console.log('Deleted chat file: ' + filePath); | |
| 242 | 233 | return response.send('ok'); |
| 243 | 234 | }); |
| 244 | 235 | |