GHSA-VHVH-MQJM-8VPG
TemplateContext.Reset() does not clear Tags, leaking cycle and random state across reused renders
Texto original en inglés · Ver en español
CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:L/A:H
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-VHVH-MQJM-8VPG divulgada · CVSS 7.6 (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
`TemplateContext.Reset()` in Scriban is documented as making a context safe to reuse on the same thread for another run, but it does not clear the context's `Tags` dictionary. Built-in functions store render-specific state in `Tags`: `array.cycle` keeps a per-group position counter and `math.random` keeps a `System.Random` instance. Any application that pools or reuses a `TemplateContext` and calls `Reset()` between renders will carry this state from one render into the next, so a later render observes cycle positions and random sequences seeded by an earlier render. An attacker who can submit template content to a reused context can read residual cycle/random state left by a prior render and influence the state a subsequent render will observe.
Root Cause
The security invariant that `Reset()` is expected to uphold is that no render-specific state survives into the next reuse of the context. `Reset()` clears the output, global scope, culture and source-file stacks, the compiled-template cache, and member accessors, but it never clears `Tags`. The `Tags` dictionary is created once in the constructor and is never emptied anywhere else in the library, so every entry written during one render remains visible to all subsequent renders on the same context.
Affected component: Scriban template engine (NuGet package Scriban)
- Scriban.TemplateContext.Reset() at src/Scriban/TemplateContext.cs:1026 omits any clear of Tags
- Tags dictionary created only in the constructor at src/Scriban/TemplateContext.cs:205
- Tags property (getter-only, contents mutable) at src/Scriban/TemplateContext.cs:412
- array.cycle writes the position counter at src/Scriban/Functions/ArrayFunctions.cs:209
- math.random stores a Random instance under the constant key "random" at src/Scriban/Functions/MathFunctions.cs:388Impact
The prerequisite is an application that reuses a single `TemplateContext` across more than one render (the usage pattern `Reset()` exists to support) and that renders `array.cycle` or `math.random`. The attacker needs the ability to supply template content to that reused context, which is a privileged-but-common capability in multi-tenant or user-template scenarios. The demonstrated capability is cross-render leakage of cycle and random state: a render observes the cycle position reached by a prior render that used the same cycle group, and a render continues the `System.Random` sequence started by a prior render rather than beginning a fresh, isolated one. This breaks the per-render isolation that `Reset()` promises. Because `array.cycle` groups are keyed only by the cycle list contents, two renders using the same cycle expression collide deterministically. The impact is confined to the template engine's own authority: output-correctness and state-predictability across renders, not code execution. Beyond that leakage, `Tags` is an unbounded store that no code path ever clears, and `array.cycle` creates a separate entry for every distinct cycle group, with the group derived from attacker-supplied template content. Because `Reset()` does not reclaim those entries, every render permanently adds to the dictionary held by a reused context, so repeated renders accumulate memory that is never released while the context lives, progressively exhausting the hosting process and eventually denying the rendering service altogether. `System.Random` is not a cryptographic generator, so this is a predictability and isolation defect rather than a direct secret disclosure.
Proof of Concept
mkdir scriban-tags-poc
cd scriban-tags-poc
dotnet new console --framework net8.0
dotnet add package Scriban --version 7.5.3Replace `Program.cs` with the following, which reuses one context across two renders with a `Reset()` in between:
using Scriban;
var context = new TemplateContext();
var template = Template.Parse("{{ array.cycle ['one','two','three'] }}");
// First render (e.g. request/tenant A)
Console.WriteLine("first=" + template.Render(context));
context.Reset();
// Second render (e.g. request/tenant B) on the same reused context
Console.WriteLine("second=" + template.Render(context));Expected behavior for an isolated context, versus the actual behavior this code produces (derived from the `array.cycle` implementation at `src/Scriban/Functions/ArrayFunctions.cs:182-211`, where the first render returns `list[0]` and stores index `1` in `Tags`, and the second render reads the surviving index `1` because `Reset()` did not clear `Tags`):
# actual output
first=one
second=two
# expected output if Reset() isolated the context
first=one
second=oneThe second render resumes at the cycle position left by the first render rather than restarting, demonstrating that the cycle counter in `Tags` survived `Reset()`.
Attack Chain
- Exposure: The application reuses one `TemplateContext` across multiple renders and calls `TemplateContext.Reset()` between them, the documented reuse pattern (`src/Scriban/TemplateContext.cs:1026`). The attacker can submit template content rendered on that context.
- Control: The attacker supplies a template containing `array.cycle` (or `math.random`). No validation restricts these built-in calls (`src/Scriban/Functions/ArrayFunctions.cs:182`, `src/Scriban/Functions/MathFunctions.cs:377`).
- Path: `array.cycle` reads and writes the position counter in `context.Tags` keyed by a `CycleKey` whose equality is purely the group string (`src/Scriban/Functions/ArrayFunctions.cs:195-209`, `:1220-1224`); `math.random` reads and writes a `Random` instance under the constant key `"random"` (`src/Scriban/Functions/MathFunctions.cs:384-391`).
- Guard: `Reset()` is expected to clear render-specific state before reuse but omits `Tags` entirely (`src/Scriban/TemplateContext.cs:1026-1055`); no other code path clears `Tags`.
- Primitive: The surviving `Tags` entries let a later render observe and continue the cycle position and random sequence established by an earlier render.
- Result: Render-to-render isolation is broken: cycle output and random sequences depend on prior renders rather than being reset, so a render leaks and influences state across the trust boundary the application relies on `Reset()` to enforce.
Bypass Evidence
The relevant protection is `TemplateContext.Reset()` itself, which the library documents as making the context safe for reuse. The strongest challenge was to find any code that clears `Tags`: a search for `Tags.Clear`, `Tags.Remove`, and reassignment of `Tags` across `src/Scriban/` returns no results, and the `Tags` dictionary is assigned only once in the constructor (`src/Scriban/TemplateContext.cs:205`). `Reset()` (`src/Scriban/TemplateContext.cs:1026-1055`) clears `CachedTemplates`, `_memberAccessors`, the output/global/culture/source-file stacks, and output-limit tracking, but never references `Tags`. The `array.cycle` key `CycleKey` compares only the group string (`src/Scriban/Functions/ArrayFunctions.cs:1224`), so identical cycle expressions in different renders collide, and `math.random` always uses the constant key `"random"` (`src/Scriban/Functions/MathFunctions.cs:28`). This finding concerns a different state container than the related advisory for this project, which cleared only the compiled-template cache (`CachedTemplates.Clear()` at `src/Scriban/TemplateContext.cs:1052`) and did not address `Tags`.
Affected Versions
- Ecosystem: `nuget`
- Package: `Scriban` (the companion package `Scriban.Signed` is built from the same source and is equally affected)
- Confirmed affected range: `<= 7.5.3`
- Latest release checked: `7.5.3` (nuget.org
https://www.nuget.org/packages/Scriban/7.5.3) - Fix status: fixed in 7.6.0 in any released version through 7.5.3
The omission of a `Tags` clear from `Reset()` is present in the earliest tag available from source (7.0.1) and remains in 7.5.3, the latest release. The introduction boundary below 7.0.1 could not be verified from available source, so the lower bound is left open.
Suggested Fix
Enforce the reuse invariant by clearing all render-specific state in `Reset()`, adding `Tags.Clear()` alongside the existing `CachedTemplates.Clear()` so that cycle counters, the stored `Random` instance, and any other `Tags` entries do not survive into a reused render. Consider auditing every piece of state written during a render (anything a built-in function stores on the context) to ensure each is reset, since `Tags` is a general-purpose store that other functions may also use. Until a fixed release is available, applications can avoid the issue by constructing a fresh `TemplateContext` for each independent render instead of reusing one via `Reset()`.
---
Reported by zx.
Remediation in Scriban 7.6.0
Upgrade either distribution, Scriban or Scriban.Signed, to 7.6.0 or later.
TemplateContext.Reset() now discards builtin cycle positions and the random generator without clearing host-owned Tags. This prevents cross-render builtin state retention and reclaims attacker-controlled cycle group strings. System.Random remains non-cryptographic and is not suitable for secrets. This report overlaps with GHSA-8637-5q5q-hxvw.
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-vhvh-mqjm-8vpgLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
