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
@@ -264,11 +264,16 @@ class BpmSessionManager {
|
||||
};
|
||||
lease.network.on('Page.frameRequestedNavigation',started);
|
||||
lease.network.on('Page.frameStartedLoading',started);
|
||||
lease.network.on('Page.frameStoppedLoading',event=>{
|
||||
const finished=event=>{
|
||||
if (event.frameId!==lease.mainFrameId) return;
|
||||
lease.finishNavigation?.(); lease.pendingNavigation=null; lease.finishNavigation=null;
|
||||
});
|
||||
};
|
||||
lease.network.on('Page.frameStoppedLoading',finished);
|
||||
// Portal images/widgets can stall after its document is ready. Login
|
||||
// verification needs the committed document, not every subresource.
|
||||
lease.network.on('Page.lifecycleEvent',event=>{if(event.name==='DOMContentLoaded')finished(event);});
|
||||
await lease.network.send('Page.enable');
|
||||
await lease.network.send('Page.setLifecycleEventsEnabled',{enabled:true});
|
||||
lease.network.on('Fetch.requestPaused', async event => {
|
||||
try {
|
||||
if (this.allowed(event.request.url)) await lease.network.send('Fetch.continueRequest',{requestId:event.requestId});
|
||||
@@ -327,8 +332,11 @@ class BpmSessionManager {
|
||||
});
|
||||
}
|
||||
async defaultProbe(page) {
|
||||
// A visible company SSO form is an explicit login state. The authenticated
|
||||
// portal renders its user menu asynchronously after DOMContentLoaded.
|
||||
if(new URL(page.url()).origin==='https://sso.skyworthdigital.com' && await page.locator('#kc-form-login').isVisible().catch(()=>false))return {ok:false};
|
||||
const marker=page.locator('a.yz-btn-signinuser .x-btn-inner').first();
|
||||
await marker.waitFor({state:'visible',timeout:4000}).catch(()=>{});
|
||||
await marker.waitFor({state:'visible',timeout:15000}).catch(()=>{});
|
||||
if (!await marker.isVisible().catch(()=>false)) return {ok:false};
|
||||
const identity=(await marker.innerText()).trim();
|
||||
return {ok:!!identity && !/^(登录|请登录|未登录|sign\s*in|login)$/i.test(identity),identity};
|
||||
|
||||
+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