Skip to content

Add MAX_STRING_SIZE limit to sandbox string multiplication#2149

Closed
kodareef5 wants to merge 1 commit intopallets:mainfrom
kodareef5:sandbox-string-multiplication-limit
Closed

Add MAX_STRING_SIZE limit to sandbox string multiplication#2149
kodareef5 wants to merge 1 commit intopallets:mainfrom
kodareef5:sandbox-string-multiplication-limit

Conversation

@kodareef5
Copy link
Copy Markdown

SandboxedEnvironment limits range() via MAX_RANGE to prevent DoS from large sequences, but does not limit string multiplication. A template expression like {{ "A" * 10**9 }} allocates 1GB of memory instantly in any application rendering untrusted templates in a sandbox.

This adds MAX_STRING_SIZE (default 1,000,000) and a safe_mul function that checks result size before performing string repetition. The * operator is added to intercepted_binops by default so the check applies automatically.

Unaffected operations:

  • Normal arithmetic: {{ 6 * 7 }}42
  • Small strings: {{ "ab" * 5 }}ababababab
  • Lists: {{ [1,2] * 3 }}[1, 2, 1, 2, 1, 2]
  • Floats: {{ 3.14 * 2 }}6.28

Blocked:

  • {{ "A" * 10000000 }}OverflowError

All 911 existing tests pass.

SandboxedEnvironment limits range() via MAX_RANGE to prevent DoS from
large sequences, but does not limit string multiplication. A template
expression like {{ "A" * 10**9 }} allocates 1GB of memory instantly.

Add MAX_STRING_SIZE (default 1,000,000) and a safe_mul function that
checks result size before performing string repetition. Wire it into
the default binop_table and intercepted_binops so the sandbox
intercepts * operations by default.

Normal arithmetic multiplication, small string repetition, and list
multiplication are all unaffected. Only str * int exceeding the limit
is blocked.
@davidism davidism closed this Mar 22, 2026
@davidism
Copy link
Copy Markdown
Member

davidism commented Mar 22, 2026

Set resource limits in the os, docker container, etc. Trying to limit the resource use from within the process is an impossible surface to cover.

Please review our security policy to learn how to responsibly report issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants