[117] Merge: strikte CSP (Tauri, Electron, Vite-Build), Rich-Text-Stile per Positivliste
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
This commit is contained in:
9 files changed
+215
-9
No files matched your search
@@ -0,0 +1,27 @@
|
||||
// [117] Die CSP-Quelle (scripts/csp.cjs) deckt die netFetch-Hosts ab und steht wörtlich in tauri.conf.json.
|
||||
import { readFileSync } from "node:fs";
|
||||
import { createRequire } from "node:module";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ALLOWED_HOSTS } from "./net";
|
||||
|
||||
const { buildCsp, NET_HOSTS } = createRequire(import.meta.url)("../../scripts/csp.cjs") as {
|
||||
buildCsp: () => string;
|
||||
NET_HOSTS: string[];
|
||||
};
|
||||
|
||||
describe("CSP", () => {
|
||||
it("connect-src enthält genau die Remote-Hosts von ALLOWED_HOSTS", () => {
|
||||
const remote = ALLOWED_HOSTS.filter((h) => h !== "localhost" && h !== "127.0.0.1");
|
||||
expect([...NET_HOSTS].sort()).toEqual([...remote].sort());
|
||||
const connect = buildCsp().split("; ").find((d) => d.startsWith("connect-src "))!;
|
||||
for (const h of remote) expect(connect).toContain(`https://${h}`);
|
||||
});
|
||||
it("tauri.conf.json trägt dieselbe Richtlinie", () => {
|
||||
const conf = JSON.parse(readFileSync("src-tauri/tauri.conf.json", "utf8"));
|
||||
expect(conf.app.security.csp).toBe(buildCsp());
|
||||
});
|
||||
it("kein unsafe-eval, keine Inline-Skripte", () => {
|
||||
const script = buildCsp().split("; ").find((d) => d.startsWith("script-src "))!;
|
||||
expect(script).toBe("script-src 'self' 'wasm-unsafe-eval'");
|
||||
});
|
||||
});
|
||||
@@ -119,3 +119,33 @@ describe("docToHtml — sizeUnit reicht bis in jeden Run-Span durch", () => {
|
||||
expect(html).toContain("font-size:1.5em");
|
||||
});
|
||||
});
|
||||
|
||||
describe("docToHtml: Bereinigung nicht vertrauenswürdiger Dateiinhalte ([117])", () => {
|
||||
const evil = (over: Record<string, unknown>, text = "x"): RichTextDoc =>
|
||||
({
|
||||
version: 1,
|
||||
paragraphs: [{ runs: [{ text, marks: { ...over } }] }],
|
||||
}) as unknown as RichTextDoc;
|
||||
|
||||
it("escapt Tags im Text", () => {
|
||||
const html = docToHtml(evil({}, '<img src=x onerror=alert(1)><script>1</script>'));
|
||||
expect(html).not.toContain("<img");
|
||||
expect(html).not.toContain("<script");
|
||||
expect(html).toContain("<img");
|
||||
});
|
||||
it("verwirft Schrift und Farbe mit CSS-/Attribut-Einschleusung", () => {
|
||||
const html = docToHtml(evil({ font: 'a";onmouseover="alert(1)', color: "red;background:url(https://evil/x)" }));
|
||||
expect(html).not.toContain("onmouseover");
|
||||
expect(html).not.toContain("url(");
|
||||
expect(html).not.toContain("font-family");
|
||||
});
|
||||
it("behält harmlose Werte", () => {
|
||||
const html = docToHtml(evil({ font: "Inter", color: "#aabbcc" }));
|
||||
expect(html).toContain("font-family:Inter");
|
||||
expect(html).toContain("color:#aabbcc");
|
||||
});
|
||||
it("Absatz-Ausrichtung nur aus der Positivliste", () => {
|
||||
const doc = { version: 1, paragraphs: [{ align: 'center;position:fixed', runs: [] }] } as unknown as RichTextDoc;
|
||||
expect(docToHtml(doc)).not.toContain("position");
|
||||
});
|
||||
});
|
||||
+31
-4
@@ -27,6 +27,31 @@ function escapeAttr(s: string): string {
|
||||
|
||||
// ── Gemeinsame Mark→Stil-Zuordnung ──────────────────────────────────────────
|
||||
|
||||
// ── CSS-Werte aus Dateien ([117]) ───────────────────────────────────────────
|
||||
// Rich-Text kommt auch aus importierten Projektdateien und landet per `innerHTML` im Editor. Der Text ist
|
||||
// escaped; die Stil-Werte (Farbe, Schriftname, Ausrichtung, Zeilenhöhe) werden hier auf eine Positivliste
|
||||
// beschränkt, damit keine CSS-Einschleusung (`;`, `url(…)`, `position:fixed` …) möglich ist.
|
||||
|
||||
/** Farbe: `#rgb[a]`/`#rrggbb[aa]`, `rgb()/rgba()/hsl()` mit Zahlen, `var(--name)` oder ein Farbwort. */
|
||||
export function safeCssColor(v: unknown): string | undefined {
|
||||
if (typeof v !== "string") return undefined;
|
||||
const c = v.trim();
|
||||
if (/^#[0-9a-f]{3,8}$/i.test(c)) return c;
|
||||
if (/^(rgb|rgba|hsl|hsla)\(\s*[0-9.%\s,/-]+\)$/i.test(c)) return c;
|
||||
if (/^var\(--[a-z0-9-]+\)$/i.test(c)) return c;
|
||||
if (/^[a-z]{3,20}$/i.test(c)) return c;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** Schriftname: Buchstaben, Ziffern, Leerzeichen, `-`, `,`, `_`, `.`, Anführungszeichen (kein `;(){}:/\\`). */
|
||||
export function safeFontFamily(v: unknown): string | undefined {
|
||||
if (typeof v !== "string") return undefined;
|
||||
const f = v.trim();
|
||||
return f.length > 0 && f.length <= 120 && /^[\p{L}\p{N} _.,'"-]+$/u.test(f) ? f : undefined;
|
||||
}
|
||||
|
||||
const SAFE_ALIGN = new Set(["left", "center", "right", "justify"]);
|
||||
|
||||
/** Kombiniert underline + strike zu einem CSS/SVG-text-decoration-Wert. */
|
||||
function textDecoration(marks: Marks): string | undefined {
|
||||
const parts: string[] = [];
|
||||
@@ -67,11 +92,13 @@ export function runCssProps(
|
||||
if (marks.italic) css["font-style"] = "italic";
|
||||
const deco = textDecoration(marks);
|
||||
if (deco) css["text-decoration"] = deco;
|
||||
if (marks.font) css["font-family"] = marks.font;
|
||||
const font = safeFontFamily(marks.font);
|
||||
if (font) css["font-family"] = font;
|
||||
const sizePt = (marks.sizePt ?? basePt) * (marks.super || marks.sub ? SCRIPT_SCALE : 1);
|
||||
css["font-size"] =
|
||||
sizeUnit === "em" ? `${round(sizePt / basePt)}em` : `${round(sizePt)}pt`;
|
||||
if (marks.color) css["color"] = marks.color;
|
||||
const color = safeCssColor(marks.color);
|
||||
if (color) css["color"] = color;
|
||||
if (marks.super) css["vertical-align"] = "super";
|
||||
if (marks.sub) css["vertical-align"] = "sub";
|
||||
return css;
|
||||
@@ -113,8 +140,8 @@ export function docToHtml(doc: RichTextDoc, opts: HtmlOptions = {}): string {
|
||||
|
||||
function paragraphToHtml(p: Paragraph, basePt: number, sizeUnit: "pt" | "em", color?: string): string {
|
||||
const styles: string[] = [];
|
||||
if (p.align && p.align !== "left") styles.push(`text-align:${p.align}`);
|
||||
if (p.lineHeight != null) styles.push(`line-height:${p.lineHeight}`);
|
||||
if (p.align && p.align !== "left" && SAFE_ALIGN.has(p.align)) styles.push(`text-align:${p.align}`);
|
||||
if (typeof p.lineHeight === "number" && Number.isFinite(p.lineHeight)) styles.push(`line-height:${p.lineHeight}`);
|
||||
const style = styles.length ? ` style="${styles.join(";")}"` : "";
|
||||
if (p.runs.length === 0) return `<div${style}><br></div>`;
|
||||
const inner = p.runs.map((r) => runToHtmlSpan(r, basePt, sizeUnit, color)).join("");
|
||||
|
||||
Reference in new issue
Block a user