mirror of
https://github.com/Skylar-Tech/node-red-contrib-matrix-chat.git
synced 2026-11-26 16:05:28 -07:00
Fix closing during startup wiping the saved encryption store
Closing an E2EE node before its crypto store was restored wrote an empty snapshot over the saved one, losing the device keys.
This commit is contained in:
@@ -186,6 +186,11 @@ async function snapshotCryptoStore(filePath, dbNamePrefix) {
|
||||
db.close();
|
||||
out.push({ name, version, stores });
|
||||
}
|
||||
if (!out.length) {
|
||||
// Nothing in memory (e.g. the store was not restored yet). Writing
|
||||
// that would replace a good snapshot with an empty one.
|
||||
return false;
|
||||
}
|
||||
|
||||
const tmp = `${filePath}.tmp`;
|
||||
fs.writeFileSync(tmp, v8.serialize(out));
|
||||
|
||||
@@ -227,6 +227,10 @@ module.exports = function(RED) {
|
||||
// endpoint. Stops any further snapshot from writing the (deleted
|
||||
// or recreated-empty) store back to disk before the redeploy.
|
||||
cryptoCleared = false,
|
||||
// Set once the Rust crypto stack is initialized. Until then the
|
||||
// in-memory store may not be restored yet, and snapshotting it
|
||||
// would overwrite the saved store on disk.
|
||||
cryptoReady = false,
|
||||
// Set when the node is closed. Startup is asynchronous (SDK import,
|
||||
// homeserver discovery, /whoami, crypto init), so every step checks
|
||||
// it: a node closed mid-startup (e.g. deployed again quickly) must
|
||||
@@ -306,7 +310,7 @@ module.exports = function(RED) {
|
||||
// Snapshot the Rust crypto store to disk so E2EE state survives
|
||||
// restarts. No-op when E2EE is disabled.
|
||||
async function persistCrypto() {
|
||||
if(!cryptoSnapshotPath || cryptoCleared) {
|
||||
if(!cryptoSnapshotPath || cryptoCleared || !cryptoReady) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -1009,6 +1013,7 @@ module.exports = function(RED) {
|
||||
{ cause: cryptoInitError }
|
||||
);
|
||||
}
|
||||
cryptoReady = true;
|
||||
let crypto = node.matrixClient.getCrypto();
|
||||
if(crypto) {
|
||||
// Blacklist (refuse to encrypt to) unverified devices only
|
||||
|
||||
Reference in New Issue
Block a user