Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(remote-desktop): guard Linux audio on lock and clipboard offer lifetime
Dash Huangf3de2a90e225
diff --git a/apps/desktop/native/remote-desktop/linux-clipboard/main.c b/apps/desktop/native/remote-desktop/linux-clipboard/main.cindex 8a9ea4e979..bf337de2e7 100644--- a/apps/desktop/native/remote-desktop/linux-clipboard/main.c+++ b/apps/desktop/native/remote-desktop/linux-clipboard/main.c@@ -64,17 +64,29 @@ static void replaced(void *d, struct zwlr_data_control_source_v1 *s) {}static const struct zwlr_data_control_source_v1_listener source_listener = {send_data, replaced};+static void offered_mime(void *d, struct zwlr_data_control_offer_v1 *o,+ const char *mime) {+ (void)d;+ (void)o;+ (void)mime;+}+static const struct zwlr_data_control_offer_v1_listener offer_listener = {+ .offer = offered_mime};static void offer(void *d, struct zwlr_data_control_device_v1 *s,struct zwlr_data_control_offer_v1 *o) {(void)d;(void)s;- zwlr_data_control_offer_v1_destroy(o);+ /* MIME events precede selection; keep the proxy until that event arrives. */+ zwlr_data_control_offer_v1_add_listener(o, &offer_listener, NULL);}static void selection(void *d, struct zwlr_data_control_device_v1 *s,struct zwlr_data_control_offer_v1 *o) {(void)d;(void)s;- (void)o;+ /* This write-only helper never receives selection contents. Both regular+ * and primary selections can be discarded once their announcement ends. */+ if (o)+ zwlr_data_control_offer_v1_destroy(o);}static void finished(void *d, struct zwlr_data_control_device_v1 *s) {(void)d;diff --git a/apps/desktop/native/remote-desktop/linux-clipboard/test.mjs b/apps/desktop/native/remote-desktop/linux-clipboard/test.mjsindex d382fd1723..553e365109 100644--- a/apps/desktop/native/remote-desktop/linux-clipboard/test.mjs+++ b/apps/desktop/native/remote-desktop/linux-clipboard/test.mjs@@ -19,11 +19,37 @@ test('clipboard parser bounds formats and binary data without a compositor', ()fs.writeFileSync(target,`+#include "wlr-data-control-unstable-v1.h"+#include <assert.h>+struct fake_offer { int destroyed; const struct zwlr_data_control_offer_v1_listener *listener; };+static void destroy_offer(struct zwlr_data_control_offer_v1 *offer) {+ struct fake_offer *fake=(struct fake_offer *)offer;+ assert(!fake->destroyed); fake->destroyed=1;+}+static int listen_offer(struct zwlr_data_control_offer_v1 *offer, const struct zwlr_data_control_offer_v1_listener *listener, void *data) {+ (void)data; ((struct fake_offer *)offer)->listener=listener; return 0;+}+#define zwlr_data_control_offer_v1_destroy destroy_offer+#define zwlr_data_control_offer_v1_add_listener listen_offer#define main clipboard_main#include ${JSON.stringify(path.join(source, 'main.c'))}#undef main#include <assert.h>int main(void) {+ for (int primary=0;primary<2;primary++) {+ struct fake_offer fake={0};+ struct zwlr_data_control_offer_v1 *o=(struct zwlr_data_control_offer_v1 *)&fake;+ device_listener.data_offer(NULL,NULL,o);+ assert(!fake.destroyed && fake.listener);+ fake.listener->offer(NULL,o,"text/plain");+ fake.listener->offer(NULL,o,"text/html");+ assert(!fake.destroyed);+ if (primary) device_listener.primary_selection(NULL,NULL,o);+ else device_listener.selection(NULL,NULL,o);+ assert(fake.destroyed);+ }+ device_listener.selection(NULL,NULL,NULL);+ device_listener.primary_selection(NULL,NULL,NULL);unsigned char *bytes=NULL;size_t length=0;assert(!decode("YQ==",4,&bytes,&length) && length==1 && bytes[0]=='a');free(bytes);assert(decode("Y!==",4,&bytes,&length)==-1);diff --git a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.tsindex 85daacea8c..9bc9cd2fff 100644--- a/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts+++ b/apps/desktop/src/main/remote-desktop/__tests__/captureBoundary.test.ts@@ -14,6 +14,8 @@ const h = vi.hoisted(() => ({hyprland: false,linuxInput: false,linuxAudio: false,+ unlocked: vi.fn(async () => true),+ powerHandlers: new Map<string, () => void>(),audioRead: vi.fn(() => new Uint8Array(8)),audioStart: vi.fn(),audioStop: vi.fn(),@@ -62,6 +64,7 @@ vi.mock('../linuxDesktop', () => ({linuxDisplay: () => ({ id: 'hyprland:eDP-2', name: 'eDP-2', width: 1600, height: 1000 }),}));vi.mock('../linuxCapture', () => ({ readLinuxCursorSupport: async () => false }));+vi.mock('../linuxSessionLock', () => ({ isLinuxDesktopUnlocked: h.unlocked }));vi.mock('../linuxAudio', () => ({supportsLinuxAudio: () => h.linuxAudio,LinuxDesktopAudio: class {@@ -94,7 +97,7 @@ vi.mock('../viewerDisplay', () => ({}));vi.mock('electron', () => ({app: { on: vi.fn() },- powerMonitor: { on: vi.fn() },+ powerMonitor: { on: (name: string, fn: () => void) => h.powerHandlers.set(name, fn) },shell: {},nativeImage: {},screen: {@@ -248,6 +251,7 @@ beforeEach(() => {h.hyprland = false;h.linuxInput = false;h.linuxAudio = false;+ h.unlocked.mockReset().mockResolvedValue(true);h.audioRead.mockClear();h.audioStart.mockClear();h.audioStop.mockClear();@@ -950,18 +954,71 @@ it('binds Linux audio to the exact capture window, opted-in video lease and stopexpect(command.nativeAudio).toBe(true);expect(h.audioStart).toHaveBeenCalledOnce();const read = h.handlers.get(DESKTOP_LOCAL.NATIVE_AUDIO);- expect(read(event(), 'lease')).toHaveLength(8);- expect(() => read(event({ mainFrame: {} }), 'lease')).toThrow('PERMISSION_DENIED');- expect(() => read(event(), 'old-lease')).toThrow('PERMISSION_DENIED');+ await expect(read(event(), 'lease')).resolves.toHaveLength(8);+ h.unlocked.mockResolvedValue(false);+ h.audioStop.mockClear();+ await expect(read(event(), 'lease')).rejects.toThrow('PERMISSION_DENIED');+ expect(h.audioStop).toHaveBeenCalledOnce();+ await expect(read(event({ mainFrame: {} }), 'lease')).rejects.toThrow('PERMISSION_DENIED');+ await expect(read(event(), 'old-lease')).rejects.toThrow('PERMISSION_DENIED');h.handlers.get(DESKTOP_LOCAL.REPLY)(event(), command.id, 'answer');await result;const owner = h.owner;h.deps.stopVideo();expect(h.audioStop).toHaveBeenCalled();- expect(() => read(event(owner), 'lease')).toThrow('PERMISSION_DENIED');+ await expect(read(event(owner), 'lease')).rejects.toThrow('PERMISSION_DENIED');expect(h.audioRead).toHaveBeenCalledOnce();});+it('does not start Linux audio while locked or unknown, and denies samples after locking', async () => {+ h.wayland = h.hyprland = h.linuxAudio = true;+ h.unlocked.mockResolvedValue(false);+ const result = h.deps.offer({ lease: 'lease', display: { id: 'wayland-portal' } }, 'sdp', {+ audio: true,+ });+ h.handlers.get(DESKTOP_LOCAL.REGISTER)(event());+ await flush();+ expect(h.audioStart).not.toHaveBeenCalled();+ const command = h.owner.send.mock.calls.at(-1)[1];+ h.handlers.get(DESKTOP_LOCAL.REPLY)(event(), command.id, 'answer');+ await result;+ const read = h.handlers.get(DESKTOP_LOCAL.NATIVE_AUDIO);+ await expect(read(event(), 'lease')).rejects.toThrow('PERMISSION_DENIED');+ expect(h.audioRead).not.toHaveBeenCalled();+ h.audioStop.mockClear();+ h.powerHandlers.get('lock-screen')!();+ expect(h.audioStop).toHaveBeenCalledOnce();+ h.powerHandlers.get('unlock-screen')!();+ expect(h.audioStop).toHaveBeenCalledTimes(2);+ expect(h.audioStart).not.toHaveBeenCalled();+});++it('does not read or stop replacement Linux audio after a delayed lock check', async () => {+ h.wayland = h.hyprland = h.linuxAudio = true;+ const result = h.deps.offer({ lease: 'lease', display: { id: 'wayland-portal' } }, 'sdp', {+ audio: true,+ });+ h.handlers.get(DESKTOP_LOCAL.REGISTER)(event());+ await flush();+ const command = h.owner.send.mock.calls.at(-1)[1];+ h.handlers.get(DESKTOP_LOCAL.REPLY)(event(), command.id, 'answer');+ await result;+ let resolve!: (value: boolean) => void;+ h.unlocked.mockImplementationOnce(+ () =>+ new Promise<boolean>((done) => {+ resolve = done;+ }),+ );+ const pending = h.handlers.get(DESKTOP_LOCAL.NATIVE_AUDIO)(event(), 'lease');+ h.deps.stopVideo();+ h.audioStop.mockClear();+ resolve(false);+ await expect(pending).rejects.toThrow('PERMISSION_DENIED');+ expect(h.audioStop).not.toHaveBeenCalled();+ expect(h.audioRead).not.toHaveBeenCalled();+});+it.each([true, false])('bounds same-screen audio recovery after the initial grant was consumed=%s',async (consumed) => {diff --git a/apps/desktop/src/main/remote-desktop/index.ts b/apps/desktop/src/main/remote-desktop/index.tsindex ef5f2d9c2a..02ed1bfcff 100644--- a/apps/desktop/src/main/remote-desktop/index.ts+++ b/apps/desktop/src/main/remote-desktop/index.ts@@ -56,6 +56,7 @@ import { NativeDesktopCapture } from './nativeCapture';import { HyprlandCapture, supportsHyprlandCapture } from './hyprlandCapture';import { readLinuxDesktopInputSupport } from './linuxInput';import { LinuxDesktopAudio, supportsLinuxAudio } from './linuxAudio';+import { isLinuxDesktopUnlocked } from './linuxSessionLock';import { supportsLinuxClipboard, stopLinuxClipboardWriter } from './linuxClipboardNative';import {supportsLinuxDisplay,@@ -364,7 +365,13 @@ async function offer(throw new Error('DESKTOP_LEASE_EXPIRED');nativeSettings = settings;setVideoLease(lease.lease);- if (hyprland && settings?.audio) linuxAudio.start();+ if (hyprland && settings?.audio) {+ const unlocked = await isLinuxDesktopUnlocked();+ if (!current() || host !== currentHost || currentHost.isDestroyed())+ throw new Error('DESKTOP_LEASE_EXPIRED');+ if (unlocked) linuxAudio.start();+ else linuxAudio.stop();+ }videoAttempt = attemptId;const result = await requestHost({@@ -820,6 +827,7 @@ export function registerRemoteDesktopIpc(const sessionChanged = () => {nativeCapture.stop(); // do not retain pixels from the previous OS session statehyprlandCapture.stop();+ linuxAudio.stop(); // Clear buffered PCM as well as pixels across lock transitions.if (remoteDesktop.state?.controlling) {try {input.input([{ kind: 'release' }]);@@ -855,7 +863,7 @@ export function registerRemoteDesktopIpc(if (generation !== offerGeneration || !remoteDesktop.hasLease(lease)) return null;return jpeg;});- ipcMain.handle(DESKTOP_LOCAL.NATIVE_AUDIO, (event, lease: unknown) => {+ ipcMain.handle(DESKTOP_LOCAL.NATIVE_AUDIO, async (event, lease: unknown) => {captureWindow.assertSender(event);if (event.sender !== host ||@@ -867,6 +875,15 @@ export function registerRemoteDesktopIpc()throwIpcError('PERMISSION_DENIED', 'Invalid desktop audio lease');try {+ const generation = offerGeneration;+ const unlocked = await isLinuxDesktopUnlocked();+ // A late check must not read or stop a replacement capture's audio.+ if (generation !== offerGeneration || lease !== videoLease || !remoteDesktop.hasLease(lease))+ throw new Error('DESKTOP_LEASE_EXPIRED');+ if (!unlocked) {+ linuxAudio.stop();+ throw new Error('DESKTOP_LOCKED');+ }return linuxAudio.read();} catch {throwIpcError('PERMISSION_DENIED', 'Desktop audio unavailable');