fix: recover BPM browsers and harden redaction and Windows supervision

This commit is contained in:
abaishu0730 committed 2026-10-19 08:56:10 +08:00
1 parent acba28c879
commit db98d78f6b
14 files changed
+223 -59

No files matched your search

+24 -46
View File
@@ -5,9 +5,7 @@ const { BpmVault } = require('../../platform/security/vault.cjs');
const { loadPlaywright, browserOptions } = require('../../platform/browser.cjs');
const { BpmListClient } = require('./list-client.cjs');
const DEFAULT_URLS = { main: 'https://bpmn.skyworthdigital.com/' };
const LOGIN_FORMS = {
'https://sso.skyworthdigital.com': { username:'#username', password:'#password', submit:'#kc-form-login button[type="submit"]', form:'#kc-form-login', actionPath:'/realms/skyworthdigital/login-actions/authenticate' }
};
const { LOGIN_FORMS, fillSavedForm, automaticSubmitForm } = require('./login-form.cjs');
const TTL = 10 * 60 * 1000;
const KEYS = new Set(['Tab','Enter','Backspace','Delete','ArrowUp','ArrowDown','ArrowLeft','ArrowRight','Home','End','Escape','Control+A']);
const error = (message, status = 400) => Object.assign(new Error(message), { status });
@@ -75,53 +73,16 @@ class BpmSessionManager {
async fillSavedAccount(lease) {
const stored=await this.vault().read('account:'+lease.id);
if (!stored) return {filled:false,message:'请先保存托管账号。'};
const origin=new URL(lease.page.url()).origin, form=this.loginForms[origin];
if (!form || lease.blocked) return {filled:false,message:'请打开支持的 BPM 登录页面。'};
try {
await Promise.all([lease.page.locator(form.username).waitFor({state:'visible',timeout:1500}),lease.page.locator(form.password).waitFor({state:'visible',timeout:1500})]);
// Validate and set both values in one browser task: a navigation or DOM replacement
// between separate locator.fill calls must never redirect a secret to another field.
const filled=await lease.page.evaluate(({origin,form,username,password})=>{
if (location.origin!==origin || window!==window.top) return false;
const user=document.querySelector(form.username), secret=document.querySelector(form.password);
const usable=(node,type)=>node instanceof HTMLInputElement && node.type===type && !node.disabled && !node.readOnly && node.getClientRects().length>0 && getComputedStyle(node).visibility==='visible' && getComputedStyle(node).display!=='none';
if (!usable(user,'text') || !usable(secret,'password') || user===secret) return false;
const set=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set;
set.call(user,username);set.call(secret,password);
for (const node of [user,secret]) { node.dispatchEvent(new Event('input',{bubbles:true})); node.dispatchEvent(new Event('change',{bubbles:true})); }
return true;
},{origin,form,username:stored.username,password:stored.password});
return {filled,message:filled?'已填入托管账号,请完成登录。':'请打开支持的 BPM 登录表单。'};
} catch { return {filled:false,message:'请等待登录表单显示后重试。'}; }
return fillSavedForm(lease,stored,this.loginForms);
}
async fillAccount(connection,principal) {
return this.run(connection?.id,async()=>{ this.authorize(connection,principal); const lease=await this.getLease(connection.id,principal,true); await this.settleNavigation(lease); return this.fillSavedAccount(lease); });
}
async automaticSubmit(lease,stored,submit=false) {
const origin=new URL(lease.page.url()).origin, form=this.loginForms[origin];
if (!form?.submit || lease.blocked) return false;
return lease.page.evaluate(({origin,form,stored,submit})=>{
if (location.origin!==origin || window!==window.top) return false;
const visible=node=>!!node && node.getClientRects().length>0 && getComputedStyle(node).visibility==='visible' && getComputedStyle(node).display!=='none';
const user=document.querySelector(form.username),secret=document.querySelector(form.password),button=document.querySelector(form.submit);
if (!(user instanceof HTMLInputElement) || user.type!=='text' || !visible(user) || user.disabled || user.readOnly || !(secret instanceof HTMLInputElement) || secret.type!=='password' || !visible(secret) || secret.disabled || secret.readOnly || !visible(button) || button.disabled || button.type!=='submit') return false;
const login=user.form;
if (!login || secret.form!==login || button.form!==login || login.method.toLowerCase()!=='post' || (form.form && !login.matches(form.form))) return false;
if ((button.getAttribute('formmethod') || login.method).toLowerCase()!=='post' || !['','_self'].includes(button.getAttribute('formtarget') || login.target)) return false;
const action=new URL(button.getAttribute('formaction') || login.action,location.href);
if (action.origin!==origin || (form.actionPath && action.pathname!==form.actionPath)) return false;
// Any additional visible field or challenge requires the user's intervention.
const challenge=/captcha|recaptcha|hcaptcha|turnstile|otp|one.?time|totp|verification|verify.?code/i;
if (Array.from(document.querySelectorAll('input,iframe,img,[id],[class]')).some(node=>visible(node) && challenge.test([node.id,node.className,node.getAttribute('name'),node.getAttribute('autocomplete'),node.getAttribute('src'),node.getAttribute('title')].join(' ')))) return false;
if (Array.from(login.elements).some(node=>visible(node) && node!==user && node!==secret && !['submit','button','hidden','reset'].includes(node.type))) return false;
if (submit) {
const set=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,'value').set;
set.call(user,stored.username);set.call(secret,stored.password);
button.click();
}
return true;
},{origin,form,stored:{username:stored.username,password:stored.password},submit}).catch(()=>false);
return automaticSubmitForm(lease,stored,submit,this.loginForms);
}
async persistLease(lease) {
await this.captureSessions(lease);
const storageState=await lease.context.storageState({indexedDB:true});
@@ -205,8 +166,24 @@ class BpmSessionManager {
}
async browser() {
if (this.stopped) throw error('BPM 服务正在关闭。',503);
if (!this.browserPromise) this.browserPromise = Promise.resolve().then(()=>this.browserFactory()).catch(cause=>{ this.browserPromise=null; throw cause; });
return this.browserPromise;
const cached=this.browserPromise;
if (cached) {
const browser=await cached;
if (browser.isConnected?.()!==false) return browser;
this.invalidateBrowser(browser,cached);
return this.browser();
}
const pending=Promise.resolve().then(()=>this.browserFactory()).then(browser=>{
browser.on?.('disconnected',()=>this.invalidateBrowser(browser,pending));
if (browser.isConnected?.()===false) { this.invalidateBrowser(browser,pending);throw error('BPM 浏览器暂不可用,请重试。',503); }
return browser;
}).catch(cause=>{if(this.browserPromise===pending)this.browserPromise=null;throw cause;});
this.browserPromise=pending;
return pending;
}
invalidateBrowser(browser,pending) {
if(this.browserPromise===pending)this.browserPromise=null;
for(const [id,lease] of this.sessions)if(lease.browser===browser){clearTimeout(lease.timer);this.sessions.delete(id);lease.context?.close().catch(()=>{});}
}
async create(connection, principal) {
this.authorize(connection,principal);
@@ -217,6 +194,7 @@ class BpmSessionManager {
const stored = await this.vault().read(connection.id);
lease.sessionStorage = stored?.sessionStorage || {};
const browser = await this.browser();
lease.browser=browser;
this.authorize(connection,principal);
lease.context = await browser.newContext({ viewport:{width:1280,height:800}, acceptDownloads:false, serviceWorkers:'block', permissions:[], storageState:stored?.storageState });
await lease.context.route('**/*', async route => {