replace deleted the replaced part's children by shifting the message's
part list, but left every other reference to part numbers as it was:
the child lists of the other multiparts and the parts a foreverypart
loop had still to visit. So replace inside foreverypart, which is how
RFC 5703 strips attachments, could make a later step index past the
end of the list, and enclose inside foreverypart left the loop walking
parts that no longer existed. Either panicked the delivery task, which
never released its slot in the local delivery queue and left the
message locked. Any user could do it with their own script and a
message to themselves, and any outside sender could do it to an account
that already strips attachments that way. Twenty-five such messages
stopped local delivery for every account on the node until restart,
and again after it.
Now replace renumbers the parts that are left (child lists, the body
and attachment lists, and every foreverypart loop and the part it
returns to), so the loop goes on over the parts still there, and enclose
ends any foreverypart loop it runs in, since those parts are now inside
the enclosed message. Headers are only added to or removed from a part
that exists, and the message size can no longer wrap below zero.
Found in a security review.
Test: in crates/common, an RFC 5703-style script replacing *.exe
attachments inside foreverypart panicked on a message with two nested
multipart attachments ("removal index (is 6) should be < len (is 6)").
Now both attachments are replaced and the rest kept, and foreverypart
with replace on every part, and with enclose then addheader, run to the
end.
sieve
sieve is a fast and secure Sieve filter interpreter for Rust that supports all registered Sieve extensions.
Usage Example
use sieve::{runtime::RuntimeError, Action, Compiler, Event, Input, Runtime};
// Sieve script to execute
let text_script = br#"
require ["fileinto", "body", "imap4flags"];
if body :contains "tps" {
setflag "$tps_reports";
}
if header :matches "List-ID" "*<*@*" {
fileinto "INBOX.lists.${2}"; stop;
}
"#;
// Message to filter
let raw_message = r#"From: Sales Mailing List <[email protected]>
To: John Doe <[email protected]>
List-ID: <[email protected]>
Subject: TPS Reports
We're putting new coversheets on all the TPS reports before they go out now.
So if you could go ahead and try to remember to do that from now on, that'd be great. All right!
"#;
// Compile
let compiler = Compiler::new();
let script = compiler.compile(text_script).unwrap();
// Build runtime
let runtime = Runtime::new();
// Create filter instance
let mut instance = runtime.filter(raw_message.as_bytes());
let mut input = Input::script("my-script", script);
let mut messages: Vec<String> = Vec::new();
// Start event loop
while let Some(result) = instance.run(input) {
match result {
Ok(event) => match event {
Event::IncludeScript { name, optional } => {
// NOTE: Just for demonstration purposes, script name needs to be validated first.
if let Ok(bytes) = std::fs::read(name.as_str()) {
let script = compiler.compile(&bytes).unwrap();
input = Input::script(name, script);
} else if optional {
input = Input::False;
} else {
panic!("Script {} not found.", name);
}
}
Event::MailboxExists { .. } => {
// Set to true if the mailbox exists
input = false.into();
}
Event::ListContains { .. } => {
// Set to true if the list(s) contains an entry
input = false.into();
}
Event::DuplicateId { .. } => {
// Set to true if the ID is duplicate
input = false.into();
}
Event::Execute { command, arguments } => {
println!(
"Script executed command {:?} with parameters {:?}",
command, arguments
);
// Set to true if the script succeeded
input = false.into();
}
Event::Keep { flags, message_id } => {
println!(
"Keep message '{}' with flags {:?}.",
if message_id > 0 {
messages[message_id - 1].as_str()
} else {
raw_message
},
flags
);
input = true.into();
}
Event::Discard => {
println!("Discard message.");
input = true.into();
}
Event::Reject { reason, .. } => {
println!("Reject message with reason {:?}.", reason);
input = true.into();
}
Event::FileInto {
folder,
flags,
message_id,
..
} => {
println!(
"File message '{}' in folder {:?} with flags {:?}.",
if message_id > 0 {
messages[message_id - 1].as_str()
} else {
raw_message
},
folder,
flags
);
input = true.into();
}
Event::SendMessage {
recipient,
message_id,
..
} => {
println!(
"Send message '{}' to {:?}.",
if message_id > 0 {
messages[message_id - 1].as_str()
} else {
raw_message
},
recipient
);
input = true.into();
}
Event::Notify {
message, method, ..
} => {
println!("Notify URI {:?} with message {:?}", method, message);
input = true.into();
}
Event::CreatedMessage { message, .. } => {
messages.push(String::from_utf8(message).unwrap());
input = true.into();
}
#[cfg(test)]
_ => unreachable!(),
},
Err(error) => {
match error {
RuntimeError::TooManyIncludes => {
eprintln!("Too many included scripts.");
}
RuntimeError::InvalidInstruction(instruction) => {
eprintln!(
"Invalid instruction {:?} found at {}:{}.",
instruction.name(),
instruction.line_num(),
instruction.line_pos()
);
}
RuntimeError::ScriptErrorMessage(message) => {
eprintln!("Script called the 'error' function with {:?}", message);
}
RuntimeError::CapabilityNotAllowed(capability) => {
eprintln!(
"Capability {:?} has been disabled by the administrator.",
capability
);
}
RuntimeError::CapabilityNotSupported(capability) => {
eprintln!("Capability {:?} not supported.", capability);
}
RuntimeError::CPULimitReached => {
eprintln!("Script exceeded the configured CPU limit.");
}
}
input = true.into();
}
}
}
Testing & Fuzzing
To run the testsuite:
$ cargo test --all-features
To fuzz the library with cargo-fuzz:
$ cargo +nightly fuzz run sieve
Conformed RFCs
- RFC 5228 - Sieve: An Email Filtering Language
- RFC 3894 - Copying Without Side Effects
- RFC 5173 - Body Extension
- RFC 5183 - Environment Extension
- RFC 5229 - Variables Extension
- RFC 5230 - Vacation Extension
- RFC 5231 - Relational Extension
- RFC 5232 - Imap4flags Extension
- RFC 5233 - Subaddress Extension
- RFC 5235 - Spamtest and Virustest Extensions
- RFC 5260 - Date and Index Extensions
- RFC 5293 - Editheader Extension
- RFC 5429 - Reject and Extended Reject Extensions
- RFC 5435 - Extension for Notifications
- RFC 5463 - Ihave Extension
- RFC 5490 - Extensions for Checking Mailbox Status and Accessing Mailbox Metadata
- RFC 5703 - MIME Part Tests, Iteration, Extraction, Replacement, and Enclosure
- RFC 6009 - Delivery Status Notifications and Deliver-By Extensions
- RFC 6131 - Sieve Vacation Extension: "Seconds" Parameter
- RFC 6134 - Externally Stored Lists
- RFC 6558 - Converting Messages before Delivery
- RFC 6609 - Include Extension
- RFC 7352 - Detecting Duplicate Deliveries
- RFC 8579 - Delivering to Special-Use Mailboxes
- RFC 8580 - File Carbon Copy (FCC)
- RFC 9042 - Delivery by MAILBOXID
- REGEX-01 - Regular Expression Extension (draft-ietf-sieve-regex-01)
License
Licensed under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
You can be released from the requirements of the AGPLv3 license by purchasing a commercial license. Please contact [email protected] for more details.
Copyright
Copyright (C) 2020, Stalwart Labs LLC