Skip to content

fix(extract): do not resolve keys to Object.prototype members - #334

Merged
adrai merged 1 commit into
i18next:mainfrom
zzoraemon043:fix/own-property-keys
Nov 21, 2026
Merged

adrai merged 1 commit into
i18next:mainfrom
zzoraemon043:fix/own-property-keys

Conversation

@zzoraemon043

Copy link
Copy Markdown
Contributor

Keys that share a name with an Object.prototype member are lost or misplaced by extract.

t('constructor', 'Builder')
t('toString', 'Text')
t('valueOf', 'Value')
t('page.constructor.title', 'Builder title')

On main, locales/en/translation.json becomes:

{
  "page": {},
  "page.constructor.title": "Builder title"
}

getNestedValue({}, 'constructor', '.') returns Object, so the key is treated as already present, its "value" is the function, and JSON.stringify drops it. In setNestedValue, page.constructor resolves to the inherited function, hits the primitive-conflict branch and the key is written flat at the root. A key such as __proto__.x goes further and assigns x on Object.prototype of the running process.

Fix: getNestedValue, setNestedValue and deleteNestedValue only follow own properties, and __proto__ is defined as a plain own key. With the change the output is:

{
  "constructor": "Builder",
  "page": { "constructor": { "title": "Builder title" } },
  "toString": "Text",
  "valueOf": "Value"
}

Tests: 3 unit tests in nested-objects.test.ts and an extractor test in extractor.builtin-key-names.test.ts; all 4 fail on main. vitest run 1646 passed, eslint . and tsc --noEmit clean.

getNestedValue and setNestedValue followed inherited properties, so a key
named constructor, toString or valueOf was read as already present (a
function) and then dropped by JSON.stringify, and page.constructor.title
was written as a flat key next to an empty page object. Look up own
properties only, and define __proto__ as a plain key instead of letting a
key such as __proto__.x write to Object.prototype.
@adrai
adrai merged commit d8dd469 into i18next:main Nov 21, 2026
11 checks passed
@adrai

adrai commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks again, another good catch. I could reproduce it on main. I merged it and added the same own-property check in two more places in translation-manager.ts: the nesting conflict check and the key order for sort: false. There, page.constructor.title or a new constructor key was still skipped. Included in v1.93.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants