fix: reconcile failed BPM handshakes and reject revoked session writes
This commit is contained in:
1 parent
42d19c707d
commit
8d010a05cd
7 files changed
+71
-21
No files matched your search
@@ -44,7 +44,7 @@ class BpmArrivalMonitor{
|
||||
}
|
||||
retry(entry,status='polling',delay){if(!this.active(entry))return;if(entry.connectedAt&&this.clock()-entry.connectedAt>60000)entry.failures=0;if(!entry.lifecycle.transition(({polling:'NETWORK_FAILED',login_required:'AUTH_REQUIRED',busy:'BUSY'})[status]))return;entry.nextRetry=this.clock()+(delay??this.retryMs[Math.min(entry.failures++,this.retryMs.length-1)]);}
|
||||
connect(entry,connection){
|
||||
const ticket=entry.lifecycle.begin();if(ticket===null)return;const attempt=++entry.attempt;let closed=false;
|
||||
const ticket=entry.lifecycle.begin();if(ticket===null)return;const attempt=++entry.attempt;let closed=false,subscribed=false;
|
||||
entry.connecting=(async()=>{
|
||||
if(this.manager.sessions.has(entry.id))throw Object.assign(new Error('人工登录窗口正在使用。'),{status:409});
|
||||
const result=await this.manager.ensure(connection,this.principal);if(!this.active(entry)||!entry.lifecycle.current(ticket))return;
|
||||
@@ -52,10 +52,10 @@ class BpmArrivalMonitor{
|
||||
entry.epoch=this.manager.epoch?.(entry.id)||0;const handle=await this.manager.run(entry.id,async()=>{
|
||||
this.manager.authorize(connection,this.principal);if(this.manager.sessions.has(entry.id))throw Object.assign(new Error('人工登录窗口正在使用。'),{status:409});
|
||||
const saved=await this.manager.vault().read(entry.id);
|
||||
return this.hubClient.connect(saved?.storageState,{signal:entry.controller.signal,onEvent:name=>{if(!this.active(entry)||!entry.lifecycle.current(ticket)||entry.attempt!==attempt||entry.epoch!==(this.manager.epoch?.(entry.id)||0))return;entry.lastSignalAt=new Date(this.clock()).toISOString();this.queue(entry,name==='worklistChanged'?['todo']:name==='informlistChanged'?['inform']:['todo','inform']);},onClose:()=>{closed=true;if(this.active(entry)&&entry.lifecycle.current(ticket)&&entry.attempt===attempt){entry.handle=null;this.retry(entry);}}});
|
||||
return this.hubClient.connect(saved?.storageState,{signal:entry.controller.signal,onEvent:name=>{if(!this.active(entry)||!entry.lifecycle.current(ticket)||entry.attempt!==attempt||entry.epoch!==(this.manager.epoch?.(entry.id)||0))return;entry.lastSignalAt=new Date(this.clock()).toISOString();this.queue(entry,name==='worklistChanged'?['todo']:name==='informlistChanged'?['inform']:['todo','inform']);},onClose:()=>{closed=true;if(this.active(entry)&&entry.lifecycle.current(ticket)&&entry.attempt===attempt){entry.handle=null;this.retry(entry);if(!subscribed)this.queue(entry,['todo','inform']);}}});
|
||||
});
|
||||
if(!handle)return;if(!this.active(entry)||closed||!entry.lifecycle.transition('CONNECTED',ticket)){await handle.stop();return;}
|
||||
entry.handle=handle;entry.connectedAt=this.clock();entry.nextRetry=Infinity;entry.lastReconcileAt=this.clock();entry.nextReconcile=this.clock()+this.scanner.store.config().intervalMinutes*60000;this.queue(entry,['todo','inform']);
|
||||
subscribed=true;entry.handle=handle;entry.connectedAt=this.clock();entry.nextRetry=Infinity;entry.lastReconcileAt=this.clock();entry.nextReconcile=this.clock()+this.scanner.store.config().intervalMinutes*60000;this.queue(entry,['todo','inform']);
|
||||
})().catch(error=>{if(!this.active(entry)||!entry.lifecycle.current(ticket))return;if(error.status===401)this.manager.observe?.(entry.id,'portal',error);this.retry(entry,error.status===409?'busy':error.status===401?'login_required':'polling',error.status===409?5000:error.status===401?5*60000:undefined);if(![401,409].includes(error.status))this.queue(entry,['todo','inform']);}).finally(()=>{entry.connecting=null;});
|
||||
}
|
||||
dueKinds(entry){return [...entry.dirty].filter(kind=>this.clock()>=this.source(entry,kind).nextAttempt);}
|
||||
|
||||
@@ -93,14 +93,16 @@ class BpmSessionManager {
|
||||
return automaticSubmitForm(lease,stored,submit,this.loginForms);
|
||||
}
|
||||
|
||||
async persistLease(lease,ticket=lease.stateTicket) {
|
||||
if(ticket)this.sessionState.assert(ticket);
|
||||
await this.captureSessions(lease);
|
||||
const storageState=await lease.context.storageState({indexedDB:true});
|
||||
if(ticket)this.sessionState.assert(ticket);
|
||||
await this.vault().write(lease.id,{storageState,sessionStorage:lease.sessionStorage});
|
||||
if(ticket)this.sessionState.assert(ticket);
|
||||
this.sessionEpochs.set(lease.id,this.epoch(lease.id)+1);
|
||||
async persistLease(lease,ticket,principal) {
|
||||
const assertCurrent=()=>{this.authorize(lease.connection,principal);if(lease.sessionId!==principal.sessionId)throw error('此连接正在另一登录窗口中使用。',409);if(ticket)this.sessionState.assert(ticket);};
|
||||
lease.persistenceTicket=ticket;
|
||||
try{
|
||||
assertCurrent();await this.captureSessions(lease);assertCurrent();
|
||||
const storageState=await lease.context.storageState({indexedDB:true});assertCurrent();
|
||||
// Vault commits synchronously before its first await; check ownership at this boundary.
|
||||
await this.vault().write(lease.id,{storageState,sessionStorage:lease.sessionStorage});assertCurrent();
|
||||
this.sessionEpochs.set(lease.id,this.epoch(lease.id)+1);
|
||||
}finally{delete lease.persistenceTicket;}
|
||||
}
|
||||
canPersistVerified(result) {
|
||||
return result.status==='connected' && result.checks.portal.ok;
|
||||
@@ -160,7 +162,7 @@ class BpmSessionManager {
|
||||
try{
|
||||
const lease=await this.create(connection,principal);let result=await this.verify(lease);
|
||||
this.authorize(connection,principal);this.sessionState.assert(ticket);
|
||||
if(this.canPersistVerified(result))await this.persistLease(lease,ticket);
|
||||
if(this.canPersistVerified(result))await this.persistLease(lease,ticket,principal);
|
||||
this.recordSessionResult(id,result);this.sessionState.finish(ticket,result.status==='connected'?'valid':result.status==='login_required'?'expired':'unavailable');
|
||||
if(result.status!=='login_required'||result.checks.portal.message!=='请在此入口完成登录。'||lease.blocked)return result;
|
||||
const stored=await this.vault().read('account:'+id);this.authorize(connection,principal);this.assertGeneration(id,generation);
|
||||
@@ -182,7 +184,7 @@ class BpmSessionManager {
|
||||
await this.settleNavigation(lease);result=await this.verify(lease);
|
||||
this.authorize(connection,principal);this.sessionState.assert(ticket);
|
||||
if(this.canPersistVerified(result)){
|
||||
await this.persistLease(lease,ticket);delete stored.nextAttemptAt;await this.vault().write('account:'+id,stored);
|
||||
await this.persistLease(lease,ticket,principal);this.authorize(connection,principal);this.sessionState.assert(ticket);delete stored.nextAttemptAt;await this.vault().write('account:'+id,stored);
|
||||
}else if(!result.checks.portal.ok)result.checks.portal.message='自动登录未完成,请检查托管账号或人工完成验证;稍后重试。';
|
||||
this.authorize(connection,principal);this.sessionState.assert(ticket);this.recordSessionResult(id,result);
|
||||
this.sessionState.finish(ticket,result.status==='connected'?'valid':result.status==='login_required'?'expired':'unavailable',result.status==='connected'?null:'manual_required');
|
||||
@@ -473,7 +475,7 @@ class BpmSessionManager {
|
||||
this.authorize(connection,principal);
|
||||
if(lease.stateTicket)this.sessionState.assert(lease.stateTicket);
|
||||
// Failed verification never replaces a previously working credential snapshot.
|
||||
if (this.canPersistVerified(result)) await this.persistLease(lease);
|
||||
if (this.canPersistVerified(result)) await this.persistLease(lease,lease.stateTicket,principal);
|
||||
this.authorize(connection,principal);if(lease.stateTicket)this.sessionState.assert(lease.stateTicket);this.recordSessionResult(connection.id,result);
|
||||
return result;
|
||||
});
|
||||
@@ -487,7 +489,7 @@ class BpmSessionManager {
|
||||
try {
|
||||
const lease=transient?await this.create(connection,principal):await this.getLease(connection.id,principal,true),guard=ticket||lease.stateTicket;
|
||||
const result=await this.verify(lease);this.authorize(connection,principal);if(guard)this.sessionState.assert(guard);
|
||||
if(this.canPersistVerified(result))await this.persistLease(lease,guard);
|
||||
if(this.canPersistVerified(result))await this.persistLease(lease,guard,principal);
|
||||
this.authorize(connection,principal);if(guard)this.sessionState.assert(guard);this.recordSessionResult(connection.id,result);
|
||||
if(ticket)this.sessionState.finish(ticket,result.status==='connected'?'valid':result.status==='login_required'?'expired':'unavailable');return result;
|
||||
} finally {this.sessionState.abort(ticket);if(transient)await this.closeLease(connection.id);}
|
||||
@@ -500,11 +502,12 @@ class BpmSessionManager {
|
||||
await lease.context?.close().catch(()=>{});
|
||||
}
|
||||
async close(id,principal) { return this.run(id,async()=>{ if (!this.sessions.has(id)) return {active:false}; await this.getLease(id,principal,false,true); await this.closeLease(id); return {active:false}; }); }
|
||||
async releaseSession(sessionId) { await Promise.all([...this.sessions.values()].filter(lease=>lease.sessionId===sessionId).map(lease=>this.run(lease.id,()=>this.closeLease(lease.id)))); }
|
||||
async releaseSession(sessionId) { await this.releaseSessionHashes([crypto.createHash('sha256').update(sessionId).digest('hex')]); }
|
||||
async releaseSessionHashes(hashes) {
|
||||
const revoked=new Set(hashes);
|
||||
for(const hash of revoked)this.revokedSessionHashes.add(hash);
|
||||
await Promise.all([...this.sessions.values()].filter(lease=>revoked.has(crypto.createHash('sha256').update(lease.sessionId).digest('hex'))).map(lease=>this.run(lease.id,()=>this.closeLease(lease.id))));
|
||||
for(const lease of this.sessions.values())if(revoked.has(crypto.createHash('sha256').update(lease.sessionId).digest('hex'))){this.sessionState.abort(lease.persistenceTicket);this.sessionState.abort(lease.stateTicket);}
|
||||
await Promise.all([...this.sessions.values()].filter(lease=>revoked.has(crypto.createHash('sha256').update(lease.sessionId).digest('hex'))).map(lease=>this.run(lease.id,()=>this.sessions.get(lease.id)===lease?this.closeLease(lease.id):undefined)));
|
||||
}
|
||||
async suspend(id) { this.disabled.add(id);this.sessionState.lifecycle(id,'pause');this.health.clear(id);await this.sessions.get(id)?.context?.close().catch(()=>{}); await this.run(id,()=>this.closeLease(id)); }
|
||||
async resume(id) { await this.run(id,async()=>{this.disabled.delete(id);this.sessionState.lifecycle(id,'resume');this.health.clear(id);}); }
|
||||
|
||||
Reference in new issue
Block a user