Allow X-Spam-Status and X-Spam-Score header queries in user scripts?

Your question

The X-Spam-Status and X-Spam-Score header values are not available for querying in user scripts, because their values are kept in some internal buffer and only appended to the final message, right?

Wouldn’t it be possible to make those headers fully queryable at the user script stage? “spamtest” is absolutely not an option!

  1. did’t find any webmailer supporting spamtest in the ui based filter editor, which makes using it impossible for non “sieve” experts. Or would you ask your mother to write a fully fledged sieve script to filter out spam into a separate folder? :wink:
  2. spamtest is supposed to return values from 1 to 10, right? … but I don’t see any I can use to match “X-Spam-Status: Yes” or “X-Spam-Status: No”. I expected to only get 1 (ham) or 10 (spam) here. But this is not the case: got 6 on a spam score of 9.1 … I can’t see how this is connected or not to the spam score and limit set in Stalwart. May be further explanation is appropriate :wink:

Please make plain filtering on X-Spam-Status and X-Spam-Score headers possible, so user can build their on sieve scripts using available editors in webmailers. Please.

I understand that topics in this category are triaged by a bot first but a human reply will follow up. If I’d prefer a human-only reply, I’ll add the no-ai tag to my topic.

on

The X-Spam-Status header is appended by the delivery/ingest code itself (email_ingest), which runs after your personal Sieve script has already executed fileinto/keep decisions. That’s why you can’t header :contains "X-Spam-Status" in a user script — the header simply doesn’t exist yet at that point in the pipeline, it’s generated once the final mailbox placement has been decided.

To act on spam classification inside a personal (untrusted) Sieve script, use the spamtest/spamtestplus tests (RFC 5235), which Stalwart maps directly from the computed spam percentage of the message:

require ["spamtest", "comparator-i;ascii-numeric"];

if spamtest :value "ge" :comparator "i;ascii-numeric" "5" {
    fileinto "Junk";
}

Regarding the score mapping you saw (9.1 → spamtest 6): the spamtest value is not a linear rescale of the raw score to 0–10. Internally, Stalwart converts the raw spam score to a 0–100 “percentage,” where a score below the configured spam threshold maps to 0–49, exactly the spam threshold maps to 50 (spamtest 5), and anything above is scaled between the spam threshold and whichever of the discard/reject thresholds is higher, up to 100 (spamtest 10). So a score of 9.1 only yields spamtest 6 if your reject/discard thresholds are set well above 9.1, stretching that scale. To replicate “Yes”/“No” classification exactly as in X-Spam-Status, test spamtest :value :ge "0" "5" (≥5 means classified spam) rather than expecting the extreme 1 or 10 values.

There’s no mechanism today to expose X-Spam-Status/X-Spam-Score headers directly for querying at the untrusted user-script stage beyond spamtest/spamtestplus.

Overview | Stalwart and Overview | Stalwart

This is an automated reply from the Stalwart Help Bot. Other community members may follow up if this answer is incomplete or wrong.

This post says something totally different, not? Spamtest sieve function only returns binary spam or ham, no intermediate values - #2 by helpbot

“A Sieve script can use a header :matches (or regex) test against X-Spam-Status to extract the numeric score”… how is this possible, when those headers are added at the end? Which Sieve script is able to read them?

Could you please provide a “human” comment on how it really is? :anxious_face_with_sweat:

Kind regards,
Sebastian

X-Spam-Score is visible to user scripts. The spam filter adds it (along with X-Spam-Result) during the SMTP DATA stage, before the message reaches the mailbox, so header :contains "X-Spam-Score" "spam," works, and so does any webmail filter editor that can match on a header. X-Spam-Status is different: it’s added per recipient at delivery, after the user’s script has run, because it takes per-user overrides into account (sender in the address book, replies in a trusted thread). The helpbot answer in topic 1404 is wrong on that point. For the basic case nobody needs a script anyway: a message with X-Spam-Status Yes that would land in Inbox is filed into Junk automatically.

spamtest doesn’t return the score itself. The score is first turned into a percentage: anything below your spam threshold maps to 0-49%, the threshold itself is 50%, and 100% is the lowest reject or discard threshold you have enabled (twice the spam threshold if there’s none). The 1-10 value is that percentage in tens, so 6 means somewhere between 60 and 69%. To test “is spam”, use the percent form:

require ["spamtestplus", "relational", "comparator-i;ascii-numeric", "fileinto"];
if spamtest :percent :value "ge" :comparator "i;ascii-numeric" "50" { fileinto "Junk"; }

Great. Thank you for your response. Please, permit me just another question.
Is the following rule “Matching To Header → Inbox folder” now covered by - what you a calling - the basic case? I was using this script with an earlier 0.16 version and also got the spam messages moved to the INBOX folder (user addressbook exemption disabled and not a response to anything)…

# Rule: Move [email protected]
if header :contains "To" "[email protected]" {
    fileinto "Inbox/A";
    stop;
}

# Rule: Move [email protected]
if header :contains "To" "[email protected]" {
    fileinto "Inbox/B";
    stop;
}

Otherwise, would a prefix guard for header :contains "X-Spam-Score" "ham," help to get this sorted out?

Thanks a lot.
Sebastian

No, the automatic move only happens when the message would otherwise end up in Inbox itself. A fileinto "Inbox/A" is an explicit destination and wins, which is why spam ended up in those folders on the earlier 0.16 too.

A guard fixes it, and I’d make it test for spam:

if not header :contains "X-Spam-Score" "spam," {
    if header :contains "To" "[email protected]" {
        fileinto "Inbox/A";
        stop;
    }
    if header :contains "To" "[email protected]" {
        fileinto "Inbox/B";
        stop;
    }
}

Spam then falls through to Inbox and the server files it into Junk, still applying the address-book and trusted-reply exemptions (a fileinto "Junk" in the script would skip those). Testing for ham, would also skip your rules for any message that never got an X-Spam-Score header. Your second rule checks [email protected] again; I assumed you meant b@.