feat(custom-widgets): complete Custom JSX v2 (#6390)

This commit is contained in:
Thomas authored and ajnart committed 2026-08-13 19:48:57 +02:00
1 parent 3a1245cbc5
commit acdfcbb18e
416 files changed
+37853 -18066

No files matched your search

@@ -1,10 +1,13 @@
import { customJsxSupportedPropsByName } from "../core";
import { isValidElement } from "react";
import { isInterpreterCallback, SafeJsxError } from "./interpreter-foundation";
import { customJsxComponentByName } from "../core/component-registry";
import { getInvalidCustomJsxPropValueReason } from "./runtime-component-policy";
import {
CUSTOM_JSX_BLOCKED_PROPERTIES,
CUSTOM_JSX_BLOCKED_PROPS,
CUSTOM_JSX_BLOCKED_STYLE_KEYS,
CUSTOM_JSX_URL_PROPS,
isBlockedCustomJsxProp,
normalizeCustomJsxProperty,
} from "./policy";
@@ -61,7 +64,7 @@ function sanitizeStyle(value: unknown): Record<string, string | number> | undefi
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
const result: Record<string, string | number> = {};
for (const [key, styleValue] of Object.entries(value)) {
if (CUSTOM_JSX_BLOCKED_STYLE_KEYS.has(key) || key.startsWith("--")) continue;
if (CUSTOM_JSX_BLOCKED_STYLE_KEYS.has(key)) continue;
if (typeof styleValue !== "string" && typeof styleValue !== "number") continue;
if (typeof styleValue === "string" && /(?:url\s*\(|expression\s*\(|javascript:|position\s*:)/i.test(styleValue)) {
continue;
@@ -71,26 +74,129 @@ function sanitizeStyle(value: unknown): Record<string, string | number> | undefi
return result;
}
function sanitizeStyles(value: unknown): Record<string, Record<string, string | number>> | undefined {
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
const result: Record<string, Record<string, string | number>> = {};
for (const [selector, styles] of Object.entries(value)) {
if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(selector)) continue;
const sanitized = sanitizeStyle(styles);
if (sanitized) result[selector] = sanitized;
}
return result;
}
export function sanitizeCustomJsxProps(
props: Readonly<Record<string, unknown>>,
componentName?: string,
): Record<string, unknown> {
const safe: Record<string, unknown> = {};
const supportedProps = componentName ? customJsxSupportedPropsByName.get(componentName) : undefined;
for (const [key, value] of Object.entries(props)) {
if ((/^on/i.test(key) && !supportedProps?.has(key)) || CUSTOM_JSX_BLOCKED_PROPS.has(key)) continue;
if (componentName && !supportedProps?.has(key)) continue;
if (typeof value === "function" || isInterpreterCallback(value)) continue;
const blockedProps = componentName
? new Set(customJsxComponentByName.get(componentName)?.blockedProps.map(({ name }) => name) ?? [])
: undefined;
return sanitizeObject(props, blockedProps, componentName);
}
function sanitizeObject(
value: Readonly<Record<string, unknown>>,
componentBlockedProps?: ReadonlySet<string>,
componentName?: string,
): Record<string, unknown> {
const safe: Record<string, unknown> = Object.create(null);
for (const [key, child] of Object.entries(value)) {
if (isBlockedCustomJsxProp(key) || componentBlockedProps?.has(key)) continue;
if (componentName && getInvalidCustomJsxPropValueReason(componentName, key, child)) continue;
if (typeof child === "function" || isInterpreterCallback(child)) continue;
if (CUSTOM_JSX_URL_PROPS.has(key)) {
if (isSafeUrl(value)) safe[key] = value;
if (isSafeUrl(child)) safe[key] = child;
continue;
}
if (key === "style") {
const style = sanitizeStyle(value);
const style = sanitizeStyle(child);
if (style) safe.style = style;
continue;
}
safe[key] = value;
if (key === "styles") {
const styles = sanitizeStyles(child);
if (styles) safe.styles = styles;
continue;
}
safe[key] = key === "data" || key === "series" ? sanitizeSerializableData(child) : sanitizeNestedValue(child);
}
return safe;
}
function sanitizeSerializableData(value: unknown): unknown {
if (typeof value === "function" || isInterpreterCallback(value)) return undefined;
if (Array.isArray(value)) return value.map(sanitizeSerializableData);
if (value !== null && typeof value === "object") {
const safe: Record<string, unknown> = Object.create(null);
for (const [key, child] of Object.entries(value)) {
if (CUSTOM_JSX_BLOCKED_PROPERTIES.has(normalizeCustomJsxProperty(key))) continue;
safe[key] = sanitizeSerializableData(child);
}
return safe;
}
return value;
}
function sanitizeNestedValue(value: unknown): unknown {
if (typeof value === "function" || isInterpreterCallback(value)) return undefined;
if (isValidElement(value)) return value;
if (Array.isArray(value)) return value.map(sanitizeNestedValue);
if (value !== null && typeof value === "object") return sanitizeObject(value as Record<string, unknown>);
return value;
}
export function diagnoseCustomJsxProps(props: Readonly<Record<string, unknown>>, componentName: string): string[] {
const diagnostics: string[] = [];
const blockedProps = new Map(
customJsxComponentByName.get(componentName)?.blockedProps.map(({ name, reason }) => [name, reason]) ?? [],
);
diagnoseObject(props, componentName, diagnostics, blockedProps, componentName);
return diagnostics;
}
function diagnoseObject(
value: Readonly<Record<string, unknown>>,
path: string,
diagnostics: string[],
componentBlockedProps?: ReadonlyMap<string, string>,
componentName?: string,
) {
for (const [key, child] of Object.entries(value)) {
const propertyPath = `${path}.${key}`;
const componentBlockedReason = componentBlockedProps?.get(key);
if (componentBlockedReason) {
diagnostics.push(
`BLOCKED_CAPABILITY: Prop '${propertyPath}' is not allowed because ${componentBlockedReason.toLowerCase()}`,
);
continue;
}
const invalidValueReason = componentName ? getInvalidCustomJsxPropValueReason(componentName, key, child) : null;
if (invalidValueReason) {
diagnostics.push(`INVALID_PROP_VALUE: '${propertyPath}' is not allowed because ${invalidValueReason}`);
continue;
}
if (isBlockedCustomJsxProp(key) || typeof child === "function" || isInterpreterCallback(child)) {
diagnostics.push(`BLOCKED_CAPABILITY: Prop '${propertyPath}' is not allowed`);
continue;
}
if (CUSTOM_JSX_URL_PROPS.has(key) && !isSafeUrl(child)) {
diagnostics.push(`INVALID_PROP_VALUE: '${propertyPath}' contains an unsafe URL`);
continue;
}
if (isValidElement(child)) continue;
if (key === "data" || key === "series") continue;
if (Array.isArray(child)) {
child.forEach((entry, index) => {
if (isValidElement(entry)) return;
if (isInterpreterCallback(entry) || typeof entry === "function") {
diagnostics.push(`BLOCKED_CAPABILITY: Prop '${propertyPath}[${index}]' is not allowed`);
} else if (entry !== null && typeof entry === "object") {
diagnoseObject(entry as Record<string, unknown>, `${propertyPath}[${index}]`, diagnostics);
}
});
} else if (child !== null && typeof child === "object" && key !== "style" && key !== "styles") {
diagnoseObject(child as Record<string, unknown>, propertyPath, diagnostics);
}
}
}