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(mail): 移除不可打开目标的阅读链接语义

Dash8be8806ec099
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx
index 80103fa3aa..05e6102006 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/MailHtmlPreview.test.tsx
@@ -1,5 +1,5 @@
// @vitest-environment jsdom
-import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
+import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react';
import { afterEach, expect, it, vi } from 'vitest';
import { MailHtmlPreview, safeMailHtml } from '../MailHtmlPreview';
import { GhostCardForm } from '../GhostCardForm';
@@ -7,6 +7,7 @@ import { ChatSessionFileProvider } from '../ChatSessionFileContext';
import { openUrlInSidebarBrowser } from '@/features/right-sidebar/lib/openInSidebarBrowser';
import { _resetLinkOpenPreferenceForTests } from '@/hooks/useLinkOpenPreference';
import { mailInlineBindingScript, mailInlineRefreshScript } from '@cindy/maker-shared/mail-text-edit';
+import { mailBodySerializer } from '../mailBodyEditing';
vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
vi.mock('../ImageLightbox', () => ({ ImageLightbox: ({ src }: { src: string }) => <img alt="attachment-preview" src={src} /> }));
vi.mock('@/features/right-sidebar/lib/openInSidebarBrowser', () => ({ openUrlInSidebarBrowser: vi.fn(async () => undefined) }));
@@ -92,9 +93,11 @@ it('opens reader links with chat preferences and modifiers but never navigates t
// Reader links must remain visibly clickable despite sender CSS; preview paint
// must also leave authored styles intact when this frame becomes an editor.
+// Unit regression for #7062: inert mailto/credential targets must not expose
+// keyboard/AT links; prior tests checked only paint and swallowed clicks.
it('keeps link affordances in both themes and restores source styles for editing', async () => {
const root = document.documentElement, originalClass = root.className, originalStyle = root.getAttribute('style');
- const value = '<style>a{text-decoration:none!important}</style><p><a style="text-decoration:none!important;color:red" href="https://example.test/page">Web</a><a style="text-decoration:underline!important" href="mailto:[email protected]">Email</a></p>';
+ const value = '<style>a{text-decoration:none!important}</style><p><a style="text-decoration:none!important;color:red" href="https://example.test/page">Web</a><a style="text-decoration:underline!important" href="mailto:[email protected]">Email</a><a role="link" href="https://user:[email protected]">Credentials</a></p>';
const change = vi.fn();
try {
root.classList.remove('dark');
@@ -113,8 +116,17 @@ it('keeps link affordances in both themes and restores source styles for editing
expect(web.style.cursor).toBe('pointer');
expect(email.style.textDecoration).toBe('none');
expect(email.style.cursor).toBe('text');
+ expect(email.hasAttribute('href')).toBe(false);
+ expect(within(doc.body).getAllByRole('link')).toEqual([web]);
}
+ const targets = (html: string) => [...new DOMParser().parseFromString(html, 'text/html').querySelectorAll('a')]
+ .map(link => [link.getAttribute('href'), link.getAttribute('role')]);
+ const originalTargets = targets(value);
+ // Serialization restores a clone without reactivating the live reader.
+ expect(targets(mailBodySerializer(doc, value)()!.html)).toEqual(originalTargets);
+ expect(within(doc.body).getAllByRole('link')).toHaveLength(1);
view.rerender(<MailHtmlPreview value={value} reading onChange={change} />);
+ expect([...doc.querySelectorAll('a')].map(link => [link.getAttribute('href'), link.getAttribute('role')])).toEqual(originalTargets);
expect(doc.querySelector('a')!.style.textDecoration).toBe('none');
expect(doc.querySelector('a')!.style.color).toBe('red');
fireEvent.input(doc.querySelector('p')!);
@@ -123,6 +135,10 @@ it('keeps link affordances in both themes and restores source styles for editing
expect(link.style.textDecoration).toBe('none');
expect(link.style.color).toBe('red');
expect(saved).not.toContain('cursor: pointer');
+ expect(targets(saved)).toEqual(originalTargets);
+ expect(saved).not.toContain('data-cindy-mail-authored-style');
+ view.rerender(<MailHtmlPreview value={value} reading />);
+ expect(within(doc.body).getAllByRole('link')).toHaveLength(1);
} finally {
cleanup(); root.className = originalClass;
if (originalStyle === null) root.removeAttribute('style'); else root.setAttribute('style', originalStyle);
diff --git a/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts b/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts
index 9c4acbb071..aa68aa2d37 100644
--- a/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts
+++ b/apps/desktop/src/renderer/components/chat/mailPreviewAppearance.ts
@@ -1,5 +1,11 @@
const authoredStyleAttribute = 'data-cindy-mail-authored-style';
+function rememberMailPreviewPaint(el: Element, style = el.getAttribute('style')) {
+ if (el.hasAttribute(authoredStyleAttribute)) return;
+ el.setAttribute(authoredStyleAttribute, JSON.stringify(el.tagName === 'A'
+ ? { style, href: el.getAttribute('href'), role: el.getAttribute('role') } : style));
+}
+
/** Mail readers share the chat browser policy only for ordinary web links. */
export function mailPreviewLinkUrl(value: string | null): string | undefined {
try {
@@ -16,10 +22,15 @@ export function restoreMailPreviewPaint(root: Element): void {
if (encoded === null) continue;
el.removeAttribute(authoredStyleAttribute);
try {
- const style: unknown = JSON.parse(encoded);
- if (style === null) el.removeAttribute('style');
- else if (typeof style === 'string') el.setAttribute('style', style);
- } catch { /* Invalid transient metadata is never interpreted as a style. */ }
+ const original: unknown = JSON.parse(encoded);
+ // Accept the original style-only format as well as link metadata.
+ const attributes = original && typeof original === 'object' ? original : { style: original };
+ for (const name of ['style', 'href', 'role']) {
+ const value = (attributes as Record<string, unknown>)[name];
+ if (value === null) el.removeAttribute(name);
+ else if (typeof value === 'string') el.setAttribute(name, value);
+ }
+ } catch { /* Invalid transient metadata is never applied. */ }
}
}
@@ -30,10 +41,14 @@ export function followMailPreviewTheme(doc: Document, host: Document, reading =
if (!reading) return;
for (const link of doc.querySelectorAll<HTMLElement>('a')) {
for (const el of [link, ...link.querySelectorAll<HTMLElement>('*')]) {
- if (!el.hasAttribute(authoredStyleAttribute)) el.setAttribute(authoredStyleAttribute, JSON.stringify(el.getAttribute('style')));
+ rememberMailPreviewPaint(el);
el.style.setProperty('color', 'inherit', 'important');
}
const clickable = Boolean(mailPreviewLinkUrl(link.getAttribute('href')));
+ if (!clickable) {
+ link.removeAttribute('href');
+ if (link.getAttribute('role')?.toLowerCase().split(/\s+/).includes('link')) link.removeAttribute('role');
+ }
link.style.setProperty('text-decoration', clickable ? 'underline' : 'none', 'important');
link.style.setProperty('cursor', clickable ? 'pointer' : 'text', 'important');
}
@@ -75,7 +90,7 @@ export function followMailPreviewTheme(doc: Document, host: Document, reading =
const { el } = entry;
// Native Enter/split/undo can clone these nodes. Carry the authored
// style with the clone so neither saving nor cleanup relies on identity.
- el.setAttribute(authoredStyleAttribute, JSON.stringify(entry.style));
+ rememberMailPreviewPaint(el, entry.style);
const set = (name: string, value: string) => el.style.setProperty(name, value, 'important');
const background = colorInfo(entry.background), foreground = colorInfo(entry.color);
if (background && background.brightness > 0.25) {
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 2ae2b87572..635e9632ea 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -3195,8 +3195,9 @@ continue displaying addresses alone. Provider contact enrichment must remain opt
when new contact scopes are missing; Gmail bounds its lookup budget and account cache.
Desktop activates only HTTP(S) body links through the chat opening policy and keeps
their underline visible despite sender CSS. Unsupported targets, including mailto,
-stay inert; this does not widen the system shell protocol allowlist. Reader paint
-is transient and restored before editing or serializing a draft.
+stay inert without an href or explicit link role in the reader, so they are not
+keyboard/screen-reader links. Reader paint and authored link attributes are restored
+before editing or serializing a draft; the system shell protocol allowlist is unchanged.
Credentials and MIME parsing remain with the provider. Cindy validates the data
and reuses its inert HTML/CID/recipient renderers for delivered mail. Multiple
matching providers/accounts cannot silently select the first. Host setup metadata