Files
openidea/plugins/official/graphviz/web-src/src/render.ts
T
rohitkumardandaandrohitdanda 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 <10144914+rohitdanda@users.noreply.github.com>
2026-11-14 01:42:03 +08:00

79 lines
3.0 KiB
TypeScript

/**
* The preview's logic apart from the page: rendering options, Graphviz's
* messages as problems, export names. Kept free of the DOM so it is tested
* against the real Graphviz in Node.
*/
import type { RenderOptions, RenderResult } from "@viz-js/viz";
/** Layout engines offered, the default first. */
export const ENGINES = ["dot", "neato", "fdp", "sfdp", "circo", "twopi", "osage", "patchwork"] as const;
export type Engine = (typeof ENGINES)[number];
export interface Problem {
/** 1-based, when Graphviz names one. */
line: number | null;
message: string;
level: "error" | "warning";
}
/**
* Defaults a graph can still override: transparent background and the IDE's
* text colour, so an unstyled graph reads in a dark theme too. Exports use
* none of them (`null`): they look as Graphviz draws them.
*/
export function renderOptions(engine: Engine, foreground: string | null): RenderOptions {
const options: RenderOptions = { format: "svg", engine };
const color = foreground && graphvizColor(foreground);
if (color) {
options.graphAttributes = { bgcolor: "transparent", color, fontcolor: color };
options.nodeAttributes = { color, fontcolor: color };
options.edgeAttributes = { color, fontcolor: color };
}
return options;
}
/**
* A CSS colour as Graphviz reads it (`#rrggbb[aa]`): the theme's variables
* are `rgb()` / `rgba()`, which it doesn't know. `null` when not one.
*/
export function graphvizColor(css: string): string | null {
const hex = (n: number) =>
Math.round(Math.min(255, Math.max(0, n)))
.toString(16)
.padStart(2, "0");
const value = css.trim();
if (/^#([0-9a-f]{6}|[0-9a-f]{8})$/i.test(value)) return value.toLowerCase();
const m = /^rgba?\(\s*([\d.]+)[\s,]+([\d.]+)[\s,]+([\d.]+)(?:\s*[,/]\s*([\d.]+)(%?))?\s*\)$/i.exec(value);
if (!m) return null;
const alpha = m[4] === undefined ? 1 : Number(m[4]) / (m[5] ? 100 : 1);
return `#${hex(+m[1]!)}${hex(+m[2]!)}${hex(+m[3]!)}${alpha < 1 ? hex(alpha * 255) : ""}`;
}
/** What Graphviz reported, as problems (on the line it names, if any). */
export function problemsOf(result: RenderResult): Problem[] {
return result.errors.map((e) => {
const line = /\bline (\d+)\b/.exec(e.message);
return {
line: line ? Number(line[1]) : null,
message: e.message.replace(/^(Error|Warning):\s*/i, "").trim(),
level: e.level === "warning" ? "warning" : "error",
};
});
}
/** The name an export starts with: the file's, without `.dot` / `.gv`. */
export function exportName(file: string, extension: string): string {
const base = file.replace(/\.(dot|gv)$/i, "") || "graph";
return `${base}.${extension}`;
}
/**
* Graphviz's SVG without its links, for the preview: Graphviz writes a graph's
* `URL` / `href` attributes as given (`javascript:` too), and the preview's
* page may run inline script. Exports keep them. Graphviz quotes attribute
* values with `"` and escapes the ones inside, so a value never holds one.
*/
export function withoutLinks(svg: string): string {
return svg.replace(/\s(?:xlink:)?(?:href|target)="[^"]*"/g, "");
}