Skip to content

Untrusted interpreter

Users want to organise their own mail without asking an administrator: file newsletters into a folder, flag messages from their manager, forward copies elsewhere, or answer automatically while they are away. User scripts give them that control, and the untrusted interpreter runs them.

A user script runs when a message is delivered to the user. It can file the message into folders, set flags, forward it, discard it or send an automatic reply. Users upload and activate their scripts over ManageSieve or JMAP.

Every user can upload a script, and the server cannot vet each one. The interpreter therefore enforces limits on what a script may consume, and lets the administrator disable extensions.

For the same reason, user scripts can call only a subset of the function library. Functions that reach external systems, such as DNS or SQL lookups, are not available. The one exception is llm_prompt, which calls an AI model for accounts that hold the interactAi permission. Two interpreters compares the two interpreters.

The interpreter is configured on the SieveUserInterpreter singleton, found in the WebUI under Settings › Sieve › User Interpreter.

A script can strain the server in two ways: by its size and structure, which matter when it is compiled, and by its behaviour, which matters every time it runs. The limits follow that division and fall into three groups. The first group bounds a script when it is uploaded, and a script that exceeds one of these limits is refused with a compile error:

FieldDefaultLimit
maxScriptSize512 KiBSize of a script.
maxScriptNameLength512Length of a script name.
maxStringLength8192Length of a string constant.
maxNestedBlocks15Depth of nested if, elsif and else blocks.
maxNestedTests15Depth of nested tests.
maxNestedForEvery3Depth of nested foreverypart loops.
maxMatchVars30Match and regular expression variables a script may capture.
maxLocalVars128Variables in scope at any point.
maxVarNameLength32Length of a variable name.
maxHeaderSize1024Length of a header value written by a script.
maxIncludes3include commands in a script.

The second group bounds a script while it runs, which it does for every message the user receives:

FieldDefaultLimit
maxCpuCycles5000Instructions executed in one run, including those of included scripts.
maxNestedIncludes3Depth of nested include commands.
maxVarSize4096Size of a variable in bytes, and of a value built with the + operator; longer values are truncated.
maxRedirects1redirect actions per run.
maxOutMessages3Messages a script may send per run, counting vacation replies, notifications and redirects.
maxReceivedHeaders50Received headers a message may carry before the script treats it as looping. At or above the limit, redirect does nothing and vacation sends no reply, and enclose copies at most this many Received headers onto the new message.

When a script fails at run time, for example by exceeding one of these limits, it stops. Actions it has already taken stay in effect. A message that the script had not yet filed, redirected or discarded is delivered to the inbox, so a failing script does not lose the message.

The headers a script adds to a message are also subject to limits outside the interpreter: the size and number of header fields allowed by maxHeaderSize and maxHeaderCount on the Email singleton. Once an added header would push the message over either limit, that header and every header added after it are dropped.

The third group bounds storage. maxScripts is the number of scripts an account may store, 100 by default. A maxSieveScripts entry in the account’s quotas overrides it for that account.

The following object body tightens the runtime limits for a server with many users:

{
"maxCpuCycles": 2000,
"maxRedirects": 1,
"maxOutMessages": 2
}

Every extension listed in Supported extensions is available to user scripts unless it is named in disableCapabilities.

The extensions an administrator typically considers disabling are the ones with effects beyond the user’s own mailbox. enotify sends notifications to other addresses. editheader, replace and enclose change the content of the message, which can remove information that the administrator or other filters rely on. Forwarding with redirect is part of the base language and cannot be disabled, but it is bounded by maxRedirects.

Disabling an extension affects scripts in different ways, depending on when they were stored. Clients learn which extensions are available from the capability list of ManageSieve and JMAP, which omits disabled extensions. A new script that requires a disabled extension is refused at upload, with a compile error such as Capability 'enotify' is not supported at line <line>, column <column>..

Existing scripts are a different matter. Scripts that users uploaded before the extension was disabled are not checked again, so they fail at delivery when they reach it. A global script that requires a disabled extension is skipped when the configuration is loaded, with the build warning Failed to compile user global Sieve script, skipping it: <error>.

The following object body disables content modification and notifications:

{
"disableCapabilities": {"editheader": true, "replace": true, "enclose": true, "enotify": true}
}

The accepted names are listed in the SieveCapability enumeration.

Two settings narrow extensions without disabling them. The URI schemes that notify may use are the keys of allowedNotifyUris, which by default allows only mailto.

The header fields that editheader may neither add nor delete are the keys of protectedHeaders. The default protects Auto-Submitted, Original-From, Original-Subject and Received, so that a script cannot hide the path a message took or remove the marker of an automatic reply.

The vacation extension sends an automatic reply to each sender. Replying to every message would flood correspondents who write often, so a sender receives a reply at most once within a period. When the script does not specify the period, the same sender receives another reply only after defaultExpiryVacation, 30 days by default.

A delivery produces at most one automatic reply. The first vacation action that is due sends its reply, and later ones in the same run, whether in an included script, the managed script or the user’s own script, are skipped without an error. A script that also rejects the message sends no reply at all, since a rejection withdraws the automatic reply of the same delivery. Redirects and notifications are still sent, and a withdrawn reply is not recorded, so the sender can receive it with the next message. Automatic replies describes how this applies to mail rules and the vacation response.

The interpreter also fills in a subject when the script gives none. The reply then carries the original subject prefixed with defaultSubjectPrefix, Auto: by default. When the original subject is not known, it carries defaultSubject, Automated reply by default.

The duplicate extension lets a script recognise a message it has already seen, for example a copy that arrives through two mailing lists. It records the identifiers of the messages it tests. When the script does not specify an expiry, the identifiers expire after defaultExpiryDuplicate, 7 days by default.

Messages that a user script sends must pass the receivers’ authentication checks like any other mail. Vacation replies and the other messages a user script sends are therefore DKIM-signed, according to dkimSignDomain.

A script cannot use the text it places in these messages to add header fields of its own. Line breaks are removed from the subject and the :from address of vacation replies, from the subject of enclose and of notifications, and from the header values of notifications, and these values are truncated to the maximum header size. A mailto: notification whose address contains an encoded line break is invalid, and a notification header whose name contains characters other than letters, digits, - and _ is refused. Notifications cannot set From, Date, Message-ID, Auto-Submitted, Received, Return-Path, MIME-Version or any Content-* or Resent-* header: such headers are ignored, and the server always generates Date and Message-ID.

For these messages, the sender and sender_domain variables of the expression refer to the address of the account whose script sends the message. The envelope sender of the reply is empty (<>), as RFC 5230 recommends for vacation replies. The default expression signs with the account’s domain when it is a domain hosted on the server, and does not sign otherwise. The signature therefore aligns with the domain in the From header of the reply, which is the condition receivers check under DMARC.

A reject or ereject in a user script refuses the message after it has been accepted, so the refusal reaches the sender as a bounce. To avoid sending bounces to forged addresses, the server sends one only when the return path is verified: the message was submitted by an authenticated user, passed SPF on MAIL FROM, or was sent by a Sieve script of an account on the server. Otherwise the message is discarded silently. In practice this means that rejections bounce only where SPF is checked on MAIL FROM, which by default is port 25. Rejected messages lists the cases and their consequences for LMTP, gateways and forwarded mail.

A user script runs after the spam filter has scored the message, so it can take the verdict into account. The script can test the result with the spamtest extension, or with the X-Spam-Score header.

The script also takes precedence over the Junk folder. When the script ends without filing the message, a message classified as spam is moved to the Junk folder as usual. When the script files the message with fileinto, including fileinto "INBOX", the message stays in the folder the script chose. It is not moved to Junk, and its X-Spam-Status header reads No.

Global scripts let the administrator publish ready-made user scripts that every user can adopt.

Each global script is a SieveUserScript, found in the WebUI under Settings › Sieve › User Scripts. It has a name, an optional description, the script text in contents, and an isActive flag. The flag is false by default, so a new global script is not available until it is activated.

A global script is offered, not imposed. It runs only when a user’s script includes it with include :global "<name>", and it then runs with the user’s permissions in the untrusted interpreter.

The following object publishes a global script that files messages from mailing lists into a Lists folder:

{
"name": "file-lists",
"description": "File mailing list messages",
"isActive": true,
"contents": "require [\"fileinto\", \"mailbox\"];\n\nif exists \"List-Id\" {\n fileinto :create \"Lists\";\n}\n"
}

A user enables it by including it from the active script:

require ["include"];
include :global "file-lists";