Skip to content
Prototype pollution Disclosed report Intermediate

[chart.js] Prototype pollution

Chart.js recursively merges attacker-controlled objects into its configuration, allowing injection of properties onto Object.prototype. This can lead to cross-site scripting in some applications.

asgerf Follow

Originally disclosed on HackerOne

  • Triaged in 27 hours
  • Fixed in 10 months
  • Disclosed after 11 months

I would like to report a prototype pollution vulnerability in chart.js It allows an attacker to inject properties on Object.prototype which can for some applications lead to XSS.

Module

module name: chart.js version: 2.9.3 npm page: https://www.npmjs.com/package/chart.js

Module Description

Simple yet flexible JavaScript charting for designers & developers

Module Stats

854,171 weekly downloads

Vulnerability

Vulnerability Description

If the dataset or options passed to Chart.js contains an attacker-controlled object, Chart.js can be tricked into adding or modifying properties of the Object prototype. These properties will be present on all objects.

The payload is an object with the __proto__ property.

Steps To Reproduce:

Install chart.js 2.9.3 into node_modules and then view the following HTML page and check the log:

HTML
        <canvas id="canvas"></canvas>
        <script src="node_modules/chart.js/dist/Chart.bundle.js"></script>
        <script>
            var ctx = document.getElementById('canvas').getContext('2d');
            var chart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: ['January', 'February', 'March', 'April', 'May'],
                    datasets: [{
                        label: 'My First dataset',
                        backgroundColor: 'rgb(255, 99, 132)',
                        borderColor: 'rgb(255, 99, 132)',
                        data: [0, 10, 5, 2, 20]
                    },
                    JSON.parse(`{"__proto__": {"abc": "Injected value through dataset"}}`)
                    ]
                },
                options: JSON.parse(`{"__proto__": {"def": "Injected value through options"}}`)
            });
            console.log({}.abc); // Print "Injected value through dataset"
            console.log({}.def); // Print "Injected value through options"
        </script>

Patch

Avoid recursively merging properties that aren't already own properties of the target object:

diff --git a/src/helpers/helpers.core.js b/src/helpers/helpers.core.js
index 100d4edf..b5127025 100644
--- a/src/helpers/helpers.core.js
+++ b/src/helpers/helpers.core.js
@@ -226,7 +226,7 @@ export function _merger(key, target, source, options) {
 	var tval = target[key];
 	var sval = source[key];

-	if (isObject(tval) && isObject(sval)) {
+	if (Object.prototype.hasOwnProperty.call(target, key) && isObject(tval) && isObject(sval)) {
 		// eslint-disable-next-line no-use-before-define
 		merge(tval, sval, options);
 	} else {

Supporting Material/References:

Tested Chart.js 2.9.3 in Chrome 79

Wrap up

  • I contacted the maintainer to let them know: N
  • I opened an issue in the related repository: N

Impact

Inject properties on Object.prototype which can for some applications lead to XSS.

The original report as published on HackerOne, by asgerf. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The merge function recursively merged properties without checking if they were own properties of the target object, allowing the __proto__ key to modify Object.prototype.

Impact

An attacker could inject properties on Object.prototype, which for some applications could lead to XSS.

What the attacker needed

Attacker-controlled object passed to Chart.js

The fix

The suggested patch only recursively merges properties that are already own properties of the target object.

How it was found

  1. Install chart.js 2.9.3 into node_modules.
  2. Create an HTML page with a canvas and include the Chart.bundle.js script.
  3. Create a new Chart with data containing a dataset that includes a JSON-parsed object with a __proto__ property.
  4. Also include an options object with a JSON-parsed __proto__ property.
  5. Check the browser console to see the injected properties on a new object.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

Prototype pollution is a JavaScript bug where an attacker can add new properties to a built-in object that all other objects inherit from. Here, Chart.js merged user-supplied data without checking if the property already existed, so an attacker could add a property like 'abc' to every object. This can sometimes be abused to run malicious scripts.

What is Prototype pollution?

Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.

More by asgerf

View all

7 more disclosed reports, best first.

More from Node.js third-party modules

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Prototype pollution

The lesson behind this report.

Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.