Skip to content

Commit

Permalink
fix(security): Fixed GHSA-g974-hxvm-x689
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Sep 19, 2024
1 parent a92527e commit 63e627c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ Gettext.prototype.warn = function (message) {
* @param {Object} translations An object of gettext-parser JSON shape
*/
Gettext.prototype.addTranslations = function (locale, domain, translations) {
if (typeof locale !== 'string') {
this.warn('You called addTranslations() with an argument of type ' + typeof locale + '. The locale must be a string.');
return;
}

if (typeof locale !== 'string') {
this.warn('You called setLocale() with an argument of type ' + typeof locale + '. The locale must be a string.');
return;
}

if (locale in {}) {
this.warn('Can not use reserved key as locale');
return;
}

if (domain in {}) {
this.warn('Can not use reserved key as domain');
return;
}

if (!this.catalogs[locale]) {
this.catalogs[locale] = {};
}
Expand All @@ -126,6 +146,11 @@ Gettext.prototype.setLocale = function (locale) {
return;
}

if (locale in {}) {
this.warn('Can not use reserved key as locale');
return;
}

if (locale.trim() === '') {
this.warn('You called setLocale() with an empty value, which makes little sense.');
}
Expand All @@ -151,6 +176,11 @@ Gettext.prototype.setTextDomain = function (domain) {
return;
}

if (domain in {}) {
this.warn('Can not use reserved key as domain');
return;
}

if (domain.trim() === '') {
this.warn('You called setTextDomain() with an empty `domain` value.');
}
Expand Down
5 changes: 5 additions & 0 deletions test/gettext-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ describe('Gettext', () => {
expect(errorListener.callCount).to.equal(0);
});

it('should emit an error event when adding a reserved key as locale', () => {
gt.addTranslations('__proto__', 'polluted', 'pwned');
expect(errorListener.callCount).to.equal(1);
});

it('should emit an error event when a locale that has no translations is set', () => {
gt.setLocale('et-EE');
expect(errorListener.callCount).to.equal(1);
Expand Down

0 comments on commit 63e627c

Please sign in to comment.