diff --git a/tests/chat_test.cpp b/tests/chat_test.cpp index 828dc180..ed75fe82 100644 --- a/tests/chat_test.cpp +++ b/tests/chat_test.cpp @@ -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("", "", "")); diff --git a/vendor/SHA256SUMS b/vendor/SHA256SUMS index 51a42341..efda0cbe 100644 --- a/vendor/SHA256SUMS +++ b/vendor/SHA256SUMS @@ -15,15 +15,15 @@ bb12b9cc7940d6728304b6ae1d0d9c53637098f6df00119d4ba5ef7f186c385e vendor/llama/c 97866824d4dacfdeeb14839183e959d665ac766e12014f38e74610933a8b9cdc vendor/llama/common/jinja/caps.h 3d6774bc2dac477e10ddbee69eff403df36f5925c8512ccf8a249bde5c26dbd7 vendor/llama/common/jinja/lexer.cpp 93aecd72cc952fab08f657aa2a70e9f58421672b19d3179f8bdf555e683a1cc7 vendor/llama/common/jinja/lexer.h -284c3dcc92cfcbdb3391f4b251bdc2bd5191899973bf665e9351aca3b795c724 vendor/llama/common/jinja/parser.cpp +0f1c3902bd181634d9847e23ad360fdd4682777ea8c86b8267e1f87053e10025 vendor/llama/common/jinja/parser.cpp 6225eb759da910f3dd7753aed71c6938131cfa04c609d744e6f1390396422316 vendor/llama/common/jinja/parser.h e3bfa255e10e81c756e1376740d752d6f73762233253f830dea2bcedd50f247f vendor/llama/common/jinja/README.md -dee35610e7de346501f49a0f62bb9c99f9dc10534a22c0ee20baabf11fc61ca3 vendor/llama/common/jinja/runtime.cpp +d422b8f5d41ac20ecdca451e69773156513cf461063013ad4515163f794b26a6 vendor/llama/common/jinja/runtime.cpp 427e1a8471394674b56c24c1946393b6c242481ffa49f3af396e6b257634a645 vendor/llama/common/jinja/runtime.h 1f7b1d6161c48a735853608849781736b69e526bf31549fc2cb692469f193715 vendor/llama/common/jinja/string.cpp 943ad34975e87c1a39570ac45238e2416cfb99530b53d853547c630198ea4b18 vendor/llama/common/jinja/string.h 61ae8bc57a7a551bdfb294a23a0ef8b5ba8e3cf4d695389f0f0797e2d7689781 vendor/llama/common/jinja/utils.h -9de5a424bc1339427800c3a22b708badd534bba2099ea35676a7e759a2a2cdb9 vendor/llama/common/jinja/value.cpp +4a5e8cec8a4c97f9e79689b09f1a224b4e2ffb16be3d540fa436a95bcfc10edc vendor/llama/common/jinja/value.cpp fe5bb39af419fffc5291796848edc9f6d990c416ea8ff5fe602b9e4b8dd4140b vendor/llama/common/jinja/value.h 00a6eb892deb9c0578fec97e72b8dbb2518216bb325a972987691b67a14ecc02 vendor/llama/common/json-partial.cpp 1ff33e25293b376c9262c5e5842fcb50ebcf8f4e5c5222505a8a757be53fd4a5 vendor/llama/common/json-partial.h @@ -50,7 +50,7 @@ d5104362705e687271807f499481db6033d85988e13fac12f36e370c6fce0551 vendor/shim/ll 5d556a22069133f7931850a13094d7390ba091051f0dc98cb1cfca824142a705 vendor/shim/llama_shim.cpp 628efd8a88524082c4736d3bc52790aa459b40728f213246c54550011f2754a2 vendor/shim/log.h 1c343ff2dbf801873acc2ee67c0e874137fb36df31421f36fc5cb6812463aae4 vendor/shim/ryml_impl.cpp -4e3fcc1f56d00c21967fa16e1d81aa9a012f1669c514ed16226204ff62e25d02 vendor/UPSTREAM +11bdda7f02eede804f1b4c56f8426def63d37a83e6484806567707b207b60dd6 vendor/UPSTREAM 45e384d3d52c73cba3a64d6e6c25d47cd738cd8a55c30629e3201046eda62947 vendor/yyjson/LICENSE d2d58ef0a3b2267862dc363832c4f3185fd375aea933d5d3419d1a940d6ece2e vendor/yyjson/yyjson.c ef803cda5c06b8962face6dfa39c3284b3bcf3e73f4d7317664690cc779679ca vendor/yyjson/yyjson.h diff --git a/vendor/UPSTREAM b/vendor/UPSTREAM index 9abb9aa1..68a26cb2 100644 --- a/vendor/UPSTREAM +++ b/vendor/UPSTREAM @@ -93,6 +93,18 @@ vendor/llama/common/jinja/ <- llama.cpp common/jinja/ hot paths: parts are moved instead of copied through the gather, appends no longer return a copy, the parts join is one compaction pass, the flat render is gathered without a parts list, and loop/member lookups stop rehashing. + Added 2026-11: budgets. A container's chat template is server-side attacker + data and the engine ran it without any: parse_any and parse_expression + recursed once per nesting block/paren with no bound -- tens of KB of `{{ (((` + or `{% if %}{% if %}` stack-overflowed whoever parsed, the capability probe + or the first render, past every catch -- so both now share one 128-level cap + whose throw is the same exception class a malformed template already raises. + And range() emitted one array element per step uncapped -- `range(4000000000)` + OOMed per render -- so it refuses above 65536 values, far past any template + that exists. String repetition is that cap's sibling -- `'x' * 4000000000` + appended one step at a time, uncapped, and chained repetitions multiply past + any bound -- so a repetition whose result passes 64 MiB throws the same + exception, at any step of the chain. vendor/llama/common/chat.{h,cpp} vendor/llama/common/chat-auto-parser.h diff --git a/vendor/llama/common/jinja/parser.cpp b/vendor/llama/common/jinja/parser.cpp index 2b25654a..b7b0935b 100644 --- a/vendor/llama/common/jinja/parser.cpp +++ b/vendor/llama/common/jinja/parser.cpp @@ -27,6 +27,30 @@ class parser { public: parser(const std::vector & t, const std::string & src) : tokens(t), source(src) {} + // RADIANCE: a container's template is server-side attacker data, and both descent families + // recursed without a bound -- every '(' (or '['/'{') re-entered parse_expression through a + // chain of precedence functions, and every nested {% if %}/{% for %} re-entered parse_any. + // A few tens of KB of brackets was an uncatchable stack overflow of whoever parsed: the + // capability probe, or the worker taking the first render. 128 units is far past any human + // template; the throw is the same parser_exception a malformed template already raises, + // which the chat loader catches as a clean refusal of that template. + static constexpr int k_max_nesting = 128; + +private: + int nesting = 0; + +public: + struct nest_guard { + int& n; + explicit nest_guard(int& n) : n(n) { + if (++n > k_max_nesting) { + --n; + throw std::runtime_error("Template nests deeper than 128 levels"); + } + } + ~nest_guard() { --n; } + }; + program parse() { statements body; while (current < tokens.size()) { @@ -94,6 +118,7 @@ private: } statement_ptr parse_any() { + nest_guard nest{ nesting }; size_t start_pos = current; switch (peek().t) { case token::comment: @@ -325,6 +350,7 @@ private: } statement_ptr parse_expression() { + nest_guard nest{ nesting }; // Choose parse function with lowest precedence return parse_if_expression(); } diff --git a/vendor/llama/common/jinja/runtime.cpp b/vendor/llama/common/jinja/runtime.cpp index 50123469..82ab0750 100644 --- a/vendor/llama/common/jinja/runtime.cpp +++ b/vendor/llama/common/jinja/runtime.cpp @@ -277,6 +277,13 @@ value binary_expression::execute_impl(context & ctx) { if (repeat <= 0) { return res; } + // RADIANCE: sibling of the range() cap in value.cpp -- repetition is the same + // attacker-template OOM, one append per step. "x" * 4000000000, and chained + // repetitions that pass a 64 MiB product at any step, are refused the way range() + // is: the same exception class, so errors-as-values carries it across the seam. + if (str.length() != 0 && repeat > (64ll << 20) / (int64_t)str.length()) { + throw raised_exception("string repetition would produce more than 67108864 bytes"); + } for (int64_t i = 0; i < repeat; ++i) { res->val_str.append(str); } diff --git a/vendor/llama/common/jinja/value.cpp b/vendor/llama/common/jinja/value.cpp index e615abf2..291d2048 100644 --- a/vendor/llama/common/jinja/value.cpp +++ b/vendor/llama/common/jinja/value.cpp @@ -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(i));