Файли
openidea/plugins/official/graphviz/web-src/src/render.test.ts
T
rohitkumardandaтаrohitdanda d2c565b19d fix(plugins): harden the official plugins against crafted input (#3219)
Several official plugins trusted crafted input: paths outside the project were accepted where only project files were meant, some loops and replacements had no upper bound, and CSV Add Row wrapped its row index on wasm32.

- ai-completion: only treat a real OpenAI host as OpenAI
- checkstyle, license-header, mybatis: keep resolved paths under the project root
- license-header: keep the generated header on one line where one is expected
- csv-editor: Add Row appends on wasm32 instead of overwriting the header
- regex-tester: cap the replaced output
- solidity: linearisation stops on cyclic inheritance
- mybatis: bound the mappers and statements it indexes
- string-tools, graphviz: guard the remaining crafted-input cases
- Rebuilt the changed components from source.

Co-authored-by: rohitdanda <[email protected]>
2026-11-14 01:42:03 +08:00

64 рядки
2.8 KiB
TypeScript

import { instance } from "@viz-js/viz";
import { describe, expect, it } from "vitest";
import { ENGINES, exportName, graphvizColor, problemsOf, renderOptions, withoutLinks } from "./render";
describe("the preview's rendering", () => {
it("renders with the theme's colour as a default the graph can override", async () => {
const viz = await instance();
const themed = viz.render('digraph { a -> b; c [color="red"] }', renderOptions("dot", "rgb(200, 200, 200)"));
expect(themed.status).toBe("success");
const svg = themed.status === "success" ? themed.output : "";
expect(svg).toContain('stroke="#c8c8c8"');
expect(svg).toContain('stroke="red"');
const plain = viz.render("digraph { a -> b }", renderOptions("dot", null));
expect(plain.status === "success" && plain.output).not.toContain("#c8c8c8");
});
it("names the line of a syntax error", async () => {
const viz = await instance();
const result = viz.render("digraph {\n a -> b\n c -> }\n", renderOptions("dot", null));
expect(result.status).toBe("failure");
const [p] = problemsOf(result);
expect(p).toMatchObject({ line: 3, level: "error" });
expect(p!.message).toMatch(/syntax error/);
});
it("offers only engines this Graphviz has", async () => {
const viz = await instance();
for (const e of ENGINES) expect(viz.engines).toContain(e);
});
it("turns the theme's colours into Graphviz's", () => {
expect(graphvizColor("rgb(200, 200, 200)")).toBe("#c8c8c8");
expect(graphvizColor("rgba(0, 128, 255, 0.5)")).toBe("#0080ff80");
expect(graphvizColor("rgb(1 2 3 / 50%)")).toBe("#01020380");
expect(graphvizColor("#ABCDEF")).toBe("#abcdef");
expect(graphvizColor("var(--x)")).toBeNull();
expect(renderOptions("dot", "nonsense").nodeAttributes).toBeUndefined();
});
it("names exports after the file", () => {
expect(exportName("flow.dot", "svg")).toBe("flow.svg");
expect(exportName("Flow.GV", "png")).toBe("Flow.png");
expect(exportName("", "svg")).toBe("graph.svg");
});
it("previews a graph's links without them", async () => {
const viz = await instance();
const js = "javascript:alert(1)";
const graph = `digraph {
URL="${js}"; target="_top"
a [URL="${js}" href="https://example.com" label="a href=\\"x\\""]
a -> b [URL="${js}" edgeURL="${js}" headURL="${js}" tailURL="${js}" labelURL="${js}" label="e" headlabel="h" taillabel="t"]
subgraph cluster_x { URL="${js}"; c }
}`;
const result = viz.render(graph, renderOptions("dot", null));
const svg = result.status === "success" ? result.output : "";
expect(svg).toContain(js);
const preview = withoutLinks(svg);
expect(preview).not.toMatch(/\s(xlink:)?(href|target)="|javascript:/);
expect(preview).toContain("a href=&quot;x&quot;");
expect(preview.match(/<g id="node\d+"/g)).toHaveLength(3);
});
});