Archived
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:
1 parent
2fda03ccdd
commit
7b9f33653f
6 files changed
+104
-4
No files matched your search
Vendored
+26
@@ -27,6 +27,30 @@ class parser {
|
||||
public:
|
||||
parser(const std::vector<token> & 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();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user