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(desktop): restore Review artifact consent actions (#3538)

DavidShenXDaa4f069c64a0
diff --git a/apps/desktop/src/main/reviewer/__tests__/reviewArtifactConfirmWindow.test.ts b/apps/desktop/src/main/reviewer/__tests__/reviewArtifactConfirmWindow.test.ts
index d81c0028ac..9a0d89b207 100644
--- a/apps/desktop/src/main/reviewer/__tests__/reviewArtifactConfirmWindow.test.ts
+++ b/apps/desktop/src/main/reviewer/__tests__/reviewArtifactConfirmWindow.test.ts
@@ -28,10 +28,16 @@ class FakeWebContents extends EventEmitter {
readonly id = nextWebContentsId++;
loadedUrl = '';
openHandler: (() => { action: 'deny' }) | null = null;
+ executedScripts: string[] = [];
setWindowOpenHandler(handler: () => { action: 'deny' }): void {
this.openHandler = handler;
}
+
+ executeJavaScript(script: string): Promise<unknown> {
+ this.executedScripts.push(script);
+ return Promise.resolve(undefined);
+ }
}
class FakeWindow extends EventEmitter {
@@ -68,25 +74,19 @@ function createHarness(timeoutMs = 60_000) {
const parent = new FakeWindow();
const dialog = new FakeWindow();
let windowOptions: BrowserWindowConstructorOptions | null = null;
- const result = showReviewArtifactConfirmWindow(
- parent as unknown as BrowserWindow,
- MODEL,
- {
- timeoutMs,
- isDark: false,
- createWindow: (options) => {
- windowOptions = options;
- return dialog as unknown as BrowserWindow;
- },
+ const result = showReviewArtifactConfirmWindow(parent as unknown as BrowserWindow, MODEL, {
+ timeoutMs,
+ isDark: false,
+ createWindow: (options) => {
+ windowOptions = options;
+ return dialog as unknown as BrowserWindow;
},
- );
+ });
return { parent, dialog, result, getWindowOptions: () => windowOptions };
}
function readEmbeddedStylesheet(document: string): string {
- const match = document.match(
- /<link rel="stylesheet" href="data:text\/css;base64,([^"]+)">/,
- );
+ const match = document.match(/<link rel="stylesheet" href="data:text\/css;base64,([^"]+)">/);
expect(match).not.toBeNull();
return Buffer.from(match?.[1] ?? '', 'base64').toString('utf8');
}
@@ -115,13 +115,18 @@ describe('Review artifact confirmation window', () => {
expect(document).toContain('data-theme="dark"');
expect(document).toContain("default-src 'none'; style-src data:");
+ expect(document).toContain('form-action cindy-review-artifact-confirm:');
expect(document).not.toContain("'unsafe-inline'");
expect(document).not.toContain('<script>');
expect(document).not.toContain('<img');
expect(document).not.toContain('<svg');
expect(document).toContain('&lt;script&gt;bad&lt;/script&gt;');
- expect(document).toContain('class="button primary" href="#allow" autofocus');
- expect(document).not.toContain('href="#cancel" autofocus');
+ expect(document).toContain('action="cindy-review-artifact-confirm://allow" method="get"');
+ expect(document).toContain(
+ 'id="review-confirm-allow" class="button primary" type="submit" autofocus',
+ );
+ expect(document).toContain('action="cindy-review-artifact-confirm://cancel" method="get"');
+ expect(document).not.toContain('href="#');
});
it('uses a hardened modal with no preload or renderer authorization bridge', async () => {
@@ -153,18 +158,30 @@ describe('Review artifact confirmation window', () => {
harness.dialog.emit('ready-to-show');
expect(harness.dialog.shown).toBe(true);
expect(harness.dialog.focused).toBe(true);
+ expect(harness.dialog.webContents.executedScripts).toEqual([
+ "document.getElementById('review-confirm-allow')?.focus()",
+ ]);
const preventDefault = vi.fn();
harness.dialog.webContents.emit('will-navigate', { preventDefault }, 'https://example.com');
expect(preventDefault).toHaveBeenCalledOnce();
+ const malformedDecisionPreventDefault = vi.fn();
harness.dialog.webContents.emit(
- 'did-navigate-in-page',
- {},
- `${harness.dialog.webContents.loadedUrl}#allow`,
- true,
+ 'will-navigate',
+ { preventDefault: malformedDecisionPreventDefault },
+ 'cindy-review-artifact-confirm://allow/forged',
+ );
+ expect(malformedDecisionPreventDefault).toHaveBeenCalledOnce();
+
+ const allowPreventDefault = vi.fn();
+ harness.dialog.webContents.emit(
+ 'will-navigate',
+ { preventDefault: allowPreventDefault },
+ 'cindy-review-artifact-confirm://allow?',
);
await expect(harness.result).resolves.toBe(true);
+ expect(allowPreventDefault).toHaveBeenCalledOnce();
expect(harness.dialog.destroyed).toBe(true);
});
@@ -192,13 +209,14 @@ describe('Review artifact confirmation window', () => {
it('fails closed on cancel, timeout, parent close, and renderer failure', async () => {
const cancelled = createHarness();
+ const cancelPreventDefault = vi.fn();
cancelled.dialog.webContents.emit(
- 'did-navigate-in-page',
- {},
- `${cancelled.dialog.webContents.loadedUrl}#cancel`,
- true,
+ 'will-navigate',
+ { preventDefault: cancelPreventDefault },
+ 'cindy-review-artifact-confirm://cancel?',
);
await expect(cancelled.result).resolves.toBe(false);
+ expect(cancelPreventDefault).toHaveBeenCalledOnce();
vi.useFakeTimers();
const timedOut = createHarness(10);
diff --git a/apps/desktop/src/main/reviewer/reviewArtifactConfirmWindow.ts b/apps/desktop/src/main/reviewer/reviewArtifactConfirmWindow.ts
index 730b8153a5..ced4189f80 100644
--- a/apps/desktop/src/main/reviewer/reviewArtifactConfirmWindow.ts
+++ b/apps/desktop/src/main/reviewer/reviewArtifactConfirmWindow.ts
@@ -1,8 +1,4 @@
-import {
- BrowserWindow,
- nativeTheme,
- type BrowserWindowConstructorOptions,
-} from 'electron';
+import { BrowserWindow, nativeTheme, type BrowserWindowConstructorOptions } from 'electron';
import { resolveAppThemeIsDark } from '../resolved-app-theme.js';
import { readWindowThemeSnapshot } from '../window-theme-mode-store.js';
@@ -10,6 +6,10 @@ import type { ReviewArtifactConfirmDialogModel } from './reviewArtifactDialog.js
import { markReviewArtifactConfirmWebContentsId } from './reviewArtifactConfirmWindowRegistry.js';
const DEFAULT_TIMEOUT_MS = 90_000;
+const DECISION_PROTOCOL = 'cindy-review-artifact-confirm:';
+const ALLOW_DECISION_URL = `${DECISION_PROTOCOL}//allow`;
+const CANCEL_DECISION_URL = `${DECISION_PROTOCOL}//cancel`;
+const FOCUS_PRIMARY_ACTION_SCRIPT = "document.getElementById('review-confirm-allow')?.focus()";
export interface ReviewArtifactConfirmWindowOptions {
timeoutMs?: number;
@@ -68,13 +68,14 @@ li { margin-top: 8px; padding: 11px 12px; border: 1px solid var(--border); borde
.label { overflow-wrap: anywhere; font-size: 13px; font-weight: 500; }
.path { margin-top: 4px; overflow-wrap: anywhere; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; }
footer { display: flex; flex: none; justify-content: flex-end; gap: 10px; margin-top: auto; padding-top: 26px; }
-.button { display: inline-flex; min-width: 88px; height: 36px; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 8px; color: var(--text); font-size: 13px; font-weight: 600; text-decoration: none; }
+form { margin: 0; }
+.button { appearance: none; display: inline-flex; min-width: 88px; height: 36px; padding: 0; align-items: center; justify-content: center; border: 1px solid var(--border); border-radius: 9999px; background: transparent; color: var(--text); cursor: pointer; font-family: inherit; font-size: 13px; font-weight: 600; }
.button:hover, .button:focus-visible { background: var(--hover); outline: none; }
.primary { border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
.primary:hover, .primary:focus-visible { background: var(--accent); opacity: .88; }
`;
-/** Build a script-free document whose only decisions are same-document fragments. */
+/** Build a script-free document whose forms can only target the private decision protocol. */
export function buildReviewArtifactConfirmDocument(
model: ReviewArtifactConfirmDialogModel,
isDark: boolean,
@@ -82,14 +83,9 @@ export function buildReviewArtifactConfirmDocument(
const stylesheet = `data:text/css;base64,${Buffer.from(DIALOG_CSS).toString('base64')}`;
const items = model.items
.map((item) => {
- const secondary =
- item.kind === 'external-path'
- ? item.path
- : item.inlineLabel;
+ const secondary = item.kind === 'external-path' ? item.path : item.inlineLabel;
return `<li><div class="label">${escapeHtml(item.label)}</div>${
- secondary
- ? `<div class="path" dir="ltr">${escapeHtml(secondary)}</div>`
- : ''
+ secondary ? `<div class="path" dir="ltr">${escapeHtml(secondary)}</div>` : ''
}</li>`;
})
.join('');
@@ -97,7 +93,7 @@ export function buildReviewArtifactConfirmDocument(
<html lang="und" data-theme="${isDark ? 'dark' : 'light'}">
<head>
<meta charset="utf-8">
- <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src data:; base-uri 'none'; form-action 'none'; frame-ancestors 'none'">
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src data:; base-uri 'none'; form-action ${DECISION_PROTOCOL}; frame-ancestors 'none'">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${stylesheet}">
<title>${escapeHtml(model.title)}</title>
@@ -110,8 +106,12 @@ export function buildReviewArtifactConfirmDocument(
<p class="detail">${escapeHtml(model.detail)}</p>
<ul>${items}</ul>
<footer>
- <a class="button" href="#cancel">${escapeHtml(model.cancelText)}</a>
- <a class="button primary" href="#allow" autofocus>${escapeHtml(model.allowText)}</a>
+ <form action="${CANCEL_DECISION_URL}" method="get">
+ <button class="button" type="submit">${escapeHtml(model.cancelText)}</button>
+ </form>
+ <form action="${ALLOW_DECISION_URL}" method="get">
+ <button id="review-confirm-allow" class="button primary" type="submit" autofocus>${escapeHtml(model.allowText)}</button>
+ </form>
</footer>
</main>
</body>
@@ -120,9 +120,20 @@ export function buildReviewArtifactConfirmDocument(
function readDecision(url: string): boolean | null {
try {
- const hash = new URL(url).hash;
- if (hash === '#allow') return true;
- if (hash === '#cancel') return false;
+ const parsed = new URL(url);
+ if (
+ parsed.protocol !== DECISION_PROTOCOL ||
+ parsed.username !== '' ||
+ parsed.password !== '' ||
+ parsed.port !== '' ||
+ parsed.pathname !== '' ||
+ parsed.search !== '' ||
+ parsed.hash !== ''
+ ) {
+ return null;
+ }
+ if (parsed.hostname === 'allow') return true;
+ if (parsed.hostname === 'cancel') return false;
} catch {
// Invalid or non-URL navigation is denied below.
}
@@ -133,8 +144,10 @@ function readDecision(url: string): boolean | null {
* One authorization request owns one modal window. Reusing this security
* prompt would risk carrying a stale parent, focus, or decision into another
* grant, so it intentionally does not use the reusable auxiliary-tool window
- * lifecycle. The document has no preload, IPC, or script: an XSS in the main
- * app Renderer cannot observe or answer this prompt.
+ * lifecycle. The document has no preload, IPC, or embedded script: an XSS in
+ * the main app Renderer cannot observe or answer this prompt. Main only runs a
+ * fixed command that focuses the primary button; every decision still arrives
+ * as a denied navigation and is settled here.
*/
export async function showReviewArtifactConfirmWindow(
parent: BrowserWindow,
@@ -150,7 +163,8 @@ export async function showReviewArtifactConfirmWindow(
persistedTheme?.mode,
persistedTheme?.resolvedIsDark,
);
- const createWindow = options.createWindow ?? ((windowOptions) => new BrowserWindow(windowOptions));
+ const createWindow =
+ options.createWindow ?? ((windowOptions) => new BrowserWindow(windowOptions));
let win: BrowserWindow;
try {
win = createWindow({
@@ -201,7 +215,6 @@ export async function showReviewArtifactConfirmWindow(
win.removeListener('closed', onClosed);
win.removeListener('ready-to-show', onReadyToShow);
win.webContents.removeListener('will-navigate', onWillNavigate);
- win.webContents.removeListener('did-navigate-in-page', onDidNavigateInPage);
win.webContents.removeListener('before-input-event', onBeforeInput);
win.webContents.removeListener('render-process-gone', onRendererGone);
if (!win.isDestroyed()) win.destroy();
@@ -215,16 +228,21 @@ export async function showReviewArtifactConfirmWindow(
// resources, so ready-to-show is also its business-content ready signal.
win.show();
win.focus();
+ try {
+ void win.webContents.executeJavaScript(FOCUS_PRIMARY_ACTION_SCRIPT, true).catch((error) => {
+ if (win.isDestroyed()) return;
+ options.log?.warn('failed to focus Review artifact confirmation action', {
+ error: error instanceof Error ? error.message : String(error),
+ });
+ });
+ } catch (error) {
+ options.log?.warn('failed to focus Review artifact confirmation action', {
+ error: error instanceof Error ? error.message : String(error),
+ });
+ }
};
- const onWillNavigate = (event: { preventDefault(): void }): void => {
+ const onWillNavigate = (event: { preventDefault(): void }, url: string): void => {
event.preventDefault();
- };
- const onDidNavigateInPage = (
- _event: unknown,
- url: string,
- isMainFrame: boolean,
- ): void => {
- if (!isMainFrame) return;
const decision = readDecision(url);
if (decision !== null) settle(decision);
};
@@ -242,7 +260,6 @@ export async function showReviewArtifactConfirmWindow(
win.once('closed', onClosed);
win.once('ready-to-show', onReadyToShow);
win.webContents.on('will-navigate', onWillNavigate);
- win.webContents.on('did-navigate-in-page', onDidNavigateInPage);
win.webContents.on('before-input-event', onBeforeInput);
win.webContents.on('render-process-gone', onRendererGone);
win.webContents.setWindowOpenHandler(() => ({ action: 'deny' }));
@@ -255,14 +272,14 @@ export async function showReviewArtifactConfirmWindow(
const document = buildReviewArtifactConfirmDocument(model, isDark);
try {
- void win.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(document)}`).catch(
- (error) => {
+ void win
+ .loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(document)}`)
+ .catch((error) => {
options.log?.warn('failed to load Review artifact confirmation; access denied', {
error: error instanceof Error ? error.message : String(error),
});
settle(false);
- },
- );
+ });
} catch (error) {
options.log?.warn('failed to load Review artifact confirmation; access denied', {
error: error instanceof Error ? error.message : String(error),