|
| 1 | +/** @type {import('svglint').Config} */ |
| 2 | +const config = { |
| 3 | + rules: { |
| 4 | + // Ensure all SVGs are valid XML. |
| 5 | + valid: true, |
| 6 | + |
| 7 | + // Block elements that can execute code or embed arbitrary content. |
| 8 | + // <script> can run arbitrary JavaScript; <foreignObject> and <iframe> |
| 9 | + // can embed arbitrary HTML. Unlike event-handler attributes (which |
| 10 | + // are a legitimate tool for matplotlib's interactive SVG examples), |
| 11 | + // there is no use case in this repository for these elements outside |
| 12 | + // of a <script> already paired with its own exemption. |
| 13 | + elm: { |
| 14 | + "script": false, |
| 15 | + "foreignObject": false, |
| 16 | + "iframe": false, |
| 17 | + }, |
| 18 | + |
| 19 | + custom: [ |
| 20 | + // Block external URL references in href / xlink:href. |
| 21 | + // Internal fragment references (#id), data: URIs, and relative |
| 22 | + // paths are all fine. http/https/ftp and protocol-relative URLs |
| 23 | + // are blocked because they cause the SVG renderer to make an |
| 24 | + // outbound network request, leaking the viewer's IP and UA to an |
| 25 | + // attacker-controlled server. |
| 26 | + (reporter, $, _ast) => { |
| 27 | + reporter.name = "no-external-references"; |
| 28 | + const externalPattern = /^(https?:|ftp:|\/\/)/i; |
| 29 | + $("[href], [xlink\\:href]").each((_i, el) => { |
| 30 | + if (!el.attribs) { return; } |
| 31 | + const href = |
| 32 | + el.attribs["href"] ?? el.attribs["xlink:href"]; |
| 33 | + if (href && externalPattern.test(href)) { |
| 34 | + reporter.error( |
| 35 | + `Found external reference '${href}' on <${el.name}>. ` + |
| 36 | + "External URL references in SVGs cause the renderer " + |
| 37 | + "to make an outbound request, leaking viewer IP/UA." |
| 38 | + ); |
| 39 | + } |
| 40 | + }); |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
| 44 | + |
| 45 | + // These four files are intentional interactive SVG examples that |
| 46 | + // demonstrate matplotlib's SVG interactivity features. They contain |
| 47 | + // embedded ECMAScript by design and are exempted from the <script> rule. |
| 48 | + ignore: [ |
| 49 | + "doc/_static/svg_histogram.svg", |
| 50 | + "doc/_static/svg_tooltip.svg", |
| 51 | + "galleries/examples/user_interfaces/images/svg_histogram.svg", |
| 52 | + "galleries/examples/user_interfaces/images/svg_tooltip.svg", |
| 53 | + ], |
| 54 | +}; |
| 55 | + |
| 56 | +export default config; |
0 commit comments