Files
inbuxa-server/vendor/sieve-rs/src/runtime/tests/test_address.rs
T
jcoffey-dev cc6f1eb298
ci / fork-checks (pull_request) Successful in 16s
ci / build (pull_request) Successful in 7m53s
Rename the identifiers that carried the upstream name
Everything clients, users and operators meet now carries the fork's name,
with no aliases (SPEC.md §2.4, changed here from "protocol identifiers
stay"):

- JMAP: upstream's registry capability is urn:inbuxa:jmap:registry, beside
  the fork's own urn:inbuxa:jmap.
- WebDAV lock and sync tokens are urn:inbuxa:dav*; clients resync once.
- Sieve: vnd.inbuxa.while and vnd.inbuxa.expressions. sieve-rs spells these
  into its compiler, so it's vendored (vendor/sieve-rs, 0.7.3) and patched in;
  a unit test fails if Cargo.lock ever moves past the vendored copy. The
  trusted runtime now names itself too, rather than answering sieve-rs's
  default.
- The web interface's OAuth client is inbuxa-webui. On every start the old
  stalwart-webui client is removed and any application naming it is moved
  over.
- The spam filter's blobs are INBUXA_SPAM_*; every start moves any left
  under the old keys, so a trained model survives.
- SQL stores and log files default to inbuxa, in the code and in the
  schema served to the admin (checksum regenerated).
- Settings are INBUXA_* only. A STALWART_* variable that's set where its
  INBUXA_* one isn't stops the server at startup, naming it.
- The version-upgrade messages link docs.inbuxa.org's migration page, and
  the OpenAPI description, smtp crate metadata and web-push test fixtures
  lose the name.

Kept on purpose, allowlisted with reasons: the OAuth key-derivation
contexts (renaming them would end every session and invalidate every
sealed client id) and the hashed application prefix.

Also fixes a latent start-up failure: ensure_client updated an existing
first-party client with a revision of 0, which the registry's assertion
never matches, so adding a redirect URI or changing the webmail secret
failed start-up. And the principal session test now expects
legacyProtocols (C-1, added 2026-09-21), which it had missed.

Tested: the server builds without warnings; common's 106 unit tests,
including the vendoring check; a new integration test for the two
start-up migrations; and the webdav, jmap, imap and SMTP Sieve suites.
2026-09-22 19:33:02 -07:00

297 lines
11 KiB
Rust

/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
*/
use mail_parser::{
Addr, Address, Header, HeaderValue,
parsers::{
MessageStream,
fields::address::{
parse_address_detail_part, parse_address_domain, parse_address_local_part,
parse_address_user_part,
},
},
};
use crate::{
Context, Event,
compiler::{
Number,
grammar::{AddressPart, MatchType, tests::test_address::TestAddress},
},
};
use super::TestResult;
impl TestAddress {
pub(crate) fn exec(&self, ctx: &mut Context) -> TestResult {
let key_list = ctx.eval_values(&self.key_list);
let header_list = ctx.parse_header_names(&self.header_list);
let result = match &self.match_type {
MatchType::Is | MatchType::Contains => {
let is_is = matches!(&self.match_type, MatchType::Is);
ctx.find_headers(
&header_list,
self.index,
self.mime_anychild,
|header, _, _| {
ctx.find_addresses(header, &self.address_part, |value| {
for key in &key_list {
if is_is {
if self.comparator.is(&value, key) {
return true;
}
} else if self.comparator.contains(value, key.to_string().as_ref())
{
return true;
}
}
false
})
},
)
}
MatchType::Value(rel_match) => ctx.find_headers(
&header_list,
self.index,
self.mime_anychild,
|header, _, _| {
ctx.find_addresses(header, &self.address_part, |value| {
for key in &key_list {
if self.comparator.relational(rel_match, &value, key) {
return true;
}
}
false
})
},
),
MatchType::Matches(capture_positions) | MatchType::Regex(capture_positions) => {
let mut captured_positions = Vec::new();
let is_matches = matches!(&self.match_type, MatchType::Matches(_));
let result = ctx.find_headers(
&header_list,
self.index,
self.mime_anychild,
|header, _, _| {
ctx.find_addresses(header, &self.address_part, |value| {
for (pattern_expr, pattern) in key_list.iter().zip(self.key_list.iter())
{
if is_matches {
if self.comparator.matches(
value,
pattern_expr.to_string().as_ref(),
*capture_positions,
&mut captured_positions,
) {
return true;
}
} else if self.comparator.regex(
pattern,
pattern_expr,
value,
*capture_positions,
&mut captured_positions,
) {
return true;
}
}
false
})
},
);
if !captured_positions.is_empty() {
ctx.set_match_variables(captured_positions);
}
result
}
MatchType::Count(rel_match) => {
let mut count: i64 = 0;
ctx.find_headers(
&header_list,
self.index,
self.mime_anychild,
|header, _, _| {
ctx.find_addresses(header, &self.address_part, |value| {
if !value.is_empty() {
count += 1;
}
false
})
},
);
let mut result = false;
for key in &key_list {
if rel_match.cmp(&Number::from(count), &key.to_number()) {
result = true;
break;
}
}
result
}
MatchType::List => {
let mut values: Vec<String> = Vec::new();
ctx.find_headers(
&header_list,
self.index,
self.mime_anychild,
|header, _, _| {
ctx.find_addresses(header, &self.address_part, |value| {
if !value.is_empty() && !values.iter().any(|v| v.eq(value)) {
values.push(value.to_string());
}
false
})
},
);
if !values.is_empty() {
return TestResult::Event {
event: Event::ListContains {
lists: ctx.eval_values_owned(&self.key_list),
values,
match_as: self.comparator.as_match(),
},
is_not: self.is_not,
};
}
false
}
};
TestResult::Bool(result ^ self.is_not)
}
}
impl Context<'_> {
#[allow(unused_assignments)]
pub(crate) fn find_addresses(
&self,
header: &Header,
part: &AddressPart,
mut visitor_fnc: impl FnMut(&str) -> bool,
) -> bool {
match &header.value {
HeaderValue::Address(Address::List(addr_list)) => {
for addr in addr_list {
if let Some(addr) = part.eval(addr)
&& visitor_fnc(addr)
{
return true;
}
}
false
}
HeaderValue::Address(Address::Group(group_list)) => {
for group in group_list {
for addr in &group.addresses {
if let Some(addr) = part.eval(addr)
&& visitor_fnc(addr)
{
return true;
}
}
}
false
}
_ => {
let mut raw_header = None;
let bytes = if header.offset_end > 0 {
self.message
.raw_message
.get(header.offset_start as usize..header.offset_end as usize)
.unwrap_or(b"")
} else if let HeaderValue::Text(text) = &header.value {
// Inserted header
raw_header = format!("{text}\n").into_bytes().into();
raw_header.as_deref().unwrap()
} else {
b""
};
match MessageStream::new(bytes).parse_address() {
HeaderValue::Address(Address::List(addr_list)) => {
for addr in &addr_list {
if let Some(addr) = part.eval(addr)
&& visitor_fnc(addr)
{
return true;
}
}
false
}
HeaderValue::Address(Address::Group(group_list)) => {
for group in group_list {
for addr in &group.addresses {
if let Some(addr) = part.eval(addr)
&& visitor_fnc(addr)
{
return true;
}
}
}
false
}
_ => visitor_fnc(""),
}
}
}
}
}
impl AddressPart {
pub(crate) fn eval<'x>(&self, addr: &'x Addr<'x>) -> Option<&'x str> {
let email = addr.address.as_deref().or(addr.name.as_deref());
match (self, email) {
(AddressPart::All, _) => email,
(AddressPart::LocalPart, Some(email)) if !email.is_empty() => {
parse_address_local_part(email)
}
(AddressPart::Domain, Some(email)) if !email.is_empty() => parse_address_domain(email),
(AddressPart::User, Some(email)) if !email.is_empty() => parse_address_user_part(email),
(AddressPart::Detail, Some(email)) if !email.is_empty() => {
parse_address_detail_part(email)
}
(AddressPart::Name, _) => addr.name.as_deref(),
_ => email,
}
}
pub(crate) fn eval_strict<'x>(&self, addr: &'x Addr<'x>) -> Option<&'x str> {
match (self, addr.address.as_deref()) {
(AddressPart::All, Some(email)) => Some(email),
(AddressPart::LocalPart, Some(email)) if !email.is_empty() => {
parse_address_local_part(email)
}
(AddressPart::Domain, Some(email)) if !email.is_empty() => parse_address_domain(email),
(AddressPart::User, Some(email)) if !email.is_empty() => parse_address_user_part(email),
(AddressPart::Detail, Some(email)) if !email.is_empty() => {
parse_address_detail_part(email)
}
(AddressPart::Name, _) => addr.name.as_deref(),
(_, email) => email,
}
}
pub(crate) fn eval_string<'x>(&self, addr: &'x str) -> Option<&'x str> {
if !addr.is_empty() {
match self {
AddressPart::All => addr.into(),
AddressPart::LocalPart => parse_address_local_part(addr),
AddressPart::Domain => parse_address_domain(addr),
AddressPart::User => parse_address_user_part(addr),
AddressPart::Detail => parse_address_detail_part(addr),
_ => addr.into(),
}
} else {
addr.into()
}
}
}