Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Security Fix for Prototype Pollution - huntr.dev #107

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class Y18N {
}

updateLocale (obj: Locale) {
if (this.locale === '__proto__') {
return
}

if (!this.cache[this.locale]) this._readLocaleFile()

for (const key in obj) {
Expand Down
10 changes: 10 additions & 0 deletions test/y18n-test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ describe('y18n', function () {

i18n.__('meow').should.equal('le meow')
})

it('prevents prototype pollution', function () {
var i18n = y18n()

i18n.setLocale('__proto__')

i18n.updateLocale({ polluted: 'Yes! Its Polluted' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after this call, shouldn't expect(i18n.getLocale()).to.have.property('polluted') pass as well?

Copy link

@JamieSlome JamieSlome Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb - before the fix, this may have been the case. However, after running the test suite with your suggested test, the following is returned:

Screenshot 2020-10-22 at 22 05 28

If the suggested expect() statement passed, wouldn't this suggest that the prototype has been polluted?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it would suggest that the locale object was properly updated.

I think the solution here isn't to disrupt usage of a string __proto__, but instead, to "salt" each locale name with a character that couldn't conflict with anything built-in. I usually use $.

ie, every get and set of a locale name should quietly stick a "$" in front. That way it'll be impossible to collide with the name of any builtin.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb - okay, we will take another look at this, and see if we can salt each locale name as suggested with a non-disruptive character.

Cheers! 🍰


expect({}).to.not.have.property('polluted')
})
})

describe('getLocale', function () {
Expand Down