fix(bpm): persist encrypted sessions without file renames
This commit is contained in:
1 parent
a785fa73d4
commit
607b240aa3
5 files changed
+78
-21
No files matched your search
+16
-6
@@ -62,7 +62,8 @@ class BpmVault {
|
||||
if(this.db)return this.db;
|
||||
const db=new DatabaseSync(path.join(this.stateDir,'accounts.sqlite'));
|
||||
try{db.exec(`PRAGMA busy_timeout=5000; PRAGMA journal_mode=WAL; PRAGMA secure_delete=ON;
|
||||
CREATE TABLE IF NOT EXISTS accounts(connection_id TEXT PRIMARY KEY,username_key TEXT NOT NULL UNIQUE,payload BLOB NOT NULL);`);this.db=db;return db;}
|
||||
CREATE TABLE IF NOT EXISTS accounts(connection_id TEXT PRIMARY KEY,username_key TEXT NOT NULL UNIQUE,payload BLOB NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS sessions(connection_id TEXT PRIMARY KEY,payload BLOB NOT NULL);`);this.db=db;return db;}
|
||||
catch{db.close();throw fail();}
|
||||
}
|
||||
accountKey(username) {
|
||||
@@ -103,21 +104,30 @@ class BpmVault {
|
||||
const filename = this.filename(id);
|
||||
try {
|
||||
if(id.startsWith('account:')){const row=this.accounts().prepare('SELECT payload FROM accounts WHERE connection_id=?').get(id.slice(8));if(row)return this.decode(id,row.payload);}
|
||||
else{const row=this.accounts().prepare('SELECT payload FROM sessions WHERE connection_id=?').get(id);if(row)return this.decode(id,row.payload);}
|
||||
const state=this.decode(id,await fsp.readFile(filename));
|
||||
if(id.startsWith('account:')){this.putAccount(id,state);await fsp.rm(filename,{force:true});}
|
||||
else{
|
||||
// Commit legacy ciphertext before removing its file. An existing database
|
||||
// session is authoritative if another process completed migration first.
|
||||
this.accounts().prepare('INSERT OR IGNORE INTO sessions(connection_id,payload) VALUES(?,?)').run(id,this.encode(id,state));
|
||||
const saved=this.decode(id,this.accounts().prepare('SELECT payload FROM sessions WHERE connection_id=?').get(id).payload);
|
||||
await fsp.rm(filename,{force:true});return saved;
|
||||
}
|
||||
return state;
|
||||
} catch (error) { if (error.code === 'ENOENT') return null;if(error.code==='BPM_ACCOUNT_DUPLICATE')throw error;throw fail(); }
|
||||
}
|
||||
async write(id, state) {
|
||||
const filename = this.filename(id), temp = filename + '.' + crypto.randomUUID() + '.tmp';
|
||||
const filename = this.filename(id);
|
||||
if(id.startsWith('account:')){this.putAccount(id,state);await fsp.rm(filename,{force:true}).catch(()=>{});return;}
|
||||
try {
|
||||
await fsp.writeFile(temp, this.encode(id,state), { flag: 'wx', mode: 0o600 });
|
||||
await fsp.rename(temp, filename);
|
||||
// Some Windows state directories reject even same-directory renames with
|
||||
// EXDEV. SQLite publishes the encrypted payload in one atomic transaction.
|
||||
this.accounts().prepare('INSERT INTO sessions(connection_id,payload) VALUES(?,?) ON CONFLICT(connection_id) DO UPDATE SET payload=excluded.payload').run(id,this.encode(id,state));
|
||||
await fsp.rm(filename,{force:true}).catch(()=>{});
|
||||
} catch { throw fail(); }
|
||||
finally { await fsp.rm(temp, { force: true }).catch(() => {}); }
|
||||
}
|
||||
async remove(id) { try { await fsp.rm(this.filename(id), { force: true });if(id.startsWith('account:'))this.accounts().prepare('DELETE FROM accounts WHERE connection_id=?').run(id.slice(8)); } catch { throw fail(); } }
|
||||
async remove(id) { try { await fsp.rm(this.filename(id), { force: true });if(id.startsWith('account:'))this.accounts().prepare('DELETE FROM accounts WHERE connection_id=?').run(id.slice(8));else this.accounts().prepare('DELETE FROM sessions WHERE connection_id=?').run(id); } catch { throw fail(); } }
|
||||
destroy() { this.db?.close();this.db=null;this.key?.fill(0);this.key=null; }
|
||||
}
|
||||
module.exports = { BpmVault };
|
||||
Reference in new issue
Block a user