From 9fd00bd6ad1ea7e1bd969141c4bfb582e83717c4 Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Sat, 13 May 2023 13:59:16 +0200 Subject: [PATCH] Rspamd: adjust learning of ham (#3334) * adjust learning of ham See #3333 When moving a mail from the Junk folder to the Trash folder, the mail previously classified as ham due to the wildcard match of `*`. Because the syntax does not allow for negation, we can only change the behavior in a way that mails are learned as ham when they are moved into `INBOX` from `Junk`. This is reasonable though. * adjust tests accordingly * adjust docs accordingly --- docs/content/config/environment.md | 4 +- .../startup/setup.d/security/rspamd.sh | 6 +-- .../rspamd_imap_move_to_inbox.txt | 4 +- .../nc_templates/rspamd_imap_move_to_junk.txt | 4 +- .../parallel/set1/spam_virus/rspamd_full.bats | 46 ++++++++++--------- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/docs/content/config/environment.md b/docs/content/config/environment.md index 5123fe57..3d204cce 100644 --- a/docs/content/config/environment.md +++ b/docs/content/config/environment.md @@ -353,7 +353,9 @@ Controls whether the [Rspamd Greylisting module][rspamd-greylisting-module] is e When enabled, 1. the "[autolearning][rspamd-autolearn]" feature is turned on; -2. the Bayes classifier will be trained when moving mails from or to the Junk folder (with the help of Sieve scripts). +2. the Bayes classifier will be trained (with the help of Sieve scripts) when moving mails + 1. from anywhere to the `Junk` folder (learning this email as spam); + 2. from the `Junk` folder into the `INBOX` (learning this email as ham). !!! warning "Attention" diff --git a/target/scripts/startup/setup.d/security/rspamd.sh b/target/scripts/startup/setup.d/security/rspamd.sh index 429a8efb..c1c2712a 100644 --- a/target/scripts/startup/setup.d/security/rspamd.sh +++ b/target/scripts/startup/setup.d/security/rspamd.sh @@ -222,13 +222,13 @@ function __rspamd__setup_learning sedfile -i -E '/^}/d' /etc/dovecot/conf.d/90-sieve.conf cat >>/etc/dovecot/conf.d/90-sieve.conf << EOF - # From elsewhere to Junk folder + # From anyhwere to Junk imapsieve_mailbox1_name = Junk imapsieve_mailbox1_causes = COPY imapsieve_mailbox1_before = file:${SIEVE_PIPE_BIN_DIR}/learn-spam.sieve - # From Junk folder to elsewhere - imapsieve_mailbox2_name = * + # From Junk to Inbox + imapsieve_mailbox2_name = INBOX imapsieve_mailbox2_from = Junk imapsieve_mailbox2_causes = COPY imapsieve_mailbox2_before = file:${SIEVE_PIPE_BIN_DIR}/learn-ham.sieve diff --git a/test/test-files/nc_templates/rspamd_imap_move_to_inbox.txt b/test/test-files/nc_templates/rspamd_imap_move_to_inbox.txt index a3f6dc13..b9191d62 100644 --- a/test/test-files/nc_templates/rspamd_imap_move_to_inbox.txt +++ b/test/test-files/nc_templates/rspamd_imap_move_to_inbox.txt @@ -1,4 +1,4 @@ A LOGIN user1@localhost.localdomain 123 B SELECT Junk -A UID MOVE 1:1 INBOX -A4 LOGOUT +A MOVE 1:1 INBOX +A LOGOUT diff --git a/test/test-files/nc_templates/rspamd_imap_move_to_junk.txt b/test/test-files/nc_templates/rspamd_imap_move_to_junk.txt index 0039d106..1e38081c 100644 --- a/test/test-files/nc_templates/rspamd_imap_move_to_junk.txt +++ b/test/test-files/nc_templates/rspamd_imap_move_to_junk.txt @@ -1,4 +1,4 @@ A LOGIN user1@localhost.localdomain 123 B SELECT INBOX -A UID MOVE 1:1 Junk -A4 LOGOUT +A MOVE 1:1 Junk +A LOGOUT diff --git a/test/tests/parallel/set1/spam_virus/rspamd_full.bats b/test/tests/parallel/set1/spam_virus/rspamd_full.bats index 611995c6..eb5de312 100644 --- a/test/tests/parallel/set1/spam_virus/rspamd_full.bats +++ b/test/tests/parallel/set1/spam_virus/rspamd_full.bats @@ -237,14 +237,8 @@ function teardown_file() { _default_teardown ; } _run_in_container grep -F 'sieve_pipe_bin_dir = /usr/lib/dovecot/sieve-pipe' "${SIEVE_CONFIG_FILE}" assert_success - # Move an email to the "Junk" folder from "INBOX"; the first email we - # sent should pass fine, hence we can now move it - _send_email 'nc_templates/rspamd_imap_move_to_junk' '0.0.0.0 143' - sleep 1 # wait for the transaction to finish - - local MOVE_TO_JUNK_LINES=( + local LEARN_SPAM_LINES=( 'imapsieve: mailbox Junk: MOVE event' - 'imapsieve: Matched static mailbox rule [1]' "sieve: file storage: script: Opened script \`learn-spam'" 'sieve: file storage: Using Sieve script path: /usr/lib/dovecot/sieve-pipe/learn-spam.sieve' "sieve: Executing script from \`/usr/lib/dovecot/sieve-pipe/learn-spam.svbin'" @@ -254,20 +248,7 @@ function teardown_file() { _default_teardown ; } "left message in mailbox 'Junk'" ) - _run_in_container cat /var/log/mail/mail.log - assert_success - for LINE in "${MOVE_TO_JUNK_LINES[@]}" - do - assert_output --partial "${LINE}" - done - - # Move an email to the "INBOX" folder from "Junk"; there should be two mails - # in the "Junk" folder - _send_email 'nc_templates/rspamd_imap_move_to_inbox' '0.0.0.0 143' - sleep 1 # wait for the transaction to finish - - local MOVE_TO_JUNK_LINES=( - 'imapsieve: Matched static mailbox rule [2]' + local LEARN_HAM_LINES=( "sieve: file storage: script: Opened script \`learn-ham'" 'sieve: file storage: Using Sieve script path: /usr/lib/dovecot/sieve-pipe/learn-ham.sieve' "sieve: Executing script from \`/usr/lib/dovecot/sieve-pipe/learn-ham.svbin'" @@ -275,9 +256,30 @@ function teardown_file() { _default_teardown ; } "left message in mailbox 'INBOX'" ) + # Move an email to the "Junk" folder from "INBOX"; the first email we + # sent should pass fine, hence we can now move it. + _send_email 'nc_templates/rspamd_imap_move_to_junk' '0.0.0.0 143' + sleep 1 # wait for the transaction to finish + _run_in_container cat /var/log/mail/mail.log assert_success - for LINE in "${MOVE_TO_JUNK_LINES[@]}" + assert_output --partial 'imapsieve: Matched static mailbox rule [1]' + refute_output --partial 'imapsieve: Matched static mailbox rule [2]' + for LINE in "${LEARN_SPAM_LINES[@]}" + do + assert_output --partial "${LINE}" + done + + # Move an email to the "INBOX" folder from "Junk"; there should be two mails + # in the "Junk" folder, since the second email we sent during setup should + # have landed in the Junk folder already. + _send_email 'nc_templates/rspamd_imap_move_to_inbox' '0.0.0.0 143' + sleep 1 # wait for the transaction to finish + + _run_in_container cat /var/log/mail/mail.log + assert_success + assert_output --partial 'imapsieve: Matched static mailbox rule [2]' + for LINE in "${LEARN_HAM_LINES[@]}" do assert_output --partial "${LINE}" done