Skip to content

Commit 7de58ca

Browse files
committed
fix: address prototype pollution issue
1 parent 45d2568 commit 7de58ca

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Y18N (opts) {
1111
this.fallbackToLanguage = typeof opts.fallbackToLanguage === 'boolean' ? opts.fallbackToLanguage : true
1212

1313
// internal stuff.
14-
this.cache = {}
14+
this.cache = Object.create(null)
1515
this.writeQueue = []
1616
}
1717

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "y18n",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "the bare-bones internationalization library used by yargs",
55
"main": "index.js",
66
"scripts": {

test/y18n-test.js

+18
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,24 @@ describe('y18n', function () {
352352
})
353353
})
354354

355+
// See: https://github.com/yargs/y18n/issues/96,
356+
// https://github.com/yargs/y18n/pull/107
357+
describe('prototype pollution', () => {
358+
it('does not pollute prototype, with __proto__ locale', () => {
359+
const y = y18n()
360+
y.setLocale('__proto__')
361+
y.updateLocale({ polluted: '👽' })
362+
y.__('polluted').should.equal('👽')
363+
;(typeof polluted).should.equal('undefined')
364+
})
365+
366+
it('does not pollute prototype, when __ is used with __proto__ locale', () => {
367+
const __ = y18n({ locale: '__proto__' }).__
368+
__('hello')
369+
;(typeof {}.hello).should.equal('undefined')
370+
})
371+
})
372+
355373
after(function () {
356374
rimraf.sync('./test/locales/fr.json')
357375
})

0 commit comments

Comments
 (0)