diff --git a/bun.lock b/bun.lock index bb24441..38e29c7 100644 --- a/bun.lock +++ b/bun.lock @@ -11,6 +11,7 @@ "@tanstack/solid-table": "^8.21.3", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", + "dompurify": "^3.4.15", "pdfjs-dist": "^6.1.200", "pixelarticons": "^2.4.1", "solid-js": "^1.9.14", @@ -289,6 +290,8 @@ "@types/node": ["@types/node@26.1.1", "", { "dependencies": { "undici-types": "~8.3.0" } }, "sha512-nxAkRSVkN1Y0JC1W8ky/fTfkGsMmcrRsbx+3XoZE+rMOX71kLYTV7fLXpqud1GpbpP5TuffXFqfX7fH2GgZREw=="], + "@types/trusted-types": ["@types/trusted-types@2.0.7", "", {}, "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw=="], + "@typescript/typescript-aix-ppc64": ["@typescript/typescript-aix-ppc64@7.0.2", "", { "os": "aix", "cpu": "ppc64" }, "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ=="], "@typescript/typescript-darwin-arm64": ["@typescript/typescript-darwin-arm64@7.0.2", "", { "os": "darwin", "cpu": "arm64" }, "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA=="], @@ -389,6 +392,8 @@ "dom-accessibility-api": ["dom-accessibility-api@0.5.16", "", {}, "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg=="], + "dompurify": ["dompurify@3.4.15", "", { "optionalDependencies": { "@types/trusted-types": "^2.0.7" } }, "sha512-EUBjM+B+lkDE41iE82DDSCfkoPGfXx8IxFxPMjNzm/Uk4xDet77rTN9wqlxlVg71kK7XGuUMv6wUxJUwwv+Xyw=="], + "electron-to-chromium": ["electron-to-chromium@1.5.389", "", {}, "sha512-cEto7aeOqBfU1D+c5py5pE+ooscKE75JifxLBdFUZsqAxRS6y7kebtxAZvICszSl05gPjYHDTjY+lXpyGvpJbg=="], "enhanced-resolve": ["enhanced-resolve@5.24.3", "", { "dependencies": { "graceful-fs": "^4.2.4", "tapable": "^2.3.3" } }, "sha512-PwKooW9JUzh5chmYfHM3IQl5OkK2u2Nm011MgeZrss3JmFraUx/fqrf78kk8GUMYoibx/14MdwTl/1WKkG7TpQ=="], diff --git a/package.json b/package.json index f19f901..91887c0 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@tanstack/solid-table": "^8.21.3", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", + "dompurify": "^3.4.15", "pdfjs-dist": "^6.1.200", "pixelarticons": "^2.4.1", "solid-js": "^1.9.14", diff --git a/src/components/homework/homework-submissions-panel.tsx b/src/components/homework/homework-submissions-panel.tsx index 13fa638..1282a01 100644 --- a/src/components/homework/homework-submissions-panel.tsx +++ b/src/components/homework/homework-submissions-panel.tsx @@ -21,6 +21,7 @@ import { fileTypeMeta } from "@/lib/file-type"; import { formatDateTime } from "@/lib/format"; import { formatBytes } from "@/lib/upload-limits"; import { usePreferences, useT } from "@/stores/preferences-context"; +import { sanitizeRichText } from "@/lib/rich-text"; export function HomeworkSubmissionsPanel(props: { homeworkId: string; courseId: string }) { const t = useT(); @@ -185,7 +186,7 @@ export function HomeworkSubmissionsPanel(props: { homeworkId: string; courseId: when={submission().text} fallback={
{t("homework.noAnswer")}
} > - {(text) => } + {(text) => } {/* Skipped entirely (not just an empty placeholder) when there are no files — this is a read-only view, so a "no files" row would just be dead weight. */} diff --git a/src/components/messages/gmail-mail-detail.tsx b/src/components/messages/gmail-mail-detail.tsx index 69cd137..80ad613 100644 --- a/src/components/messages/gmail-mail-detail.tsx +++ b/src/components/messages/gmail-mail-detail.tsx @@ -8,6 +8,7 @@ import { postMessage } from "@/api/messages"; import { formatApiError } from "@/api/client"; import { personLabel } from "@/lib/person"; import { useT } from "@/stores/preferences-context"; +import { sanitizeRichText } from "@/lib/rich-text"; interface GmailMailDetailProps { message: Message; @@ -276,7 +277,7 @@ export function GmailMailDetail(props: GmailMailDetailProps) { {/* Email Body Card */}Hi there
Hi there
"); + }); + + it("drops javascript links and opens real ones in a new tab", () => { + expect(sanitizeRichText('x')).not.toContain("javascript"); + expect(sanitizeRichText('x')).toBe('x'); + }); + + it("turns imported markdown-ish text into blocks", () => { + expect(toRichTextHtml("## Unit 1\n\nFirst line\nsecond line\n\n- a\n- b\n1. one")).toBe( + "First line
second line
a < b
"); + }); + + it("reads excerpts and emptiness from the text, not the markup", () => { + expect(richTextExcerpt("Body text
")).toBe("Title Body text"); + expect(richTextExcerpt("abcdef
", 3)).toBe("abc…"); + expect(isRichTextEmpty("x
")).toBe(false); + }); +}); diff --git a/src/lib/rich-text.ts b/src/lib/rich-text.ts new file mode 100644 index 0000000..5ce1469 --- /dev/null +++ b/src/lib/rich-text.ts @@ -0,0 +1,88 @@ +import DOMPurify from "dompurify"; + +// Note bodies are stored as HTML written by the contentEditable editor, and +// course notes render for every enrolled student — so anything that reaches +// `innerHTML`, stored or pasted, goes through this allow-list first. +const ALLOWED_TAGS = ["p", "div", "br", "h2", "h3", "strong", "b", "em", "i", "u", "s", "strike", "ul", "ol", "li", "blockquote", "pre", "code", "a", "span"]; +const ALLOWED_ATTR = ["href", "target", "rel"]; + +export function sanitizeRichText(html: string): string { + const clean = DOMPurify.sanitize(html, { ALLOWED_TAGS, ALLOWED_ATTR, ALLOW_DATA_ATTR: false }); + // Links open outside the app without handing it the opener. + const template = document.createElement("template"); + template.innerHTML = clean; + for (const link of template.content.querySelectorAll("a")) { + link.setAttribute("target", "_blank"); + link.setAttribute("rel", "noopener noreferrer"); + } + return template.innerHTML; +} + +/** Plain text of a stored body, whitespace collapsed — for card excerpts, never rendered as HTML. */ +export function richTextExcerpt(html: string, maxLength = 280): string { + const doc = new DOMParser().parseFromString(html.replace(/<\/(p|div|h2|h3|li|blockquote|pre)>|${paragraph.join("
")}