Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(remote-desktop): bound permissions and fallback frame failures

Dashed798f19475f
diff --git a/apps/desktop/src/main/__tests__/mainWindowBackgroundThrottling.test.ts b/apps/desktop/src/main/__tests__/mainWindowBackgroundThrottling.test.ts
index bd424a9dcc..5442c3fb4f 100644
--- a/apps/desktop/src/main/__tests__/mainWindowBackgroundThrottling.test.ts
+++ b/apps/desktop/src/main/__tests__/mainWindowBackgroundThrottling.test.ts
@@ -58,7 +58,7 @@ describe('主 BrowserWindow 后台节流', () => {
expect(source).toContain('const nextAllowed = !hasRunningTurn;');
expect(source).toMatch(/setBackgroundThrottling\(\s*mainWindowBackgroundThrottlingAllowed,?\s*\)/);
expect(source).not.toContain('isRemoteDesktopVideoActive');
- expect(source).toContain('registerRemoteDesktopIpc();');
+ expect(source).toContain('registerRemoteDesktopIpc(isGlobalVoiceInputOverlaySender);');
expect(source).toContain('onAnySessionTurnKeepaliveChange: (isRunning) => {');
expect(source).toContain('setMainWindowBackgroundThrottlingForActiveTurn(isRunning);');
expect(source).toContain('notifyUpdateAutoRelaunchBusyStateChanged();');
diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.ts
index 6154bb32fd..08d008f2fe 100644
--- a/apps/desktop/src/main/bootstrap-electron.ts
+++ b/apps/desktop/src/main/bootstrap-electron.ts
@@ -8847,7 +8847,7 @@ app.on('ready', async () => {
// owning modules above; future collections/actions do not add tunnel channels.
registerRemoteResourcesIpc();
registerDeviceLinkIpc();
- registerRemoteDesktopIpc();
+ registerRemoteDesktopIpc(isGlobalVoiceInputOverlaySender);
void startupPurgeDrain
.then(({ purged, pending }) => {
if (purged > 0 || pending > 0) {
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
index 1e2d248664..2159b3157f 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts
@@ -165,6 +165,19 @@ afterEach(() => {
vi.unstubAllGlobals();
});
+it('drops enumeration timeouts for one relay frame and resumes without stopping the host', async () => {
+ vi.stubGlobal('process', { ...process, platform: 'linux' });
+ h.source = new Promise(() => {});
+ const pending = h.deps.frame('1', false);
+ await vi.advanceTimersByTimeAsync(5000);
+ await expect(pending).resolves.toBeNull();
+ expect(h.stop).not.toHaveBeenCalled();
+ h.source = Promise.resolve([]);
+ await expect(h.deps.frame('1', false)).resolves.toBeNull();
+ h.source = Promise.reject(new Error('DESKTOP_DISABLED'));
+ await expect(h.deps.frame('1', false)).rejects.toThrow('DESKTOP_DISABLED');
+});
+
it.each(['success', 'timeout'])('keeps relay polls out of native preparation and resumes after %s', async (outcome) => {
vi.stubGlobal('process', { ...process, platform: 'darwin' });
let sourcesReady!: (sources: any[]) => void;
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/captureWindow.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/captureWindow.test.ts
index d79e89356b..d8515848f3 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/captureWindow.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/captureWindow.test.ts
@@ -24,6 +24,7 @@ vi.mock('electron', () => ({
}),
},
BrowserWindow: class {
+ static fromWebContents(owner: any) { return owner; }
destroyed = false;
webContents = Object.assign(new EventEmitter(), {
mainFrame: { url: '', parent: null },
@@ -46,6 +47,10 @@ vi.mock('electron', () => ({
}
},
}));
+vi.mock('../../security/trustedAppRenderer', () => ({
+ isTrustedAppRendererWindow: (owner: any) => owner?.kind === 'app' && owner.mainFrame.url === 'file:///app/index.html',
+ isTrustedCindyRendererWindow: (owner: any) => owner?.mainFrame.url === 'file:///app/index.html',
+}));
vi.mock('../../utils/ipcValidate', () => ({
throwIpcError: () => {
throw new Error('PERMISSION_DENIED');
@@ -160,20 +165,44 @@ it('denies app display/legacy capture while preserving physical microphone and c
setPermissionCheckHandler: vi.fn(),
setPermissionRequestHandler: vi.fn(),
};
- denyAppDesktopCapture(ses as any);
+ const owner = { kind: 'app', mainFrame: { url: 'file:///app/index.html' }, isDestroyed: () => false };
+ const voice = { ...owner, kind: 'voice' };
+ const details = { isMainFrame: true, requestingUrl: owner.mainFrame.url };
+ denyAppDesktopCapture(ses as any, (candidate) => candidate === voice as any);
const check = ses.setPermissionCheckHandler.mock.calls[0][0];
const request = ses.setPermissionRequestHandler.mock.calls[0][0];
for (const mediaType of ['unknown', undefined])
- expect(check(null, 'media', '', { mediaType })).toBe(false);
+ expect(check(owner, 'media', '', { ...details, mediaType })).toBe(false);
for (const mediaType of ['audio', 'video'])
- expect(check(null, 'media', '', { mediaType })).toBe(true);
+ expect(check(owner, 'media', '', { ...details, mediaType })).toBe(true);
for (const types of [[], undefined, ['unknown']]) {
const reply = vi.fn();
- request(null, 'media', reply, { mediaTypes: types });
+ request(owner, 'media', reply, { ...details, mediaTypes: types });
expect(reply).toHaveBeenCalledWith(false);
}
const reply = vi.fn();
- request(null, 'media', reply, { mediaTypes: ['audio'] });
+ request(owner, 'media', reply, { ...details, mediaTypes: ['audio'] });
+ expect(reply).toHaveBeenLastCalledWith(true);
+ request(voice, 'media', reply, { ...details, mediaTypes: ['audio'] });
+ expect(reply).toHaveBeenLastCalledWith(true);
+ for (const permission of ['notifications', 'geolocation', 'midi', 'hid', 'display-capture', 'unknown']) {
+ expect(check(owner, permission, '', details)).toBe(false);
+ request(owner, permission, reply, details);
+ expect(reply).toHaveBeenLastCalledWith(false);
+ }
+ for (const [candidate, requestDetails] of [
+ [null, details],
+ [{ ...owner, kind: 'foreign' }, details],
+ [owner, { ...details, isMainFrame: false }],
+ [owner, { ...details, requestingUrl: 'https://evil.example/' }],
+ [{ ...voice, mainFrame: { url: 'https://evil.example/' } }, details],
+ ]) {
+ expect(check(candidate, 'media', '', { ...requestDetails, mediaType: 'audio' })).toBe(false);
+ request(candidate, 'media', reply, { ...requestDetails, mediaTypes: ['audio'] });
+ expect(reply).toHaveBeenLastCalledWith(false);
+ }
+ expect(check(owner, 'clipboard-sanitized-write', '', details)).toBe(true);
+ request(owner, 'clipboard-sanitized-write', reply, details);
expect(reply).toHaveBeenLastCalledWith(true);
request(null, 'display-capture', reply, {});
expect(reply).toHaveBeenLastCalledWith(false);
diff --git a/apps/desktop/src/main/remote-desktop/capturePermissions.ts b/apps/desktop/src/main/remote-desktop/capturePermissions.ts
index cc54ecced9..2f958ad41c 100644
--- a/apps/desktop/src/main/remote-desktop/capturePermissions.ts
+++ b/apps/desktop/src/main/remote-desktop/capturePermissions.ts
@@ -1,23 +1,34 @@
-import type { Session } from 'electron';
+import { BrowserWindow, type Session, type WebContents } from 'electron';
+import { isTrustedAppRendererWindow, isTrustedCindyRendererWindow } from '../security/trustedAppRenderer';
/** Remove desktop capture from the app session, including Chromium's legacy
* chromeMediaSource path (reported as media with no physical mediaTypes).
- * Keep microphone/camera requests working; do not change unrelated permissions. */
-export function denyAppDesktopCapture(ses: Session): void {
+ * Keep physical media and clipboard writes confined to trusted top-level pages. */
+export function denyAppDesktopCapture(
+ ses: Session,
+ isVoiceInputOwner: (owner: WebContents) => boolean = () => false,
+): void {
+ const trusted = (owner: WebContents | null, details: { isMainFrame: boolean; requestingUrl?: string }) => {
+ if (!owner || owner.isDestroyed() || !details.isMainFrame || details.requestingUrl !== owner.mainFrame.url)
+ return false;
+ const win = BrowserWindow.fromWebContents(owner);
+ return isTrustedAppRendererWindow(win) || (isVoiceInputOwner(owner) && isTrustedCindyRendererWindow(win));
+ };
ses.setDisplayMediaRequestHandler((_request, callback) => callback({}));
- ses.setPermissionCheckHandler((_owner, permission, _origin, details) => {
+ ses.setPermissionCheckHandler((owner, permission, _origin, details) => {
+ if (!trusted(owner, details)) return false;
if (permission === 'media')
return details.mediaType === 'audio' || details.mediaType === 'video';
- return true;
+ return permission === 'clipboard-sanitized-write';
});
- ses.setPermissionRequestHandler((_owner, permission, callback, details) => {
- if (permission === 'display-capture') return callback(false);
+ ses.setPermissionRequestHandler((owner, permission, callback, details) => {
+ if (!trusted(owner, details)) return callback(false);
if (permission === 'media') {
const types = 'mediaTypes' in details ? details.mediaTypes : undefined;
return callback(
Boolean(types?.length && types.every((type) => type === 'audio' || type === 'video')),
);
}
- callback(true);
+ callback(permission === 'clipboard-sanitized-write');
});
}
diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.ts
index 5385d91a93..5090ab1775 100644
--- a/apps/desktop/src/main/remote-desktop/index.ts
+++ b/apps/desktop/src/main/remote-desktop/index.ts
@@ -327,7 +327,11 @@ export const remoteDesktop = new RemoteDesktopController({
);
return encodeNativeRelayFrame(frame, (jpeg) => nativeImage.createFromBuffer(jpeg));
}
- const source = desktopCaptureSource(await sources(true), displayId, screen.getAllDisplays());
+ const available = await sources(true).catch((error) => {
+ if (error instanceof Error && error.message === 'DESKTOP_VIDEO_TIMEOUT') return [];
+ throw error;
+ });
+ const source = desktopCaptureSource(available, displayId, screen.getAllDisplays());
return source ? encodeDesktopFrame(source.thumbnail) : null;
},
clipboard: (action, text, isCurrent) =>
@@ -354,8 +358,8 @@ export const remoteDesktop = new RemoteDesktopController({
},
});
-export function registerRemoteDesktopIpc(): void {
- denyAppDesktopCapture(session.defaultSession);
+export function registerRemoteDesktopIpc(isVoiceInputOwner?: Parameters<typeof denyAppDesktopCapture>[1]): void {
+ denyAppDesktopCapture(session.defaultSession, isVoiceInputOwner);
const timer = setInterval(() => remoteDesktop.tick(), 1000);
timer.unref();
app.on('before-quit', () => {