|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import DOMPurify from 'dompurify'; |
| 3 | +import { |
| 4 | + EMAIL_IFRAME_SANITIZE_CONFIG, |
| 5 | + applyNewTabToAnchor, |
| 6 | + plainTextToSafeHtml, |
| 7 | + sanitizePlainTextRenderedHtml, |
| 8 | + parseHtmlSafely, |
| 9 | +} from '../email-sanitization'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Regression guard for "email links open in a new tab". The at-risk links are |
| 13 | + * generated by linkification (not in the source) and DOMPurify was silently |
| 14 | + * stripping their target/rel, so they opened in the same tab. Drives both real |
| 15 | + * EmailViewer pipelines end-to-end — plaintext and HTML/iframe — so it can't |
| 16 | + * regress unnoticed. |
| 17 | + */ |
| 18 | + |
| 19 | +/** Reproduce the EmailViewer iframe pipeline for an HTML body. */ |
| 20 | +function renderIframeHtml(html: string): Document { |
| 21 | + DOMPurify.addHook('afterSanitizeAttributes', applyNewTabToAnchor); |
| 22 | + let clean: string; |
| 23 | + try { |
| 24 | + clean = DOMPurify.sanitize(html, EMAIL_IFRAME_SANITIZE_CONFIG); |
| 25 | + } finally { |
| 26 | + DOMPurify.removeAllHooks(); |
| 27 | + } |
| 28 | + const doc = parseHtmlSafely(clean); |
| 29 | + // Post-render walk, exactly as handleIframeLoad does on the live iframe doc. |
| 30 | + doc.querySelectorAll('a').forEach(applyNewTabToAnchor); |
| 31 | + return doc; |
| 32 | +} |
| 33 | + |
| 34 | +/** Reproduce the EmailViewer plaintext pipeline (rendered into the main DOM). */ |
| 35 | +function renderPlaintext(text: string): Document { |
| 36 | + return parseHtmlSafely(sanitizePlainTextRenderedHtml(plainTextToSafeHtml(text))); |
| 37 | +} |
| 38 | + |
| 39 | +const findLink = (doc: Document, hrefIncludes: string): HTMLAnchorElement | undefined => |
| 40 | + Array.from(doc.querySelectorAll('a')).find((a) => (a.getAttribute('href') || '').includes(hrefIncludes)); |
| 41 | + |
| 42 | +describe('email link new-tab behaviour (integration)', () => { |
| 43 | + describe('plaintext body (links are generated, not in the source)', () => { |
| 44 | + it('opens an http(s) URL in a new tab with noopener noreferrer', () => { |
| 45 | + const doc = renderPlaintext('Please visit https://example.com/welcome today.'); |
| 46 | + const link = findLink(doc, 'example.com'); |
| 47 | + expect(link).toBeTruthy(); |
| 48 | + expect(link!.getAttribute('href')).toBe('https://example.com/welcome'); |
| 49 | + expect(link!.getAttribute('target')).toBe('_blank'); |
| 50 | + expect(link!.getAttribute('rel')).toBe('noopener noreferrer'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('does not turn a bare email address into a new-tab link', () => { |
| 54 | + const doc = renderPlaintext('Write to foo@bar.com for help.'); |
| 55 | + // plaintext linkification only targets http(s) URLs, never mailto. |
| 56 | + expect(doc.querySelectorAll('a').length).toBe(0); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('HTML alternative that looks like plaintext (server-generated <a> tags)', () => { |
| 61 | + it('opens http(s) anchors in a new tab and adds noopener noreferrer', () => { |
| 62 | + const doc = renderIframeHtml('Hi<br><a href="http://example.org/page">http://example.org/page</a><br>Bye'); |
| 63 | + const link = findLink(doc, 'example.org'); |
| 64 | + expect(link!.getAttribute('target')).toBe('_blank'); |
| 65 | + expect(link!.getAttribute('rel')).toBe('noopener noreferrer'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does NOT add target=_blank to mailto links', () => { |
| 69 | + const doc = renderIframeHtml('<a href="mailto:sales@example.com">sales@example.com</a>'); |
| 70 | + const link = findLink(doc, 'mailto:'); |
| 71 | + expect(link).toBeTruthy(); |
| 72 | + expect(link!.getAttribute('target')).toBeNull(); |
| 73 | + expect(link!.getAttribute('rel')).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('does NOT add target=_blank to in-page #anchors', () => { |
| 77 | + const doc = renderIframeHtml('<a href="#section">jump</a>'); |
| 78 | + const link = findLink(doc, '#section'); |
| 79 | + expect(link!.getAttribute('target')).toBeNull(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('strips an author-supplied target=_blank from a mailto link', () => { |
| 83 | + const doc = renderIframeHtml('<a href="mailto:x@y.com" target="_blank" rel="noopener noreferrer">x</a>'); |
| 84 | + const link = findLink(doc, 'mailto:'); |
| 85 | + expect(link!.getAttribute('target')).toBeNull(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('handles a mixed body: http gets a new tab, mailto does not', () => { |
| 89 | + const doc = renderIframeHtml( |
| 90 | + 'See <a href="https://docs.example.com">docs</a> or mail <a href="mailto:hi@example.com">us</a>.', |
| 91 | + ); |
| 92 | + expect(findLink(doc, 'docs.example.com')!.getAttribute('target')).toBe('_blank'); |
| 93 | + expect(findLink(doc, 'mailto:')!.getAttribute('target')).toBeNull(); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments