fix(executor): bound Rhai→JSON materialization to prevent anonymous OOM
The per-element Rhai sandbox caps (max_string_size 64 KiB, max_array_size / max_map_size 10 000) do NOT bound the *materialized* size: Rhai shares strings and arrays by Rc, so a 10 000-element array of one aliased 64 KiB string is cheap to build (~10 000 ops, far under the 1 M op budget) yet dealiases to ~640 MiB of distinct JSON. Every Dynamic→JSON conversion deep-copies the aliases; the HTTP response body path had NO size cap at all, and the KV/docs value caps run only AFTER full materialization — so a handful of anonymous requests could OOM the node (32 concurrent × ~640 MiB ≈ 20 GiB) on the stated consumer-hardware target. Add a byte-budgeted materializer that bails the moment the budget is exceeded, so the transient allocation is bounded regardless of aliasing: - bridge.rs: `dynamic_to_json_capped(value, max) -> Result<Json, JsonSizeError>` (charges a running budget) + `MAX_JSON_MATERIALIZE_BYTES` (16 MiB hard rail, far above the 256 KiB business caps). `dynamic_to_json` stays infallible for best-effort paths (logging) but is now internally bounded — it collapses an over-limit value to a marker string instead of OOMing. - Route every user-value boundary through the fail-closed capped form: KV set/set_if (+ the CAS `expected`), docs create/update/find, secrets set, http request body, workflow input, `json::stringify`, and the HTTP RESPONSE body (previously the one fully-uncapped exit → now a 500). `invoke` args and the pubsub/queue message materializers get the same byte budget in place. - `impl From<JsonSizeError> for Box<EvalAltResult>` so SDK sites protect themselves with a bare `?`. Pinned by bridge unit tests: an aliased 10 000×64 KiB array errors on the budget rather than materializing, and the infallible wrapper yields a marker instead of OOMing. Workspace 914 + journeys 157/157 green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -21,7 +21,9 @@ use std::sync::Arc;
|
||||
use picloud_shared::{SdkCallCx, SecretsListPage, Services};
|
||||
use rhai::{Array, Dynamic, Engine as RhaiEngine, EvalAltResult, Map, Module};
|
||||
|
||||
use super::bridge::{block_on, dynamic_to_json, json_to_dynamic, runtime_err};
|
||||
use super::bridge::{
|
||||
block_on, dynamic_to_json_capped, json_to_dynamic, runtime_err, MAX_JSON_MATERIALIZE_BYTES,
|
||||
};
|
||||
|
||||
pub(super) fn register(engine: &mut RhaiEngine, services: &Services, cx: Arc<SdkCallCx>) {
|
||||
let svc = services.secrets.clone();
|
||||
@@ -34,7 +36,7 @@ pub(super) fn register(engine: &mut RhaiEngine, services: &Services, cx: Arc<Sdk
|
||||
module.set_native_fn(
|
||||
"set",
|
||||
move |name: &str, value: Dynamic| -> Result<(), Box<EvalAltResult>> {
|
||||
let json = dynamic_to_json(&value);
|
||||
let json = dynamic_to_json_capped(&value, MAX_JSON_MATERIALIZE_BYTES)?;
|
||||
let svc = svc.clone();
|
||||
let cx = cx.clone();
|
||||
block_on("secrets", async move { svc.set(&cx, name, json).await })
|
||||
|
||||
Reference in New Issue
Block a user