Skip to content

Commit

Permalink
fix: change message-id field size and filter larger values as spam (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rpcross authored Nov 22, 2023
1 parent d55b299 commit e18a949
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
9 changes: 8 additions & 1 deletion backend/mlarchive/archive/inspectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,11 @@ def handle_file(self):
pass

def raise_error(self):
raise NoArchiveMessage('X-No-Archive Message-ID: {}'.format(self.message_wrapper.msgid))
raise NoArchiveMessage('X-No-Archive Message-ID: {}'.format(self.message_wrapper.msgid))


class LongMessageIDSpamInspector(SpamInspector):
'''Checks if the Message-ID header exceeds max length'''
def has_condition(self):
msgid = self.message_wrapper.email_message.get('Message-ID')
return len(msgid) > 998
18 changes: 18 additions & 0 deletions backend/mlarchive/archive/migrations/0002_alter_message_msgid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-11-09 12:41

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("archive", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="message",
name="msgid",
field=models.CharField(db_index=True, max_length=998),
),
]
2 changes: 1 addition & 1 deletion backend/mlarchive/archive/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Message(models.Model):
in_reply_to_value = models.TextField(blank=True, default='')
# mapping to MHonArc message number
legacy_number = models.IntegerField(blank=True, null=True, db_index=True)
msgid = models.CharField(max_length=240, db_index=True)
msgid = models.CharField(max_length=998, db_index=True) # See RFC 2822
references = models.TextField(blank=True, default='')
spam_score = models.IntegerField(default=0) # > 0 = spam
subject = models.CharField(max_length=512, blank=True)
Expand Down
1 change: 1 addition & 0 deletions backend/mlarchive/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
'ListIdExistsSpamInspector': {'includes': ['httpbisa']},
'SpamLevelSpamInspector': {'includes': ['rfc-dist', 'rfc-interest', 'httpbisa', 'ipp', 'krb-wg', 'ietf-dkim']},
'NoArchiveInspector': {},
'LongMessageIDSpamInspector': {},
}

# AUTH
Expand Down
25 changes: 24 additions & 1 deletion backend/mlarchive/tests/archive/inspectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pytest

from mlarchive.archive.inspectors import (ListIdSpamInspector, SpamMessage,
SpamLevelSpamInspector, NoArchiveInspector, NoArchiveMessage)
SpamLevelSpamInspector, NoArchiveInspector, NoArchiveMessage,
LongMessageIDSpamInspector)
from mlarchive.archive.mail import MessageWrapper


Expand Down Expand Up @@ -72,3 +73,25 @@ def test_NoArchiveInspector(client, settings):
with pytest.raises(NoArchiveMessage) as excinfo:
inspector.inspect()
assert 'X-No-Archive' in str(excinfo.value)


@pytest.mark.django_db(transaction=True)
def test_LongMessageIDSpamInspector(client, settings):
settings.INSPECTORS = {'LongMessageIDSpamInspector': {}}
# regular message
path = os.path.join(settings.BASE_DIR, 'tests', 'data', 'mail_normal.1')
with open(path) as f:
message = email.message_from_file(f)
mw = MessageWrapper.from_message(message, 'acme')
inspector = LongMessageIDSpamInspector(mw)
inspector.inspect()
# spam message
path = os.path.join(settings.BASE_DIR, 'tests', 'data', 'mail_long_messageid.1')
with open(path) as f:
message = email.message_from_file(f)
mw = MessageWrapper.from_message(message, 'acme')
inspector = LongMessageIDSpamInspector(mw)
with pytest.raises(SpamMessage) as excinfo:
inspector.inspect()
print(excinfo)
assert 'Spam' in str(excinfo.value)
10 changes: 10 additions & 0 deletions backend/mlarchive/tests/data/mail_long_messageid.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
From: User <[email protected]>
To: User <[email protected]>
Date: Thu, 7 Nov 2013 17:54:55 +0000
Message-ID: <0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000@amsl.com>
Content-Type: text/plain; charset="us-ascii"
Subject: This is a test

Hello,

This is a test email.

0 comments on commit e18a949

Please sign in to comment.