email headers fixing

This commit is contained in:
nahakubuilde
2025-06-01 12:09:12 +01:00
parent 3cd698e289
commit db5767a547
10 changed files with 492 additions and 94 deletions

141
alembic.ini Normal file
View File

@@ -0,0 +1,141 @@
# A generic, single database configuration.
[alembic]
script_location = alembic
# path to migration scripts.
# this is typically a path given in POSIX (e.g. forward slashes)
# format, relative to the token %(here)s which refers to the location of this
# ini file
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
# Uncomment the line below if you want the files to be prepended with date and time
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
# for all available tokens
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory. for multiple paths, the path separator
# is defined by "path_separator" below.
prepend_sys_path = .
# timezone to use when rendering the date within the migration file
# as well as the filename.
# If specified, requires the python>=3.9 or backports.zoneinfo library and tzdata library.
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
# string value is passed to ZoneInfo()
# leave blank for localtime
# timezone =
# max length of characters to apply to the "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; This defaults
# to <script_location>/versions. When using multiple version
# directories, initial revisions must be specified with --version-path.
# The path separator used here should be the separator specified by "path_separator"
# below.
# version_locations = %(here)s/bar:%(here)s/bat:%(here)s/alembic/versions
# path_separator; This indicates what character is used to split lists of file
# paths, including version_locations and prepend_sys_path within configparser
# files such as alembic.ini.
# The default rendered in new alembic.ini files is "os", which uses os.pathsep
# to provide os-dependent path splitting.
#
# Note that in order to support legacy alembic.ini files, this default does NOT
# take place if path_separator is not present in alembic.ini. If this
# option is omitted entirely, fallback logic is as follows:
#
# 1. Parsing of the version_locations option falls back to using the legacy
# "version_path_separator" key, which if absent then falls back to the legacy
# behavior of splitting on spaces and/or commas.
# 2. Parsing of the prepend_sys_path option falls back to the legacy
# behavior of splitting on spaces, commas, or colons.
#
# Valid values for path_separator are:
#
# path_separator = :
# path_separator = ;
# path_separator = space
# path_separator = newline
#
# Use os.pathsep. Default configuration used for new projects.
path_separator = os
# set to 'true' to search source files recursively
# in each "version_locations" directory
# new in Alembic version 1.10
# recursive_version_locations = false
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
# database URL. This is consumed by the user-maintained env.py script only.
# other means of configuring database URLs may be customized within the env.py
# file.
sqlalchemy.url = sqlite:///email_server/server_data/smtp_server.db
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
# hooks = ruff
# ruff.type = exec
# ruff.executable = %(here)s/.venv/bin/ruff
# ruff.options = check --fix REVISION_SCRIPT_FILENAME
# Logging configuration. This is also consumed by the user-maintained
# env.py script only.
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARNING
handlers = console
qualname =
[logger_sqlalchemy]
level = WARNING
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

1
alembic/README Normal file
View File

@@ -0,0 +1 @@
Generic single-database configuration.

79
alembic/env.py Normal file
View File

@@ -0,0 +1,79 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
from email_server.models import Base
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

28
alembic/script.py.mako Normal file
View File

@@ -0,0 +1,28 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
"""Upgrade schema."""
${upgrades if upgrades else "pass"}
def downgrade() -> None:
"""Downgrade schema."""
${downgrades if downgrades else "pass"}

View File

@@ -0,0 +1,133 @@
"""Initial migration
Revision ID: 53036910f343
Revises:
Create Date: 2025-06-01 11:14:39.589608
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '53036910f343'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
# The following columns may already exist, so we skip adding them if present.
# op.add_column('esrv_auth_logs', sa.Column('auth_type', sa.String(), nullable=False, server_default='user'))
# op.add_column('esrv_auth_logs', sa.Column('identifier', sa.String(), nullable=False, server_default='unknown'))
# op.add_column('esrv_auth_logs', sa.Column('ip_address', sa.String(), nullable=True))
# op.add_column('esrv_auth_logs', sa.Column('created_at', sa.DateTime(), nullable=True))
# SQLite does not support ALTER COLUMN, so we skip changing 'message' type/nullability here.
# If you need to change the type/nullability, do a manual migration as described in Alembic docs.
# op.drop_column('esrv_auth_logs', 'peer')
# op.drop_column('esrv_auth_logs', 'timestamp')
# op.drop_column('esrv_auth_logs', 'username')
# op.alter_column('esrv_custom_headers', 'created_at',
# existing_type=sa.TIMESTAMP(),
# type_=sa.DateTime(),
# existing_nullable=True,
# existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.drop_index(op.f('idx_esrv_custom_headers_domain'), table_name='esrv_custom_headers')
op.drop_index(op.f('idx_esrv_dkim_keys_domain'), table_name='esrv_dkim_keys')
# op.alter_column('esrv_domains', 'created_at',
# existing_type=sa.TIMESTAMP(),
# type_=sa.DateTime(),
# existing_nullable=True,
# existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.drop_column('esrv_domains', 'requires_auth')
op.add_column('esrv_email_logs', sa.Column('from_address', sa.String(), nullable=False, server_default='unknown'))
op.add_column('esrv_email_logs', sa.Column('to_address', sa.String(), nullable=False, server_default='unknown'))
op.add_column('esrv_email_logs', sa.Column('subject', sa.Text(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('message', sa.Text(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('created_at', sa.DateTime(), nullable=True))
op.drop_index(op.f('idx_esrv_email_logs_mail_from'), table_name='esrv_email_logs')
op.drop_index(op.f('idx_esrv_email_logs_timestamp'), table_name='esrv_email_logs')
# op.drop_column('esrv_email_logs', 'rcpt_tos')
# op.drop_column('esrv_email_logs', 'content')
# op.drop_column('esrv_email_logs', 'dkim_signed')
# op.drop_column('esrv_email_logs', 'peer')
# op.drop_column('esrv_email_logs', 'message_id')
# op.drop_column('esrv_email_logs', 'mail_from')
# op.drop_column('esrv_email_logs', 'timestamp')
# op.alter_column('esrv_users', 'created_at',
# existing_type=sa.TIMESTAMP(),
# type_=sa.DateTime(),
# existing_nullable=True,
# existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.drop_index(op.f('idx_esrv_users_domain'), table_name='esrv_users')
op.drop_index(op.f('idx_esrv_users_email'), table_name='esrv_users')
# op.alter_column('esrv_whitelisted_ips', 'created_at',
# existing_type=sa.TIMESTAMP(),
# type_=sa.DateTime(),
# existing_nullable=True,
# existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.drop_index(op.f('idx_esrv_whitelisted_ips_domain'), table_name='esrv_whitelisted_ips')
op.drop_index(op.f('idx_esrv_whitelisted_ips_ip'), table_name='esrv_whitelisted_ips')
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_index(op.f('idx_esrv_whitelisted_ips_ip'), 'esrv_whitelisted_ips', ['ip_address'], unique=False)
op.create_index(op.f('idx_esrv_whitelisted_ips_domain'), 'esrv_whitelisted_ips', ['domain_id'], unique=False)
op.alter_column('esrv_whitelisted_ips', 'created_at',
existing_type=sa.DateTime(),
type_=sa.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.create_index(op.f('idx_esrv_users_email'), 'esrv_users', ['email'], unique=False)
op.create_index(op.f('idx_esrv_users_domain'), 'esrv_users', ['domain_id'], unique=False)
op.alter_column('esrv_users', 'created_at',
existing_type=sa.DateTime(),
type_=sa.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.add_column('esrv_email_logs', sa.Column('timestamp', sa.DATETIME(), nullable=False))
op.add_column('esrv_email_logs', sa.Column('mail_from', sa.VARCHAR(), nullable=False))
op.add_column('esrv_email_logs', sa.Column('message_id', sa.VARCHAR(), nullable=False))
op.add_column('esrv_email_logs', sa.Column('peer', sa.VARCHAR(), nullable=False))
op.add_column('esrv_email_logs', sa.Column('dkim_signed', sa.BOOLEAN(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('content', sa.TEXT(), nullable=False))
op.add_column('esrv_email_logs', sa.Column('rcpt_tos', sa.VARCHAR(), nullable=False))
op.create_index(op.f('idx_esrv_email_logs_timestamp'), 'esrv_email_logs', ['timestamp'], unique=False)
op.create_index(op.f('idx_esrv_email_logs_mail_from'), 'esrv_email_logs', ['mail_from'], unique=False)
op.drop_column('esrv_email_logs', 'created_at')
op.drop_column('esrv_email_logs', 'message')
op.drop_column('esrv_email_logs', 'subject')
op.drop_column('esrv_email_logs', 'to_address')
op.drop_column('esrv_email_logs', 'from_address')
op.add_column('esrv_domains', sa.Column('requires_auth', sa.BOOLEAN(), nullable=True))
op.alter_column('esrv_domains', 'created_at',
existing_type=sa.DateTime(),
type_=sa.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.create_index(op.f('idx_esrv_dkim_keys_domain'), 'esrv_dkim_keys', ['domain_id'], unique=False)
op.create_index(op.f('idx_esrv_custom_headers_domain'), 'esrv_custom_headers', ['domain_id'], unique=False)
op.alter_column('esrv_custom_headers', 'created_at',
existing_type=sa.DateTime(),
type_=sa.TIMESTAMP(),
existing_nullable=True,
existing_server_default=sa.text("'2025-05-31 00:00:00'"))
op.add_column('esrv_auth_logs', sa.Column('username', sa.VARCHAR(), nullable=True))
op.add_column('esrv_auth_logs', sa.Column('timestamp', sa.DATETIME(), nullable=False))
op.add_column('esrv_auth_logs', sa.Column('peer', sa.VARCHAR(), nullable=False))
op.alter_column('esrv_auth_logs', 'message',
existing_type=sa.Text(),
type_=sa.VARCHAR(),
nullable=False)
op.drop_column('esrv_auth_logs', 'created_at')
op.drop_column('esrv_auth_logs', 'ip_address')
op.drop_column('esrv_auth_logs', 'identifier')
op.drop_column('esrv_auth_logs', 'auth_type')
# ### end Alembic commands ###

View File

@@ -0,0 +1,42 @@
"""Add legacy EmailLog columns for backward compatibility
Revision ID: d02f993649e8
Revises: 53036910f343
Create Date: 2025-06-01 11:50:54.362830
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'd02f993649e8'
down_revision: Union[str, None] = '53036910f343'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# Add legacy columns for backward compatibility with existing EmailRelay code
op.add_column('esrv_email_logs', sa.Column('message_id', sa.String(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('timestamp', sa.DateTime(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('peer', sa.String(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('mail_from', sa.String(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('rcpt_tos', sa.String(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('content', sa.Text(), nullable=True))
op.add_column('esrv_email_logs', sa.Column('dkim_signed', sa.Boolean(), nullable=True, default=False))
def downgrade() -> None:
"""Downgrade schema."""
# Remove the legacy columns
op.drop_column('esrv_email_logs', 'dkim_signed')
op.drop_column('esrv_email_logs', 'content')
op.drop_column('esrv_email_logs', 'rcpt_tos')
op.drop_column('esrv_email_logs', 'mail_from')
op.drop_column('esrv_email_logs', 'peer')
op.drop_column('esrv_email_logs', 'timestamp')
op.drop_column('esrv_email_logs', 'message_id')

View File

@@ -129,15 +129,26 @@ class EmailLog(Base):
__tablename__ = 'esrv_email_logs'
id = Column(Integer, primary_key=True)
from_address = Column(String, nullable=False)
to_address = Column(String, nullable=False)
subject = Column(Text)
# Legacy columns (from original schema)
message_id = Column(String, unique=True, nullable=False)
timestamp = Column(DateTime, nullable=False)
peer = Column(String, nullable=False)
mail_from = Column(String, nullable=False)
rcpt_tos = Column(String, nullable=False)
content = Column(Text, nullable=False)
status = Column(String, nullable=False)
message = Column(Text)
dkim_signed = Column(Boolean, default=False)
# New columns (added later)
from_address = Column(String, nullable=False, server_default='unknown')
to_address = Column(String, nullable=False, server_default='unknown')
subject = Column(Text, nullable=True)
message = Column(Text, nullable=True)
created_at = Column(DateTime, default=func.now())
def __repr__(self):
return f"<EmailLog(id={self.id}, from='{self.from_address}', to='{self.to_address}', status='{self.status}')>"
return f"<EmailLog(id={self.id}, message_id='{self.message_id}', from='{self.mail_from}', to='{self.rcpt_tos}', status='{self.status}')>"
class AuthLog(Base):
"""Authentication log model for security auditing."""

View File

@@ -9,6 +9,7 @@ Security Features:
"""
import uuid
import email.utils
from datetime import datetime
from aiosmtpd.smtp import SMTP as AIOSMTP, AuthResult
from aiosmtpd.controller import Controller
@@ -61,85 +62,47 @@ class EnhancedCustomSMTPHandler:
self.auth_methods = ['LOGIN', 'PLAIN']
def _ensure_required_headers(self, content: str, envelope, message_id: str) -> str:
"""Ensure all required email headers are present.
"""Ensure all required email headers are present and properly formatted.
Args:
content (str): Email content.
envelope: SMTP envelope.
message_id (str): Generated message ID.
Returns:
str: Email content with all required headers.
"""
import email.utils
from datetime import datetime
# Parse existing headers
lines = content.split('\n')
headers = {}
body_start = 0
# Find where headers end and body begins
for i, line in enumerate(lines):
if line.strip() == '':
body_start = i + 1
break
if ':' in line and not line.startswith(' ') and not line.startswith('\t'):
header_name, header_value = line.split(':', 1)
headers[header_name.strip().lower()] = header_value.strip()
# Extract body
body = '\n'.join(lines[body_start:]) if body_start < len(lines) else ''
# Build required headers
required_headers = []
# Add Message-ID if missing
if 'message-id' not in headers:
required_headers.append(f"Message-ID: <{message_id}@{envelope.mail_from.split('@')[1] if '@' in envelope.mail_from else 'localhost'}>")
# Add Date if missing
if 'date' not in headers:
required_headers.append(f"Date: {email.utils.formatdate(localtime=True)}")
# Add From if missing
if 'from' not in headers:
required_headers.append(f"From: {envelope.mail_from}")
# Add To if missing
if 'to' not in headers:
to_list = ', '.join(envelope.rcpt_tos)
required_headers.append(f"To: {to_list}")
# Add MIME-Version if missing
if 'mime-version' not in headers:
required_headers.append("MIME-Version: 1.0")
# Add Content-Type if missing
if 'content-type' not in headers:
required_headers.append("Content-Type: text/plain; charset=utf-8")
# Rebuild the email with required headers first, then existing headers, then body
new_content_lines = required_headers
# Add existing headers (excluding the ones we just added)
for i in range(body_start):
line = lines[i]
if ':' in line and not line.startswith(' ') and not line.startswith('\t'):
header_name = line.split(':', 1)[0].strip().lower()
if header_name not in ['message-id', 'date', 'from', 'to', 'mime-version', 'content-type']:
new_content_lines.append(line)
elif line.startswith(' ') or line.startswith('\t'):
# Continuation of previous header
new_content_lines.append(line)
# Add empty line between headers and body
new_content_lines.append('')
# Add body
new_content_lines.append(body)
return '\r\n'.join(new_content_lines)
import email
from email.parser import Parser
from email.policy import default
# Parse the message using the email library
msg = Parser(policy=default).parsestr(content)
# Set or add required headers if missing
if not msg.get('Message-ID'):
msg['Message-ID'] = f"<{message_id}@{envelope.mail_from.split('@')[1] if '@' in envelope.mail_from else 'localhost'}>"
if not msg.get('Date'):
msg['Date'] = email.utils.formatdate(localtime=True)
if not msg.get('From'):
msg['From'] = envelope.mail_from
if not msg.get('To'):
msg['To'] = ', '.join(envelope.rcpt_tos)
if not msg.get('MIME-Version'):
msg['MIME-Version'] = '1.0'
if not msg.get('Content-Type'):
msg['Content-Type'] = 'text/plain; charset=utf-8'
if not msg.get('Subject'):
msg['Subject'] = '(No Subject)'
if not msg.get('Content-Transfer-Encoding'):
msg['Content-Transfer-Encoding'] = '7bit'
# Ensure exactly one blank line between headers and body
# The email library will handle this when flattening
from io import StringIO
out = StringIO()
out.write(msg.as_string())
return out.getvalue()
async def handle_DATA(self, server, session, envelope):
"""Handle incoming email data."""

17
main.py
View File

@@ -0,0 +1,17 @@
from email_server.server_runner import start_server
from email_server.tool_box import get_logger
import asyncio
import sys
logger = get_logger()
if __name__ == '__main__':
try:
logger.info('Server started')
asyncio.run(start_server())
except KeyboardInterrupt:
logger.info('Server interrupted by user')
sys.exit(0)
except Exception as e:
logger.error(f'Server error: {e}')
sys.exit(1)

View File

@@ -1,17 +0,0 @@
from email_server.server_runner import start_server
from email_server.tool_box import get_logger
import asyncio
import sys
logger = get_logger()
if __name__ == '__main__':
try:
logger.info('Server started')
asyncio.run(start_server())
except KeyboardInterrupt:
logger.info('Server interrupted by user')
sys.exit(0)
except Exception as e:
logger.error(f'Server error: {e}')
sys.exit(1)