GHSA-PG73-HFG9-25RC
object.from_json deserializes JSON arrays and objects without applying LoopLimit, enabling resource exhaustion
Texto original en inglés · Ver en español
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-PG73-HFG9-25RC divulgada · CVSS 7.5 (GitHub) | |
| Remediación | Parche 7.6.0 | Scriban · 0 d desde divulgación | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Summary
Scriban's `object.from_json` builtin deserializes an attacker-supplied JSON string into Scriban values without participating in the `LoopLimit` iteration budget. The JSON-to-Scriban conversion in `JsonElementExtensions.CopyToScriptArray` and `CopyToScriptObject` iterates every array element and object property in a plain `foreach` loop with no `StepLoop`, `EnterLoopScope`, or `CheckAbort` call. Any caller that renders an untrusted template (or an untrusted JSON argument to `from_json`) and relies on `LoopLimit` as a safe-runtime control can be forced into large amounts of allocation and CPU work by a single expression, even when `LoopLimit` is set to a very small value.
Root Cause
`LoopLimit` enforcement in Scriban is opt-in: each iteration site must explicitly call `TemplateContext.StepLoop()` inside a scope opened by `TemplateContext.EnterLoopScope()`. The JSON deserialization path does neither. `ObjectFunctions.FromJson` receives a `TemplateContext`, but it discards it when it hands the parsed document to the extension method `JsonElement.ToScriban()`, which has no `TemplateContext` parameter and therefore cannot consult or decrement the loop budget. As a result the element/property loops run to completion regardless of `LoopLimit`.
src/Scriban/Functions/ObjectFunctions.cs:545 public static object? FromJson(TemplateContext context, string json)
src/Scriban/Functions/ObjectFunctions.cs:550-551 JsonDocument.Parse(json); doc.RootElement.Clone().ToScriban()
src/Scriban/Runtime/JsonElementExtensions.cs:16 internal static object? ToScriban(this JsonElement model) // no TemplateContext
src/Scriban/Runtime/JsonElementExtensions.cs:39-45 CopyToScriptArray: foreach (var value in json.EnumerateArray()) array.Add(value.ToScriban());
src/Scriban/Runtime/JsonElementExtensions.cs:30-36 CopyToScriptObject: foreach (var property in json.EnumerateObject()) obj.SetValue(...);The asymmetry is clear from the reverse operation: `object.to_json` serialization in the same file guards its iteration with `context.EnterLoopScope()` and `context.StepLoop()` (`src/Scriban/Functions/ObjectFunctions.cs:590,648,660`). The deserialization direction was never given the same protection.
Impact
An attacker who can submit or influence template content, or supply the JSON string argument to `object.from_json`, can cause iteration and allocation far in excess of the configured `LoopLimit`. A JSON array of two-byte elements encodes roughly 500,000 entries within the default 1 MB string budget, each of which is materialized into a `ScriptArray` entry in a single uninterruptible loop. The default `LoopLimit` of 1000 is bypassed by a factor of several hundred. In the documented threat model for this control (template-as-a-service, CMS and email rendering, and other multi-tenant use of Scriban with untrusted template content) repeated requests drive CPU and managed-heap consumption and can degrade or exhaust a shared server. The demonstrated primitive is uncontrolled resource consumption (denial of service); no confidentiality or integrity impact is claimed.
Proof of Concept
mkdir scriban-poc-fromjson
cd scriban-poc-fromjson
dotnet new console --framework net8.0
dotnet add package Scriban --version 7.5.3using Scriban;
using Scriban.Runtime;
// Build a JSON array of ~500,000 small elements (well under the 1 MB LimitToString budget).
var json = "[" + string.Join(",", System.Linq.Enumerable.Repeat("0", 500000)) + "]";
var context = new TemplateContext
{
LoopLimit = 1 // intended to forbid more than a single iteration
};
var model = new ScriptObject();
model.SetValue("payload", json, true);
context.PushGlobal(model);
// A single expression; LoopLimit=1 should make this impossible.
var template = Template.Parse("{{ (payload | object.from_json) | array.size }}");
var sw = System.Diagnostics.Stopwatch.StartNew();
var result = template.Render(context);
sw.Stop();
System.Console.WriteLine($"size={result} elapsed={sw.ElapsedMilliseconds}ms");size=500000 elapsed=... msExpected behavior: with `LoopLimit = 1`, Scriban should abort with a `ScriptRuntimeException` referencing the iteration limit (as it does for `object.to_json` and script `for` loops). Actual behavior: the full 500,000-element array is deserialized and materialized, and the expression returns `500000`.
Attack Chain
- Exposure: A server renders a Scriban template (or evaluates a `from_json` call) whose text or JSON argument is attacker-controlled, relying on `LoopLimit` to cap per-request work. `object.from_json` is a default builtin available to every template.
- Control: The attacker supplies a flat JSON array (or object) string with a very large element/property count; only the element count matters, so values can be a single byte each.
- Path: `ObjectFunctions.FromJson` (`src/Scriban/Functions/ObjectFunctions.cs:545`) parses the string with `JsonDocument.Parse` and calls `doc.RootElement.Clone().ToScriban()` (line 551), dispatching to `JsonElementExtensions.CopyToScriptArray`/`CopyToScriptObject`.
- Guard: The conversion loop at `src/Scriban/Runtime/JsonElementExtensions.cs:39-45` (and `:30-36`) has no `StepLoop`, `EnterLoopScope`, or `CheckAbort`. The `TemplateContext` is not threaded into the extension method, so the loop cannot participate in `LoopLimit`. `JsonDocument.Parse`'s default `MaxDepth` of 64 constrains nesting depth only, not element count.
- Primitive: The loop allocates one `ScriptArray`/`ScriptObject` entry per JSON element with no upper bound tied to `LoopLimit`.
- Result: A single expression performs hundreds of thousands of iterations and allocations, bypassing the configured limit by roughly 500x and enabling CPU and memory exhaustion under repeated requests.
Bypass Evidence
The relevant protection is `LoopLimit`, enforced through `EnterLoopScope()`/`StepLoop()`. A source scan of the deserialization path shows the guard is entirely absent: `grep -n 'StepLoop\|EnterLoopScope\|CheckAbort' src/Scriban/Runtime/JsonElementExtensions.cs` returns nothing, and `CopyToScriptArray`/`CopyToScriptObject` take only a `JsonElement`, structurally preventing any budget check. By contrast the serialization direction `ObjectFunctions.ToJson` opens a loop scope and steps it (`src/Scriban/Functions/ObjectFunctions.cs:590,648,660`), and the generic array builders use the same pattern (`ObjectFunctions.ToScriptArray`, `src/Scriban/Functions/ObjectFunctions.cs:494-507`), confirming the missing enforcement here is an oversight rather than intended behavior. The upstream remediation for the originally reported LoopLimit bypass (commit `d17bd4b`, "Share LoopLimit across nested iteration," released in the 7.x line) modified `ArrayFunctions.cs`, `ObjectFunctions.cs`, `ListAccessor.cs`, `ScriptArray.cs`, `ScriptRange.cs`, and `TemplateContext.cs`, but did not touch `JsonElementExtensions.cs`; the file's only commit in the examined history is a documentation change. The `from_json` path therefore remains unprotected in the latest release. `LimitToString` (default 1 MB) bounds string conversions but not `from_json` input parsing, and still allows roughly 500,000 elements, so it does not block this path.
Affected Versions
- Ecosystem: `nuget`
- Package: `Scriban` (the `Scriban.Signed` distribution shares the same code)
- Confirmed affected range: `<= 7.5.3`
- Latest release checked: `7.5.3` (NuGet
https://www.nuget.org/packages/Scriban/7.5.3) - Fix status: fixed in 7.6.0
The unguarded `JsonElementExtensions` conversion is present at every tag inspected through the current latest release 7.5.3 and was never modified by a security fix. The exact version that introduced `object.from_json` could not be established from the available history, so the lower bound is left open; the range is expressed as all published versions through 7.5.3.
Suggested Fix
Enforce the `LoopLimit` budget on JSON deserialization the same way serialization already does. Thread the `TemplateContext` into the conversion so that `CopyToScriptArray` and `CopyToScriptObject` open a loop scope with `EnterLoopScope()` and call `StepLoop()` once per element and property (including nested arrays and objects reached recursively through `ToScriban()`). The fix must cover both the array and object branches and the recursive descent, not just the top-level collection, so that deeply or widely structured JSON cannot evade the budget. As an interim mitigation, callers that accept untrusted input can validate or cap the size of JSON passed to `object.from_json` before rendering, but this does not substitute for enforcing the limit inside the engine.
Reported by zx.
Remediation in Scriban 7.6.0
Upgrade either distribution, Scriban or Scriban.Signed, to 7.6.0 or later.
object.from_json now threads the active context through recursive JSON conversion. Every array element and object property participates in the same nested LoopLimit budget, and cancellation is checked before parsing and during conversion. Contextless host import APIs retain their previous behavior.
No CVE identifier is requested for this advisory.
_Remediation notes and package metadata updated by CodeAlta._
Incluye detalles técnicos publicados por el investigador. Úselos solo para validar y corregir sus propios sistemas.
Desglose de métricas
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
nugetScriban |
Corregida | 7.6.0 | 0 d | 7.6.1 no afectada |
nugetScriban.Signed |
Corregida | 7.6.0 | 0 d | 7.6.1 no afectada |
7.6.0Arsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/scriban/scriban/security/advisories/GHSA-pg73-hfg9-25rcLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
