fix(custom-widgets): address v2 merge review (#6446)
This commit is contained in:
1 parent
6aceb2fd1f
commit
df967751f6
123 files changed
+8632
-1477
No files matched your search
@@ -1,14 +1,85 @@
|
||||
import { createSafeCallable } from "./safe-bindings";
|
||||
import { SafeJsxBudgetError, SafeJsxError } from "./interpreter-foundation";
|
||||
|
||||
export const CUSTOM_JSX_DATA_LIMITS = Object.freeze({
|
||||
depth: 32,
|
||||
nodes: 50_000,
|
||||
stringLength: 200_000,
|
||||
});
|
||||
|
||||
interface SanitizeTask {
|
||||
value: unknown;
|
||||
depth: number;
|
||||
assign(value: unknown): void;
|
||||
}
|
||||
|
||||
function sanitizeData(value: unknown): unknown {
|
||||
if (value === null || typeof value !== "object") return value;
|
||||
if (Array.isArray(value)) return value.map(sanitizeData);
|
||||
const safe: Record<string, unknown> = Object.create(null);
|
||||
for (const [key, child] of Object.entries(value)) {
|
||||
if (["constructor", "__proto__", "prototype"].includes(key)) continue;
|
||||
safe[key] = sanitizeData(child);
|
||||
let result: unknown;
|
||||
let nodes = 0;
|
||||
const seen = new WeakSet<object>();
|
||||
const tasks: SanitizeTask[] = [{ value, depth: 0, assign: (sanitized) => (result = sanitized) }];
|
||||
|
||||
while (tasks.length > 0) {
|
||||
const task = tasks.pop();
|
||||
if (!task) break;
|
||||
if (++nodes > CUSTOM_JSX_DATA_LIMITS.nodes) {
|
||||
throw new SafeJsxBudgetError(`Response data exceeded the node limit (${CUSTOM_JSX_DATA_LIMITS.nodes})`);
|
||||
}
|
||||
if (task.depth > CUSTOM_JSX_DATA_LIMITS.depth) {
|
||||
throw new SafeJsxBudgetError(`Response data exceeded the depth limit (${CUSTOM_JSX_DATA_LIMITS.depth})`);
|
||||
}
|
||||
if (typeof task.value === "string" && task.value.length > CUSTOM_JSX_DATA_LIMITS.stringLength) {
|
||||
throw new SafeJsxBudgetError(
|
||||
`Response data exceeded the string length limit (${CUSTOM_JSX_DATA_LIMITS.stringLength})`,
|
||||
);
|
||||
}
|
||||
if (task.value === null || typeof task.value !== "object") {
|
||||
task.assign(task.value);
|
||||
continue;
|
||||
}
|
||||
if (seen.has(task.value)) throw new SafeJsxError("Response data must not contain circular or repeated objects");
|
||||
seen.add(task.value);
|
||||
|
||||
if (Array.isArray(task.value)) {
|
||||
if (task.value.length > CUSTOM_JSX_DATA_LIMITS.nodes - nodes) {
|
||||
throw new SafeJsxBudgetError(`Response data exceeded the node limit (${CUSTOM_JSX_DATA_LIMITS.nodes})`);
|
||||
}
|
||||
const safe = Array.from<unknown>({ length: task.value.length });
|
||||
task.assign(safe);
|
||||
for (let index = task.value.length - 1; index >= 0; index -= 1) {
|
||||
if (!Object.hasOwn(task.value, index)) continue;
|
||||
tasks.push({
|
||||
value: task.value[index],
|
||||
depth: task.depth + 1,
|
||||
assign: (sanitized) => (safe[index] = sanitized),
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const entries = Object.entries(task.value);
|
||||
if (entries.length > CUSTOM_JSX_DATA_LIMITS.nodes - nodes) {
|
||||
throw new SafeJsxBudgetError(`Response data exceeded the node limit (${CUSTOM_JSX_DATA_LIMITS.nodes})`);
|
||||
}
|
||||
const safe: Record<string, unknown> = Object.create(null);
|
||||
task.assign(safe);
|
||||
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
||||
const [key, child] = entries[index] as [string, unknown];
|
||||
if (["constructor", "__proto__", "prototype"].includes(key)) continue;
|
||||
if (key.length > CUSTOM_JSX_DATA_LIMITS.stringLength) {
|
||||
throw new SafeJsxBudgetError(
|
||||
`Response data exceeded the string length limit (${CUSTOM_JSX_DATA_LIMITS.stringLength})`,
|
||||
);
|
||||
}
|
||||
tasks.push({
|
||||
value: child,
|
||||
depth: task.depth + 1,
|
||||
assign: (sanitized) => (safe[key] = sanitized),
|
||||
});
|
||||
}
|
||||
}
|
||||
return safe;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const safeMath = Object.freeze(
|
||||
|
||||
Reference in new issue
Block a user