25 lines
2.1 KiB
JavaScript
25 lines
2.1 KiB
JavaScript
export const DESKTOP_CHANNELS = Object.freeze({ status: 'awb:desktop-status', pickProject: 'awb:desktop-pick-project',
|
|
revealFile: 'awb:desktop-reveal-file', openFilePreview: 'awb:desktop-file-preview', projectFolder: 'awb:desktop-project-folder', reviewTask: 'awb:desktop-review-task', stopHost: 'awb:desktop-stop-host' });
|
|
export const desktopError = message => Object.assign(new Error(message), { code: 'DESKTOP_SCOPE_REJECTED', status: 403 });
|
|
export function desktopOrigin(value, origin) {
|
|
try { const url = new URL(value); return url.origin === origin && url.protocol === 'http:' && !url.username && !url.password; } catch { return false; }
|
|
}
|
|
export function assertDesktopFrame(event, window, origin) {
|
|
if (!window || window.isDestroyed() || event.sender !== window.webContents || event.senderFrame !== window.webContents.mainFrame
|
|
|| !desktopOrigin(event.senderFrame.url, origin)) throw desktopError('本机操作只能由当前工作台主窗口发起。');
|
|
}
|
|
export function desktopInput(action, input) {
|
|
const fields = { status: [], pickProject: [], stopHost: [], projectFolder: ['projectId'], reviewTask: ['projectId', 'sessionId', 'taskId'],
|
|
openFilePreview: ['projectId', 'url'], revealFile: ['projectId', 'sessionId', 'taskId', 'path', 'sha256'] }[action];
|
|
if (!fields) throw desktopError('本机操作不存在。');
|
|
const value = input ?? {};
|
|
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).some(key => !fields.includes(key))) throw desktopError('本机操作字段无效。');
|
|
for (const key of fields) {
|
|
if (typeof value[key] !== 'string') throw desktopError('本机操作缺少当前归属。');
|
|
if (key.endsWith('Id') && !/^[a-f0-9-]{36}$/i.test(value[key])) throw desktopError('项目或任务身份无效。');
|
|
}
|
|
if (action === 'revealFile' && (!/^[a-f0-9]{64}$/i.test(value.sha256) || value.path.length > 2000 || !value.path
|
|
|| value.path.includes('\0') || value.path.includes(':') || /^[\/\\]/.test(value.path) || value.path.replaceAll('\\', '/').split('/').includes('..'))) throw desktopError('原文件入口需要所属相对路径及当前SHA256。');
|
|
return value;
|
|
}
|