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

+44
View File
@@ -1094,6 +1094,50 @@ TEST(mc_a_reply_with_no_call_is_just_content) {
* report tool support for a template without it.
*/
/* The template is container data and the engine parses and runs it. Both sides of that had no
* budget: the parser recursed once per nesting paren/block -- tens of KB of brackets in a
* container's tokenizer.chat_template was an uncatchable stack overflow of the probe or the
* first render -- and range() materialised one value per step, so {{ range(4000000000) }} was
* an OOM per render. The budgets refuse; the refusals cross this seam as the error values the
* block above promises, and templates past any real nesting still render. */
TEST(template_budgets_refuse_as_values_never_as_crashes) {
json msgs = json::array();
msgs.push_back({{"role", "user"}, {"content", "x"}});
ChatOptions opt;
opt.add_generation_prompt = true;
ChatPrompt p;
ChatTemplate parens;
const std::string deep = "{{ " + std::string(5000, '(') + "1" + std::string(5000, ')') + " }}";
CHECK(parens.load(deep, "", "") < 0 || parens.apply(msgs, json(), opt, &p) < 0);
ChatTemplate ifs;
std::string nested;
for (int i = 0; i < 5000; ++i) nested += "{% if 1 %}";
nested += "ok";
for (int i = 0; i < 5000; ++i) nested += "{% endif %}";
CHECK(ifs.load(nested, "", "") < 0 || ifs.apply(msgs, json(), opt, &p) < 0);
/* The engine constant-folds at load, so the huge range is refused at load already; the
* seam's promise is that SOME step refuses with an error value -- same shape as above. */
ChatTemplate big;
CHECK(big.load("{{ range(4000000000)|length }}", "", "") < 0 ||
big.apply(msgs, json(), opt, &p) < 0);
/* Repetition is range()'s sibling: "x" * 4e9 materialised a byte per step. The cap
* refuses at load or at render -- the seam still answers with a value, never memory. */
ChatTemplate rep;
CHECK(rep.load("{{ 'x' * 4000000000 }}", "", "") < 0 ||
rep.apply(msgs, json(), opt, &p) < 0);
/* The shape no real template reaches is refused; what real ones do still renders. */
ChatTemplate sane;
CHECK_OK(sane.load("A{{ (((1))) }}{% if 1 %}{% if 1 %}B{% endif %}{% endif %}{{ range(10)|length }}",
"", ""));
CHECK_OK(sane.apply(msgs, json(), opt, &p));
CHECK(p.prompt.find("A1B10") != std::string::npos);
}
TEST(an_empty_template_is_the_chatml_fallback_and_says_which_one_it_is) {
ChatTemplate t;
CHECK_OK(t.load("", "", ""));