Skip to main content
Version: 4.1

Sanitization

JointJS renders the cell attributes (e.g. markup, attrs, ports and labels) as they are. Graph JSON or cell attributes from an untrusted source (e.g. a file import, another user in a collaborative session, a URL parameter) can therefore run scripts in the page.

Sanitize such data with the @joint/sanitize package before it enters the graph. It removes the unsafe parts (e.g. <script> elements, onclick attributes, javascript: URLs) and keeps the rest.

Installation​


npm install @joint/sanitize

The package works with @joint/core 4.1 and later.

Graph JSON​


Sanitize the JSON before you pass it to graph.fromJSON():

import { dia, shapes } from '@joint/core';
import { sanitizeGraphJSON } from '@joint/sanitize';

const graph = new dia.Graph({}, { cellNamespace: shapes });

graph.fromJSON(sanitizeGraphJSON(untrustedJSON, { cellNamespace: shapes }));

Pass the same cellNamespace as the graph uses. The sanitizer looks up the markup and the defaults of the shapes in it; with a different namespace, it can remove legitimate content of your custom shapes.

sanitizeGraphJSON() returns a sanitized copy and leaves the input unchanged. It returns null if the input is not an object, and it throws if the JSON is nested too deeply to be copied.

Cells​


Sanitize every cell before you pass it to graph.addCell(), graph.addCells(), graph.resetCells() or to a shape constructor:

import { sanitizeCellJSON } from '@joint/sanitize';

graph.addCell(sanitizeCellJSON(untrustedCellJSON, { cellNamespace: shapes }));

Changes to existing cells​


Sanitize attributes before you set them on an existing cell with cell.set() (e.g. changes received from another user in a collaborative session):

import { sanitizeCellAttributes } from '@joint/sanitize';

const cell = graph.getCell(change.id);
cell.set(sanitizeCellAttributes(cell, change.attributes));

Pass the returned object to cell.set() as it is. Do not merge the unsanitized input back in.

A path-based change (e.g. cell.prop(), cell.attr() or the port and label methods) can not be sanitized on its own. Apply it to a copy of the top-level attribute, then sanitize and set that:

import { util } from '@joint/core';

const attrs = util.cloneDeep(cell.get('attrs'));
util.setByPath(attrs, 'body/fill', untrustedFill);
cell.set(sanitizeCellAttributes(cell, { attrs }));

HTML​


Sanitize HTML before you insert it into an HTML element (e.g. the content of a tooltip or a popup):

import { sanitizeHTML } from '@joint/sanitize';

element.innerHTML = sanitizeHTML(untrustedHTML);

Insert the result only as the content of an HTML element, not into an SVG element or an attribute.

Options​


sanitizeGraphJSON() and sanitizeCellJSON() accept these options (sanitizeCellAttributes() accepts onRemove and strict):

  • cellNamespace - the namespace of the cell constructors (the cellNamespace of the graph).
  • onRemove(path, reason) - a callback called for every removed part of the JSON.
  • strict - true (the default) removes string markup and the html attribute. false sanitizes them instead (this requires a DOM).

For example, to report what was removed from an imported file:

const removed = [];
const json = sanitizeGraphJSON(untrustedJSON, {
cellNamespace: shapes,
onRemove: (path, reason) => removed.push(`${path.join('/')}: ${reason}`)
});

What is not checked​


  • Custom attribute definitions (the attributes of a shape). Their values are checked as attributes of the name they are set under, so a definition must use a value only as an attribute value or a property, never to build a markup string (e.g. for V(), innerHTML or util.template()).
  • Custom views, and what your code does with the cell attributes.
  • CSS (e.g. a <style> element can change the look of the whole page) and external http(s) resources.
  • Graph attributes other than cells. They are copied unchanged.

Script tag​


The UMD build of the package (dist/sanitize.js) adds the functions to joint.util. Its sanitizeHTML() replaces the one of JointJS:

<script src="joint.js"></script>
<script src="sanitize.js"></script>
<script>
graph.fromJSON(joint.util.sanitizeGraphJSON(untrustedJSON, { cellNamespace: joint.shapes }));
</script>