Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 405e808

Browse files
yinlianghuiclaude
andauthored
fix(i18n): pickLocalized reads own properties only and takes only string values on every limb (#3907) (#4359)
Four of the six limbs — exact tag, base language, `default`, `en` — used bare bracket access, so a locale naming an `Object.prototype` member resolved to that member and rendered its source text as the label, and a non-string value short-circuited the chain into `[object Object]`. Both guards now apply uniformly; a guarded limb misses rather than aborting, so the chain continues. Zero change for any real BCP-47 tag, which collapses the two departures the backend twin `resolveI18nLabel` (objectstack#6765) recorded against this function to zero. The parity table now pins that convergence. Claude-Session: https://claude.ai/code/session_017Qqyix2QcnpUC9XeYVDzx3 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7e4f0e5 commit 405e808

4 files changed

Lines changed: 334 additions & 10 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@object-ui/i18n': patch
3+
---
4+
5+
`pickLocalized` reads own properties only, and takes only string values, on every limb
6+
7+
The resolver read four of its six limbs — the exact tag, the base language, `default` and `en` — with a bare bracket access. Bare access walks the prototype chain, so a locale that happened to name an `Object.prototype` member resolved to that member and the function stringified it into the label: `pickLocalized({ en: 'Pricing' }, 'constructor')` returned `function Object() { [native code] }`, and the same held for `toString`, `valueOf`, `hasOwnProperty`, `isPrototypeOf`, `propertyIsEnumerable` and `toLocaleString`. Those same four limbs also skipped the `typeof === 'string'` filter the regional and last-resort limbs already applied, so a non-string value short-circuited the chain and rendered as `[object Object]`.
8+
9+
Both guards now apply uniformly. A guarded limb **misses** rather than aborting the resolution, so an unusable entry falls through to the next limb exactly as an absent one does — `pickLocalized({ en: 'Pricing' }, 'constructor')` is now `'Pricing'` (the `en` limb), and only a map with no usable entry at all resolves to `''`. An empty-string value is still a hit, because `''` is a label the author wrote.
10+
11+
No real language tag can observe this: no BCP-47 tag is an `Object.prototype` member, and the inline locale map is declared `z.record(<tag>, z.string())`, so every in-contract input resolves byte-identically to before. What it changes is agreement with the backend twin `resolveI18nLabel` (objectstack#6765), which shipped with exactly these two narrowings recorded as deliberate departures from this function because on a server the locale can arrive in an `Accept-Language` header. That recorded rule divergence is now zero; the only remaining difference is how each side spells a miss (`''` here for a text node, `undefined` there for a producer's fallback chain), which is pinned as an identity in the cross-resolver parity table.

‎packages/i18n/src/__tests__/pickLocalized.test.ts‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,188 @@ describe('pickLocalized', () => {
2828
expect(pickLocalized({ en: 'E' }, undefined)).toBe('E');
2929
});
3030
});
31+
32+
/**
33+
* objectui#3907 — every limb reads OWN properties only, and accepts only
34+
* `string` values.
35+
*
36+
* Both halves used to hold on limbs 3 and 6 alone, so the other four limbs
37+
* (exact tag, base language, `default`, `en`) could resolve against
38+
* `Object.prototype` and could short-circuit the chain with a non-string. They
39+
* are the two departures the backend twin `resolveI18nLabel` (objectstack#6765,
40+
* `packages/spec/src/ui/i18n-label-resolver.ts`) documented as deliberate
41+
* narrowings of this function; landing them here collapses that recorded
42+
* divergence to zero.
43+
*
44+
* ## The shape of the fix is "the limb misses", NOT "the call returns `''`"
45+
*
46+
* A locale naming an `Object.prototype` member stops being a HIT on that limb —
47+
* it does not abort the resolution. The `??` chain therefore continues to the
48+
* next limb exactly as it would for any other unknown tag, so
49+
* `pickLocalized({ en: 'Pricing' }, 'constructor')` is `'Pricing'` (limb 5), not
50+
* `''`. `''` is only correct when NO limb hits, which needs a map with no usable
51+
* entry at all. Both are pinned below, because a test that demanded `''` for the
52+
* first case would be pinning a fallthrough bug rather than this fix.
53+
*/
54+
describe('pickLocalized — own properties only (objectui#3907)', () => {
55+
/** Every `Object.prototype` member the card enumerates. */
56+
const PROTOTYPE_MEMBERS = [
57+
'constructor',
58+
'toString',
59+
'valueOf',
60+
'hasOwnProperty',
61+
'isPrototypeOf',
62+
'propertyIsEnumerable',
63+
'toLocaleString',
64+
] as const;
65+
66+
it.each(PROTOTYPE_MEMBERS)(
67+
'a locale named %s misses every limb instead of resolving up the prototype chain',
68+
(member) => {
69+
// Before the fix this returned the member's source text, e.g.
70+
// 'function Object() { [native code] }', as the rendered label.
71+
expect(pickLocalized({ en: 'Pricing' }, member)).toBe('Pricing');
72+
},
73+
);
74+
75+
it.each(PROTOTYPE_MEMBERS)(
76+
'a locale named %s never leaks a function body into the label',
77+
(member) => {
78+
// Stated once over the whole set rather than per member: an
79+
// implementation that resolved a DIFFERENT prototype member would satisfy
80+
// no assertion above but would also fail none of them.
81+
expect(pickLocalized({ en: 'Pricing' }, member)).not.toContain('native code');
82+
expect(pickLocalized({ ja: '価格' }, member)).not.toContain('native code');
83+
},
84+
);
85+
86+
it('the prototype limb is a miss, so the chain continues to the next limb', () => {
87+
// Limb 5 (`en`) is what answers above. With no `en`, the last-resort limb
88+
// answers; with nothing usable at all, and only then, the answer is `''`.
89+
expect(pickLocalized({ ja: '価格' }, 'constructor')).toBe('価格');
90+
expect(pickLocalized({ default: 'D' }, 'toString')).toBe('D');
91+
expect(pickLocalized({}, 'constructor')).toBe('');
92+
});
93+
94+
it('own keys that happen to be named like prototype members still resolve', () => {
95+
// The guard is `hasOwnProperty`, not a denylist of names: an author who
96+
// really wrote such a key gets it back. No BCP-47 tag looks like this, but
97+
// a denylist-shaped fix would answer differently here and that difference
98+
// is worth pinning.
99+
expect(pickLocalized({ constructor: 'Ctor', en: 'Pricing' }, 'constructor')).toBe('Ctor');
100+
expect(pickLocalized({ toString: 'Str', en: 'Pricing' }, 'toString')).toBe('Str');
101+
});
102+
103+
it('a map with a null prototype resolves identically to a plain object', () => {
104+
const nullProto = Object.assign(Object.create(null), { en: 'Pricing' }) as Record<string, unknown>;
105+
expect(pickLocalized(nullProto, 'constructor')).toBe('Pricing');
106+
expect(pickLocalized(nullProto, 'en')).toBe('Pricing');
107+
});
108+
});
109+
110+
describe('pickLocalized — only `string` values are eligible, on every limb (objectui#3907)', () => {
111+
// Off-spec input: the framework's `InlineLocaleMapSchema` is
112+
// `z.record(<tag>, z.string())`, so nothing in contract can hold these. The
113+
// renderer must still not paint '[object Object]' onto a screen — a
114+
// non-string value is treated as absent and the limb misses.
115+
116+
it('limb 1 (exact tag): a non-string value falls through instead of stringifying', () => {
117+
expect(pickLocalized({ 'zh-CN': { nested: 'x' }, en: 'Pricing' }, 'zh-CN')).toBe('Pricing');
118+
});
119+
120+
it('limb 2 (base language): a non-string value falls through', () => {
121+
expect(pickLocalized({ zh: { nested: 'x' }, en: 'Pricing' }, 'zh-CN')).toBe('Pricing');
122+
});
123+
124+
it('limb 4 (`default`): a non-string value falls through', () => {
125+
expect(pickLocalized({ default: { nested: 'x' }, ja: '価格' }, 'fr')).toBe('価格');
126+
});
127+
128+
it('limb 5 (`en`): a non-string value falls through', () => {
129+
expect(pickLocalized({ en: { nested: 'x' }, ja: '価格' }, 'fr')).toBe('価格');
130+
});
131+
132+
it('never renders the stringified object on any limb', () => {
133+
const offSpec = [
134+
[{ 'zh-CN': { n: 1 }, en: 'Pricing' }, 'zh-CN'],
135+
[{ zh: { n: 1 }, en: 'Pricing' }, 'zh-CN'],
136+
[{ default: { n: 1 }, ja: '価格' }, 'fr'],
137+
[{ en: { n: 1 }, ja: '価格' }, 'fr'],
138+
[{ 'zh-CN': [1, 2], en: 'Pricing' }, 'zh-CN'],
139+
[{ en: { n: 1 } }, 'fr'],
140+
] as const;
141+
for (const [map, locale] of offSpec) {
142+
expect(pickLocalized(map, locale)).not.toBe('[object Object]');
143+
}
144+
});
145+
146+
it('an off-spec value on every limb at once is a miss, not a stringified object', () => {
147+
expect(pickLocalized({ en: { n: 1 }, ja: { n: 2 } }, 'fr')).toBe('');
148+
});
149+
150+
it('an empty-string value is still a hit — `""` is a label the author wrote', () => {
151+
// The boundary the string filter must not cross: `''` is a string, so the
152+
// limb hits and the chain stops. Pinned because a truthiness-based filter
153+
// would pass every test above and break exactly this.
154+
expect(pickLocalized({ en: '', 'zh-CN': '定价' }, 'en')).toBe('');
155+
expect(pickLocalized({ en: '' }, 'fr')).toBe('');
156+
expect(pickLocalized({ 'zh-CN': '' }, 'zh')).toBe('');
157+
});
158+
159+
it('numeric and boolean VALUES are not labels (they were stringified before)', () => {
160+
// Distinct from a scalar passed as the whole `value`, which is still
161+
// stringified by design — asserted in the controls below.
162+
expect(pickLocalized({ en: 42, ja: '価格' }, 'fr')).toBe('価格');
163+
expect(pickLocalized({ en: true, ja: '価格' }, 'fr')).toBe('価格');
164+
expect(pickLocalized({ en: null, ja: '価格' }, 'fr')).toBe('価格');
165+
});
166+
});
167+
168+
describe('pickLocalized — controls: every real language tag is byte-identical (objectui#3907)', () => {
169+
/** `[map, locale, the string this resolved to BEFORE the #3907 guards]` */
170+
const UNCHANGED: ReadonlyArray<readonly [Record<string, unknown>, string | undefined, string]> = [
171+
// Limb 1 — exact tag.
172+
[{ en: 'Pricing', 'zh-CN': '定价' }, 'zh-CN', '定价'],
173+
[{ en: 'Pricing', 'zh-CN': '定价' }, 'en', 'Pricing'],
174+
[{ en: 'Pricing', 'zh-CN': '定价' }, ' zh-CN ', '定价'],
175+
// Limb 2 — base language.
176+
[{ en: 'Pricing', zh: '定价' }, 'zh-CN', '定价'],
177+
[{ en: 'Pricing', zh: '定价' }, 'zh-Hans-CN', '定价'],
178+
[{ zh: '基础', 'zh-CN': '区域' }, 'zh', '基础'],
179+
// Limb 3 — a regional key sharing the base.
180+
[{ en: 'Pricing', 'zh-CN': '定价' }, 'zh', '定价'],
181+
[{ en: 'Pricing', 'zh-CN': '定价' }, 'zh-TW', '定价'],
182+
[{ 'pt-BR': 'Preços' }, 'pt-PT', 'Preços'],
183+
[{ 'zh-TW': '區域', 'zh-CN': '区域' }, 'zh', '區域'],
184+
// Limb 4 — `default` outranks `en`.
185+
[{ default: 'D', en: 'E' }, 'fr', 'D'],
186+
// Limb 5 — `en`.
187+
[{ en: 'E', ja: 'J' }, 'fr', 'E'],
188+
[{ en: 'E', 'zh-CN': 'Z' }, undefined, 'E'],
189+
[{ en: 'E', 'zh-CN': 'Z' }, '', 'E'],
190+
// Limb 6 — last resort, in key order.
191+
[{ ja: 'J' }, 'fr', 'J'],
192+
[{ ja: 'J', ko: 'K' }, 'fr', 'J'],
193+
// The language subtag's case still matters; the region's still does not.
194+
[{ 'zh-CN': '定价' }, 'zh-cn', '定价'],
195+
[{ 'zh-CN': '定价', en: 'Pricing' }, 'ZH-CN', 'Pricing'],
196+
// Misses.
197+
[{}, 'en', ''],
198+
[{}, undefined, ''],
199+
];
200+
201+
it.each(UNCHANGED)('resolves %j at locale %j to %j, exactly as before', (map, locale, expected) => {
202+
expect(pickLocalized(map, locale)).toBe(expected);
203+
});
204+
205+
it('the non-map forms are untouched', () => {
206+
expect(pickLocalized('Pricing', 'zh-CN')).toBe('Pricing');
207+
expect(pickLocalized('', 'zh-CN')).toBe('');
208+
expect(pickLocalized(null, 'zh')).toBe('');
209+
expect(pickLocalized(undefined, 'zh')).toBe('');
210+
// A scalar passed as the whole value is still stringified — that is the
211+
// documented pass-through, and it is NOT what the value filter governs.
212+
expect(pickLocalized(42, 'en')).toBe('42');
213+
expect(pickLocalized(true, 'en')).toBe('true');
214+
});
215+
});

‎packages/i18n/src/pickLocalized.ts‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,58 @@
1111
*
1212
* Pure — pair it with `useObjectTranslation().language` (or any current-locale
1313
* source) at the call site.
14+
*
15+
* Every limb reads **own properties only** and accepts **only `string` values**
16+
* (objectui#3907). Both guards used to hold on the regional and last-resort
17+
* limbs alone, which left the other four able to resolve a locale against
18+
* `Object.prototype` — `pickLocalized({ en: 'Pricing' }, 'constructor')`
19+
* rendered the constructor's source text as the label — and able to
20+
* short-circuit the chain with a non-string value, rendering `[object Object]`.
21+
* Neither is reachable by an input the contract admits (no BCP-47 tag is an
22+
* `Object.prototype` member, and `InlineLocaleMapSchema` is
23+
* `z.record(<tag>, z.string())`), so applying them uniformly changes nothing
24+
* for any real language tag. It makes this function agree limb for limb with
25+
* the backend twin `resolveI18nLabel` (`@objectstack/spec`, objectstack#6765),
26+
* whose locale can arrive from an untrusted `Accept-Language` header and which
27+
* shipped with exactly these two narrowings recorded as deliberate departures.
28+
* The only remaining difference between the two is how each spells a miss —
29+
* `''` here for a text node, `undefined` there for a producer's `?? name` chain
30+
* — pinned in `plugin-list/src/__tests__/i18nLabel-resolver-parity.test.ts`.
31+
*
32+
* A guard makes its limb **miss**; it never aborts the resolution. An unusable
33+
* entry falls through to the next limb exactly as an absent one does, so
34+
* `pickLocalized({ en: 'Pricing' }, 'constructor')` is `'Pricing'`, not `''`.
1435
*/
1536
export function pickLocalized(value: unknown, language: string | undefined | null): string {
1637
if (value == null) return '';
1738
if (typeof value === 'string') return value;
1839
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
1940
if (typeof value === 'object') {
2041
const o = value as Record<string, unknown>;
42+
/**
43+
* One entry, or `undefined` when this limb does not hit — which is what
44+
* lets the limbs keep chaining through `??` in the same order as before.
45+
*/
46+
const read = (key: string): string | undefined => {
47+
if (!Object.prototype.hasOwnProperty.call(o, key)) return undefined;
48+
const entry = o[key];
49+
return typeof entry === 'string' ? entry : undefined;
50+
};
2151
const lang = (language || 'en').trim();
2252
const base = lang.split('-')[0];
2353
// Runtime language is often a bare base code ('zh') while metadata authors
2454
// write full BCP-47 tags ('zh-CN') — upgrade to any key sharing the base.
25-
const regional = Object.keys(o).find((k) => k.split('-')[0] === base && typeof o[k] === 'string');
55+
// `Object.keys` is already own-and-enumerable, so `read` here is uniformity
56+
// rather than a second guard.
57+
const regional = Object.keys(o).find((k) => k.split('-')[0] === base && read(k) !== undefined);
2658
const pick =
27-
o[lang] ??
28-
o[base] ??
29-
(regional !== undefined ? o[regional] : undefined) ??
30-
o.default ??
31-
o.en ??
32-
Object.values(o).find((v) => typeof v === 'string');
33-
return pick == null ? '' : String(pick);
59+
read(lang) ??
60+
read(base) ??
61+
(regional !== undefined ? read(regional) : undefined) ??
62+
read('default') ??
63+
read('en') ??
64+
Object.values(o).find((v): v is string => typeof v === 'string');
65+
return pick == null ? '' : pick;
3466
}
3567
return String(value);
3668
}

0 commit comments

Comments
 (0)