vendor: the jinja engine budgets its nesting and its range()

A container's chat template is server-side attacker data the engine
parses and runs without any budget. The parser recursed once per nesting
block or paren: tens of KB of "{{ (((" or "{% if %}" in one
tokenizer.chat_template stack-overflowed whoever read it -- the
capability probe or the first render -- past every catch, and errors-
as-values cannot hold a stack overflow. And range() materialised one
array element per step: {{ range(4000000000) }} was an OOM per render.

Both entry points now share one 128-level guard whose throw is the same
exception class a malformed template already raises, and range() refuses
above 65536 values -- far past any template that exists, small enough
that the whole range stays transient. The refusals cross the ChatTemplate
seam as the error values the seam promises (a chat_test case pins that
both shapes refuse and that real nesting still renders). UPSTREAM keeps
the addition under the chat lift's record; the manifest re-hashes.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:09 +00:00
1 parent 2fda03ccdd
commit 7b9f33653f
6 files changed
+104 -4

No files matched your search

+11
View File
@@ -496,6 +496,17 @@ const func_builtins & global_builtins() {
if (step == 0) {
throw raised_exception("range() step argument must not be zero");
}
// RADIANCE: a container template is server-side attacker data, and range() emitted
// one element per step with no bound: {{ range(4000000000) }} OOMed the process per
// render. No real chat template counts past a handful; 65536 is far past plausible
// and small enough that even the join of the whole range is transient, not fatal.
{
const __int128 span = step > 0 ? (__int128)stop - start : (__int128)start - stop;
const __int128 abs_step = step > 0 ? step : -(__int128)step;
if (span > 0 && (span + abs_step - 1) / abs_step > 65536) {
throw raised_exception("range() would produce more than 65536 values");
}
}
if (step > 0) {
for (int64_t i = start; i < stop; i += step) {
out->push_back(mk_val<value_int>(i));