Skip to content

Better default value for SA_KILL variable#3058

Merged
casperklein merged 4 commits intomasterfrom
casperklein-patch-1
Feb 7, 2023
Merged

Better default value for SA_KILL variable#3058
casperklein merged 4 commits intomasterfrom
casperklein-patch-1

Conversation

@casperklein
Copy link
Copy Markdown
Member

@casperklein casperklein commented Feb 3, 2023

Description

Fixes #3028 (comment)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (non-breaking change that does improve existing functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (README.md or the documentation under docs/)
  • If necessary I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@casperklein casperklein self-assigned this Feb 3, 2023
@casperklein casperklein added kind/improvement Improve an existing feature, configuration file or the documentation service/security/spamassassin labels Feb 3, 2023
@casperklein casperklein added this to the v12.0.0 milestone Feb 3, 2023
georglauterbach
georglauterbach previously approved these changes Feb 3, 2023
@polarathene
Copy link
Copy Markdown
Member

polarathene commented Feb 4, 2023

EDIT: This should already provide the desired behaviour by default... what are we actually fixing with this then?

VARS[MOVE_SPAM_TO_JUNK]="${MOVE_SPAM_TO_JUNK:=1}"

VARS[SPAMASSASSIN_SPAM_TO_INBOX]="${SPAMASSASSIN_SPAM_TO_INBOX:=1}"


This is a no from me 👎

I have documented why in the associated issue. Basically SA_KILL is a threshold to trigger the Amavis action (we default to D_BOUNCE), and simply bumping this value to an excessively high default to avoid mail being bounced seems redundant?

We have other ENV to support the alternative behaviour this PR is attempting to resolve, it would be better to adjust the defaults instead.


ENV and values were contributed by the original DMS author in Feb 2016, with minimal context. They are the default upstream values (and the ENV docs are taken directly from comments there too).

Debian Bullseye image with Amavis package defaults (no DMS involved): `/etc/amavis/conf.d/20-debian_defaults`:
$sa_tag_level_deflt  = 2.0;  # add spam info headers if at, or above that level
$sa_tag2_level_deflt = 6.31; # add 'spam detected' headers at that level
$sa_kill_level_deflt = 6.31; # triggers spam evasive actions
$final_spam_destiny       = D_PASS;    
$final_bad_header_destiny = D_PASS;     # False-positive prone (for spam)

We can see that we're currently just leaving these ENV to their respective defaults upstream and expecting users to change them if they're not happy with that. That probably isn't an issue, except that we have D_BOUNCE instead of the default D_PASS.


As detailed below, SA_KILL being at the same value as SA_TAG2 is just to say always perform the configured action when mail is marked as spam.

If you needed some flexibility to conditionally treat some spam differently, such as allowing spam to arrive into the junk folder, but discarding/bouncing mail with a very high spam score (differentiating between maybe spam and definitely spam), this could be a reason to go with a higher SA_KILL and the D_BOUNCE (or other supported actions).

Resources for references:

Here is an article that looks into tweaking the values a bit for what works better for them. I don't know about those values or relevance now, but it explains the config nicely with some feedback on their experience (some mail still not being detected as spam, some getting treated as spam):

There is a certain minimum score for obvious spam messages. This value can be used for $sa_tag2_level_deflt. (Note that this has no effect on the number of messages being blocked. It just indicates that anything below this limit is considered non-spam.)
Anything below 0.5 is considered clean.
Most major players (Gmail, Hotmail, Mailchimp etc.) have excellent scores (far far below zero) and therefore these will always be delivered. Some spam messages have scores just above or even below zero and will also pass the filter.

The problem area is the twilight zone between 0.5 and 2.0.
For some reason too many legit messages look kind of spammy (no subject line, only images or videos, all caps, misconfigured server, …) and there is no foolproof way for a non-human to distinguish them from the spam messages that succeed in hiding their spammy intentions.
By narrowing down this range you will receive less spam, but potentially miss certain legit messages. Using trial and error will give result in values that are acceptable for you.

More technical reference on the config, that also seemed helpful:

When SpamAssassin processes message, it gives them a score identifying what is the probability of message being a spam.
$sa_tag_level_deflt tells amavis that if the score is greater or equal to this level that amavis should append X-Spam headers to the message.
The score of -999 means that we want to apply X-Spam header to all of our messages, so it is easier for us to see why is, or why is not, a message considered spam by examining the headers of the message.

$sa_tag2_level_deflt = 5 is the spam level the message needs to reach for amavis to apply the header X-Spam-Flag: YES so we (our mail client) knows that this message is considered to be spam. At this level the message subject is prefixed with the value of the variable $sa_spam_subject_tag.

$sa_kill_level_deflt holds the value of the spam level that message needs to reach to do something with the message. What we are going to do with messages that are spam depends of the value of $final_spam_destiny variable.
In this case we are discarding this messages.
This means that the message is not going to reach to recipients mailbox at all. But that does not mean that the message is lost. Spam messages that are discarded can still be fetched from the system. Default configuration is to store them in the /var/lib/amavis/virusmails folder.

@georglauterbach
Copy link
Copy Markdown
Member

Oh, I misunderstood this too then. Thanks for the clarification! I'd not merge this as is then too, but maybe this PR can bring an enhanced documentation?

@casperklein
Copy link
Copy Markdown
Member Author

  • MOVE_SPAM_TO_JUNK - This is only for IMAP setups, not for POP3.
  • SPAM_TO_INBOX disables bouncing of SPAM messages completely

Some might set this both to 0.

SA_TAG2 defines when to add spam headers, e.g. changing the subject to *** SPAM *** and SA_KILL defines at what level to bounce the mail.

We can see that we're currently just leaving these ENV to their respective defaults upstream and expecting users to change them if they're not happy with that. That probably isn't an issue, except that we have D_BOUNCE instead of the default D_PASS.

Because we changed it to D_BOUNCE, it's a an issue, or at least not sane:

Setting SA_TAG2 == SA_KILL leads to never receiving an email with *** SPAM *** subject?

SA_KILL=10000.0 might be the wrong approach. As you pointed out, this will basically prevent any mail from bouncing.

But SA_KILL should be higher than SA_TAG2. What do you think?

@polarathene
Copy link
Copy Markdown
Member

polarathene commented Feb 4, 2023

Because we changed it to D_BOUNCE, it's a an issue, or at least not sane:

EDIT: This was my bad, as shown with snippet at the end of this comment, it should still be D_PASS by default, and only change to D_BOUNCE when explicitly opt-ing out of delivering spam.


  • MOVE_SPAM_TO_JUNK - This is only for IMAP setups, not for POP3.

Is it? I thought Dovecot sieve scripts were for handling any mail arriving into the inbox, protocol agnostic?

Mail that is delivered to DMS goes through SA and Amavis, which would add the spam header to trigger the sieve script would it not?:

require "fileinto";
if header :contains "X-Spam-Flag" "YES" {
fileinto "Junk";
}

Then IMAP and POP3 are for mail clients to retrieve mail from Dovecot after the fact? Why would POP3 be excluded from the feature?

Setting SA_TAG2 == SA_KILL leads to never receiving an email with *** SPAM *** subject?

That's what I understand. I am not familiar with spam scores as I don't actually run DMS (all this time and I still just contribute to a project I don't use 😂 ), so I don't know what a good value would be for SA_KILL. Based on the blog I referenced, they had SA_KILL at 2.0, but it wasn't clear what score is 99% likely to be actual spam 🤷‍♂️

But SA_KILL should be higher than SA_TAG2. What do you think?

I am not sure. In what scenario do you want to mark mail as spam, but not handle it differently? (I'm not sure if the action D_PASS actually treats the mail differently, maybe metadata or logs? 🤷‍♂️ )

If there was a reasonable "definitely spam" score that SA_KILL could use for D_BOUNCE perhaps that'd make sense if 6.31 is still prone to a fair amount of false-positives.

SA_KILL=10000.0 might be the wrong approach. As you pointed out, this will basically prevent any mail from bouncing.

Honestly considering what this would do, it just sounds like the smarter choice is to default to D_PASS instead of D_BOUNCE. (**EDIT: which I forgot we already do, I just forgot to update the part of my message that you quoted 😅 )

if [[ ${SPAMASSASSIN_SPAM_TO_INBOX} -eq 1 ]]
then
_log 'trace' 'Configuring Spamassassin/Amavis to send SPAM to inbox'
sed -i "s|\$final_spam_destiny.*=.*$|\$final_spam_destiny = D_PASS;|g" /etc/amavis/conf.d/49-docker-mailserver
sed -i "s|\$final_bad_header_destiny.*=.*$|\$final_bad_header_destiny = D_PASS;|g" /etc/amavis/conf.d/49-docker-mailserver
else
_log 'trace' 'Configuring Spamassassin/Amavis to bounce SPAM'
sed -i "s|\$final_spam_destiny.*=.*$|\$final_spam_destiny = D_BOUNCE;|g" /etc/amavis/conf.d/49-docker-mailserver
sed -i "s|\$final_bad_header_destiny.*=.*$|\$final_bad_header_destiny = D_BOUNCE;|g" /etc/amavis/conf.d/49-docker-mailserver
fi

Ok so if bouncing mail is the problem when that ENV is flipped, we could also use D_DISCARD (I think that's the correct one), which my other reference link said would not deliver the mail, but still keep it stored locally in quarantine.

Alternatively, we better document that disabling SPAMASSASSIN_SPAM_TO_INBOX will bounce any mail marked as spam? And that if that is too aggressive, they can raise the SA_KILL value to increase the threshold of when spam is bounced? (allowing some mail marked as spam through still if SA_TAG2 is lower)

@casperklein
Copy link
Copy Markdown
Member Author

MOVE_SPAM_TO_JUNK - This is only for IMAP setups, not for POP3.
Is it? I thought Dovecot sieve scripts were for handling any mail arriving into the inbox, protocol agnostic?

That's my assumption, or at least part of it, because POP3 does not now the concept of folders. So moving SPAM to junk folder has no effect. In POP3 this have to be configured client side in the MUA as a rule (not in scope of DMS).

You are right, that Sieve processing also apply to POP3, but moving a mail to junk folder in particular will have no effect (from POP3 view).


SPAM_TO_INBOX disables rejecting emails completely. The use case I thought of was:

SPAM_TO_INBOX=0
SA_TAG2=6
SA_KILL=10

This allows to tag mails as SPAM beginning with a score of 6 and rejecting mails with a score of >=10.

SA_KILL=10000.0 is bad I agree, because it disables rejecting mails. But SA_KILL should be higher than SA_TAG2 IMO.

@polarathene
Copy link
Copy Markdown
Member

polarathene commented Feb 7, 2023

This seems acceptable 👍 Although I prefer there to be an actual demand to warrant the change 🤔

Since it only affects the users explicitly opt-ing in to bouncing mail marked as spam, this change may result in receiving unwanted spam in their inbox. Improving docs probably makes more sense.


That's my assumption, or at least part of it, because POP3 does not now the concept of folders. So moving SPAM to junk folder has no effect.
In POP3 this have to be configured client side in the MUA as a rule (not in scope of DMS).

I thought that this is about what happens when mail arrives to Postfix, it gets filtered through Amavis + SA, then sent to Dovecot for mail storage. The MUA then can access that mailbox via IMAP or POP3.

It should still be in Dovecot's junk folder for that mailbox, and marked as spam in the mail headers?

You are right, that Sieve processing also apply to POP3, but moving a mail to junk folder in particular will have no effect (from POP3 view).

My understanding is that sieve is on Dovecot's end with how it manages mail storage. Agnostic from MUA access regardless of protocol.


This allows to tag mails as SPAM beginning with a score of 6 and rejecting mails with a score of >=10.

SA_KILL=10000.0 is bad I agree, because it disables rejecting mails. But SA_KILL should be higher than SA_TAG2 IMO.

SA_KILL=10 seems okay-ish from a quick look at non-standard values used elsewhere:

Ok, so for the defaults, this makes no difference right? Due to D_PASS, the mail still gets tagged as spam (SA_TAG2) at the same score, and thus the sieve will move to junk.

SA_KILL only has relevance for our alternative action D_BOUNCE, those users will potentially get more spam delivered instead of rejected (and we don't have any major complaints AFAIK of users opting in to this feature about legitimate mail being rejected).

I suppose with release notes, if there is more spam being delivered when they'd rather it bounced, they can manually change SA_KILL or raise an issue about it 🤷‍♂️ Likewise, no reason to lower SA_TAG2... so sounds good 👍

Amavis config observations (not too important)

EDIT: I have noticed in the relevant Amavis docs section:

kill level
if spam score is at or above kill level, mail is blocked; and sender receives a nondelivery notification unless spam score exceeds dsn cutoff level.

/etc/amavis/conf.d/20-debian_defaults:

$sa_dsn_cutoff_level = 10;   # spam level beyond which a DSN is not sent

I thought I had seen something like a percentage somewhere, and was going to raise this as a concern with increasing the SA_KILL value, but I'm mistaken 😅 (I believe it was when I saw sa_quarantine_cutoff_level = 25 in ArchWiki config snippet)

That config file has some other info that might be worth noting:

# You should:
#   Use D_DISCARD to discard data (viruses)
#   Use D_BOUNCE to generate local bounces by amavisd-new
#   Use D_REJECT to generate local or remote bounces by the calling MTA
#   Use D_PASS to deliver the message
#
# Whatever you do, *NEVER* use D_REJECT if you have other MTAs *forwarding*
# mail to your account.  Use D_BOUNCE instead, otherwise you are delegating
# the bounce work to your friendly forwarders, which might not like it at all.
#
# On dual-MTA setups, one can often D_REJECT, as this just makes your own
# MTA generate the bounce message.  Test it first.
#
# Bouncing viruses is stupid, always discard them after you are sure the AV
# is working correctly.  Bouncing real SPAM is also useless, if you cannot
# D_REJECT it (and don't D_REJECT mail coming from your forwarders!).

$final_virus_destiny      = D_DISCARD;  # (data not lost, see virus quarantine)
$final_banned_destiny     = D_DISCARD;  
$final_spam_destiny       = D_PASS;    
$final_bad_header_destiny = D_PASS;     # False-positive prone (for spam)

Related issue history

For better traceability via referencing existing issues / PRs

@casperklein casperklein enabled auto-merge (squash) February 7, 2023 19:25
@casperklein casperklein merged commit e7790ce into master Feb 7, 2023
@casperklein casperklein deleted the casperklein-patch-1 branch February 7, 2023 19:26
@polarathene
Copy link
Copy Markdown
Member

I noticed that this only changed a default in the optional .env file. Docs and variables.sh were not updated 😅

Due to #3112 PR wanting to move variables.sh, may be best to wait until after that is sorted out though.

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

Labels

kind/improvement Improve an existing feature, configuration file or the documentation service/security/spamassassin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to disable SA_SPAM_SUBJECT?

3 participants