Files
openidea/src/features/notebook/security.test.ts
Dimas Victoranddvictor357 0d368ba947 feat(notebook): add native Jupyter notebook editing and execution (#268)
Opens .ipynb files in a native notebook editor, in a project or LightEdit.

- crates/ide-notebook: lossless nbformat documents (unchanged notebooks save byte-for-byte, unknown fields kept, external changes detected) and owned local kernels over ZMQ via kernelspec — the selected Python environment only needs ipykernel, no Jupyter server.
- ide-host owns notebook documents per project incarnation and admits one execution batch at a time; outputs stream over IPC channels and kernel status is an IdeEvent.
- src/features/notebook: virtualized Python/Markdown cells, insert/delete/move/change type, Run Cell / Run All (Shift+Enter), interrupt, restart, interpreter selection from ide-sdk discovery, shared split views. Raw/unknown cells stay read-only.
- Custom editor kinds take part in dirty state, save/save all, undo/redo and Save/Don't Save/Cancel; closing editors from other features awaits that decision.
- Outputs: text, tracebacks, PNG/JPEG and sanitized SVG inline; HTML sanitized in a sandboxed iframe without allow-same-origin; JavaScript output is shown as inert source.
- Explicit launches (opening a notebook from the OS) win over session restoration.

Closes #131

Co-authored-by: dvictor357 <187467850+dvictor357@users.noreply.github.com>
2026-10-30 07:26:43 +08:00

70 lines
4.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { markdownRoot, resolveMarkdownAttachments, sanitizedSvgUrl, staticOutputDocument } from "./security";
const parse = (html: string) => new DOMParser().parseFromString(html, "text/html");
describe("static notebook output sanitization", () => {
it("retains table content while removing executable, navigation and external-resource surfaces", () => {
const document = parse(
staticOutputDocument(`
<meta http-equiv="refresh" content="0;url=https://attacker.invalid">
<base href="https://attacker.invalid"><script>window.__TAURI_INTERNALS__.invoke('x')</script>
<form action="https://attacker.invalid"><input name="secret"><button>submit</button></form>
<iframe src="https://attacker.invalid"></iframe><object data="https://attacker.invalid"></object>
<a href="https://attacker.invalid" target="_top" ping="https://attacker.invalid">link</a>
<img src="https://attacker.invalid/a.png" srcset="https://attacker.invalid/b.png 2x" onerror="alert(1)">
<div style="background-image:image-set('https://attacker.invalid/x')">safe text</div>
<table><thead><tr><th>name</th><th>value</th></tr></thead><tbody><tr><td>alpha</td><td>42</td></tr></tbody></table>
`),
);
expect(document.querySelector("tbody tr")?.textContent).toBe("alpha42");
expect(document.querySelector("script,base,form,input,button,iframe,object")).toBeNull();
expect(document.querySelector("meta[http-equiv=refresh]")).toBeNull();
expect(document.querySelector("[href],[target],[srcset],[onerror],[ping]")).toBeNull();
expect(document.querySelector("img")?.hasAttribute("src")).toBe(false);
expect(document.querySelector("body div")?.hasAttribute("style")).toBe(false);
});
it("retains safe SVG chart paint, local definitions and raster images but removes active/external SVG", () => {
const url = sanitizedSvgUrl(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<script>alert(1)</script><foreignObject><div>host markup</div></foreignObject>
<defs><linearGradient id="paint"><stop offset="0" stop-color="red"/></linearGradient></defs>
<path id="chart" d="M0 0L10 10" style="fill:none;stroke:#123456;stroke-width:2" onclick="alert(1)"/>
<rect fill="url(#paint)" width="10" height="10"/>
<use id="local-marker" href="#chart"/>
<use href="https://attacker.invalid/a.svg#x"/><image href="data:image/png;base64,aGVsbG8="/>
<path id="external" fill="url(https://attacker.invalid/paint)"/>
<animate attributeName="href" to="https://attacker.invalid"/>
</svg>`);
const xml = decodeURIComponent(url.slice(url.indexOf(",") + 1));
const document = new DOMParser().parseFromString(xml, "image/svg+xml");
expect(document.querySelector("script,foreignObject,animate,style")).toBeNull();
expect(document.querySelector('[href^="https:"],[href^="http:"]')).toBeNull();
expect(document.querySelector("#chart")?.getAttribute("stroke")).toBe("#123456");
expect(document.querySelector("#chart")?.getAttribute("fill")).toBe("none");
expect(document.querySelector("rect")?.getAttribute("fill")).toBe("url(#paint)");
expect(document.querySelector("#local-marker")?.getAttribute("href")).toBe("#chart");
expect(document.querySelector("#external")?.hasAttribute("fill")).toBe(false);
expect(document.querySelector("image")?.getAttribute("href")).toBe("data:image/png;base64,aGVsbG8=");
expect(document.querySelector("[onclick],[style]")).toBeNull();
});
it("resolves only embedded image attachments and makes Markdown file links editor-controlled", () => {
const root = resolveMarkdownAttachments(
markdownRoot(
`![chart](attachment:chart.png)\n\n![remote](https://attacker.invalid/a.png)\n\n[local](./other.py#L8)\n\n<script>alert(1)</script><div style="position:fixed">text</div>`,
),
{
"chart.png": { "image/png": "aGVsbG8=" },
},
);
const images = root.querySelectorAll("img");
expect(images[0]?.getAttribute("src")).toBe("data:image/png;base64,aGVsbG8=");
expect(images[1]?.hasAttribute("src")).toBe(false);
const link = root.querySelector("a");
expect(link?.hasAttribute("href")).toBe(false);
expect(link?.getAttribute("data-notebook-href")).toBe("./other.py#L8");
expect(root.querySelector("script,[style]")).toBeNull();
});
});