Skip to content

Commit a9ac604

Browse files
authored
fix: address prototype pollution issue (#108)
1 parent 61a8b9a commit a9ac604

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Y18N {
4747
this.fallbackToLanguage = typeof opts.fallbackToLanguage === 'boolean' ? opts.fallbackToLanguage : true
4848

4949
// internal stuff.
50-
this.cache = {}
50+
this.cache = Object.create(null)
5151
this.writeQueue = []
5252
}
5353

test/y18n-test.cjs

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

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

0 commit comments

Comments
 (0)