Skip to content
Open
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
8 changes: 6 additions & 2 deletions packages/localize/src/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ export function loadTranslations(translations: Record<MessageId, TargetMessage>)
$localize.translate = translate;
}
if (!$localize.TRANSLATIONS) {
$localize.TRANSLATIONS = {};
// Keyed by message ID, which is taken verbatim from the `translations` map (e.g. parsed from a
// translation file), so it can be `__proto__`. Indexing a plain object with that key assigns
// through the inherited `__proto__` setter, reparenting the map instead of storing the entry
// (and throwing under `--disable-proto=throw`). A null-prototype map makes it an ordinary key.
$localize.TRANSLATIONS = Object.create(null);
}
Object.keys(translations).forEach((key) => {
$localize.TRANSLATIONS[key] = parseTranslation(translations[key]);
Expand All @@ -86,7 +90,7 @@ export function loadTranslations(translations: Record<MessageId, TargetMessage>)
*/
export function clearTranslations() {
$localize.translate = undefined;
$localize.TRANSLATIONS = {};
$localize.TRANSLATIONS = Object.create(null);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions packages/localize/test/translate_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ describe('$localize tag with translations', () => {
);
});
});

describe('with a `__proto__` message id', () => {
afterEach(() => {
clearTranslations();
});

it('should treat `__proto__` as an ordinary translation key', () => {
loadTranslations({
['__proto__' as MessageId]: 'pwned' as TargetMessage,
[computeMsgId('abc', '')]: 'abc' as TargetMessage,
});

const store = ($localize as unknown as {TRANSLATIONS: Record<string, unknown>}).TRANSLATIONS;
// The malicious id must be stored as an own key rather than assigned through the inherited
// `__proto__` setter, which would reparent the map (and drop the entry).
expect(Object.getPrototypeOf(store)).toBe(null);
expect(Object.prototype.hasOwnProperty.call(store, '__proto__')).toBe(true);
// A translation loaded alongside it still resolves.
expect($localize`abc`).toEqual('abc');
});
});
});

function computeIds(
Expand Down
Loading