Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## 2024-06-30 - Replace dangerouslySetInnerHTML with Sandboxed iframe for Untrusted HTML

**Vulnerability:** Used `dangerouslySetInnerHTML` to render dynamic, untrusted email HTML within a React component.
**Learning:** Even when using a library like `DOMPurify` to sanitize HTML, rendering it directly into the main application document using `dangerouslySetInnerHTML` carries inherent risk. If the sanitizer fails to catch a novel exploit, is misconfigured, or if the sanitization rules change over time, the application is completely exposed to XSS, which can lead to session hijacking and unauthorized actions within the primary app context.
**Prevention:** For untrusted or dynamic HTML (like email previews), always use a sandboxed `iframe` with the `srcDoc` attribute to create an isolated browsing context. Apply `sandbox="allow-popups allow-popups-to-escape-sandbox"` to permit expected interactions (like clicking links) while omitting `allow-scripts` and `allow-same-origin`. Because the iframe is isolated, external CSS (like Tailwind classes) won't apply to its contents; any necessary styling must be explicitly injected via an inline `<style>` tag within the `srcDoc` string.
32 changes: 30 additions & 2 deletions components/accounts/ppp-savings-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ export function PppSavingsPanel({ orgSlug, accountId }: PppSavingsPanelProps) {
return `mailto:${encodeURIComponent(report.recipientEmail)}?subject=${encodeURIComponent(report.email.subject)}&body=${encodeURIComponent(draft)}`;
}, [draft, report]);
const sanitizedEmailHtml = useMemo(() => (report?.email.html ? DOMPurify.sanitize(report.email.html) : ""), [report]);
const previewHtml = useMemo(() => {
if (!sanitizedEmailHtml) return "";
return `
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: ui-sans-serif, system-ui, sans-serif;
padding: 0;
margin: 0;
color: #000;
font-size: 0.875rem;
background: #fff;
}
</style>
</head>
<body>
${sanitizedEmailHtml}
</body>
</html>
`;
}, [sanitizedEmailHtml]);

const pdfHref = report ? `/api/runtime/organizations/${orgSlug}/accounts/${accountId}/ppp-savings/pdf?year=${report.year}` : null;

Expand Down Expand Up @@ -232,8 +255,13 @@ export function PppSavingsPanel({ orgSlug, accountId }: PppSavingsPanelProps) {
)}
</div>
</div>
<div className="min-h-[24rem] w-full overflow-auto bg-white p-6 text-sm text-black">
<div dangerouslySetInnerHTML={{ __html: sanitizedEmailHtml }} />
<div className="min-h-[24rem] w-full bg-white p-0">
<iframe
title="Email preview"
srcDoc={previewHtml}
className="h-full min-h-[24rem] w-full border-none p-6"
sandbox="allow-popups allow-popups-to-escape-sandbox"
/>
</div>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading