Skip to content
Open
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: 8 additions & 0 deletions src/theme/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const isPlatformColorSentinel = (v: unknown): boolean =>
typeof v === 'object' &&
('resource_paths' in v || 'semantic' in v || 'dynamic' in v);

// Keys that would otherwise let a crafted `overrides` object (e.g. parsed via
// `JSON.parse` from an external theme config) hijack the prototype of the
// merged output through bracket-notation assignment below.
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);

export const safeMerge = <T,>(base: T, overrides: unknown): T => {
if (
!base ||
Expand All @@ -45,6 +50,9 @@ export const safeMerge = <T,>(base: T, overrides: unknown): T => {
}
const out: Record<string, unknown> = { ...(base as Record<string, unknown>) };
for (const key of Object.keys(overrides as Record<string, unknown>)) {
if (UNSAFE_KEYS.has(key)) {
continue;
}
out[key] = safeMerge(
(base as Record<string, unknown>)[key],
(overrides as Record<string, unknown>)[key]
Expand Down