[BUG] /api/chats/get returns HTTP 200 with {} on internal errors, letting getChat() overwrite a healthy chat with its greeting
Version
1.18.0 (staging). Same code in release.
Environment
Self-hosted, Node. Reproducible with no extensions enabled — the affected consumer below is core public/script.js.
Describe the problem
POST /api/chats/get responds HTTP 200 with {} on internal failure, so a caller cannot distinguish "the read failed" from "this chat is empty". There are four such paths, including the catch-all:
// src/endpoints/chats.js — router.post('/get')
if (!chatDirExists) {
fs.mkdirSync(directoryPath);
return response.send({}); // 200
}
if (!request.body.file_name) {
return response.send({}); // 200
}
return response.send(getChatData(chatFilePath));
} catch (error) {
console.error(error);
return response.send({}); // 200 — any internal error
}getChatData() additionally returns [] for a file that exists but whose every line fails to parse, which collapses "unreadable" into the same value as "empty".
Impact on core code
getChat() in public/script.js treats the ambiguous response as an empty chat and then writes that assumption back to disk:
const data = await response.json();
if (Array.isArray(data) && data.length > 0) {
const chatHeader = data.shift();
chat_metadata = chatHeader?.chat_metadata ?? {};
chat.splice(0, chat.length, ...data);
chat.forEach(ensureMessageMediaIsArray);
} else {
// An empty/corrupted chat file
chat.splice(0, chat.length);
chat_metadata = {};
}
if (!chat_metadata.integrity) {
chat_metadata.integrity = uuidv4();
}
await getChatResult();On a {} response for a chat that is actually fine on disk:
chatis cleared andchat_metadata = {}—integrityandmain_chatare both discarded.- A new
integrityUUID is minted, so the original slug is gone from memory. getChatResult()seeschat.length === 0, pushes the greeting, and callssaveChatConditional()— writing[header, first_mes]over the real file.
Whether the user is protected depends entirely on whether the on-disk file happens to carry an integrity slug. If it does, the freshly minted UUID mismatches and checkChatIntegrity rejects the write (scary popup + reload, but the data survives). If the on-disk header has no integrity slug, the check is skipped and the overwrite is silent:
// src/endpoints/chats.js — trySaveChat()
if (chatIntegritySlug && !await checkChatIntegrity(filePath, chatIntegritySlug)) {That population is not hypothetical. On one real install I counted 84 of 311 chat files with no integrity slug in their header (chats predating the feature, plus files whose header was rewritten by older code). For all of those, one transient read failure is enough to replace the chat with its greeting, with no confirmation.
This also matches the profile of #3809 ("Random loss of recent AI replies", ~10% on refresh, "No corresponding auto-backup files found"), though I have not confirmed they are the same root cause.
Suggested fix
Two independent, small changes:
- Make the endpoint honest. Return a non-2xx status (or at least a distinguishable body) for the
catchpath and for a file that exists but yielded zero parseable records. Reserve{}/[]for "this chat genuinely has no messages". - Make
getChat()fail closed. Distinguish "loaded an empty chat" from "could not read", and on the latter do not fall through togetChatResult()'s greeting-and-save path. Overwriting a chat is never the right response to a failed read of that same chat.
Fixing (1) alone would let every third-party consumer stop guessing as well — the current contract makes safe read-modify-write against this endpoint impossible, since a failed read is indistinguishable from a legitimately empty one.
Additional info
Found while auditing an extension of mine that does read-modify-write on chat files; the extension bug is mine and already fixed. Filing this because the ambiguous contract also affects core getChat() as shown above, independent of any extension.
Source: SillyTavern/SillyTavern