UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

46 lines (45 loc) • 1.42 kB
//#region src/plugins/host-hook-json.ts const PLUGIN_JSON_VALUE_LIMITS = { maxDepth: 32, maxNodes: 4096, maxObjectKeys: 512, maxStringLength: 65536, maxSerializedBytes: 262144 }; function isPluginJsonValueWithinLimits(e, t) { const n = PLUGIN_JSON_VALUE_LIMITS; t.nodes += 1; if (t.nodes > n.maxNodes || t.depth > n.maxDepth) return false; if (e === null || typeof e === "boolean") return true; if (typeof e === "string") return e.length <= n.maxStringLength; if (typeof e === "number") return Number.isFinite(e); if (Array.isArray(e)) { t.depth += 1; const n = e.every((e) => isPluginJsonValueWithinLimits(e, t)); t.depth -= 1; return n; } if (typeof e !== "object") return false; const r = Object.getPrototypeOf(e); if (r !== Object.prototype && r !== null) return false; const i = Object.keys(e); if (i.length > n.maxObjectKeys) return false; t.depth += 1; const a = i.every((r) => r.length <= n.maxStringLength && isPluginJsonValueWithinLimits(Reflect.get(e, r), t)); t.depth -= 1; return a; } /** Validates that a plugin hook payload is finite, plain JSON under size limits. */ function isPluginJsonValue(e) { if (!isPluginJsonValueWithinLimits(e, { depth: 0, nodes: 0 })) return false; try { return Buffer.byteLength(JSON.stringify(e), "utf8") <= PLUGIN_JSON_VALUE_LIMITS.maxSerializedBytes; } catch { return false; } } //#endregion export { isPluginJsonValue as t };