Skip to main content

gitlab-ico licence-mpl20 pre-commit-ico pipeline-status coverage status

Kilter Service

Kilter is a framework for writing mail filters (known as "milters") compatible with Sendmail and Postfix MTAs. Unlike many previous milter implementations in Python it is not simply bindings to the libmilter library (originally from the Sendmail project). The framework aims to provide Pythonic interfaces for implementing filters, including leveraging coroutines instead of libmilter's callback-style interface.

The kilter.service package contains the higher-level, asynchronous framework for writing mail filters.

Sendmail Filters

The Sendmail filter (milter) API facilitates communication between a Mail Transfer Agent (MTA) and arbitrary filters running as external services. These filters can perform a number of operations on received and outgoing mail, such as: virus scanning; checking senders' reputations; signing outgoing mail; and verifying signatures of incoming mail.

While the protocol was originally for filtering mail through a Sendmail MTA, Postfix has also reverse engineered the protocol and supports most filters made for Sendmail.

libmilter

Historically filters used the libmilter library supplied by the Sendmail project to handle all aspects of communication with an MTA. Filters simply registered callbacks for various events then started the library's main loop. This approach makes implementing simple filters in C easy for users, but makes writing "Pythonic" filters difficult, especially when a user wishes to make use of async/await features.

Use of libmilter to implement filters is almost universal as it is a black-box; the on-the-wire protocol used is undocumented and subject to change between versions, which makes writing a third-party parser difficult.

Alternatives in the Python Space

  • purepythonmilter: A modern and robust implementation for asyncio, written purely in statically typed Python without the need for libmilter.

  • python-libmilter: Another pure-Python module using threading. Lacks static type annotations and is no longer actively developed, although still minimally maintained.

Usage

To write filters, create a coroutine function that conforms to Filter. This function takes a Session object as its only argument.

Session instances have several awaitable methods corresponding to SMTP commands (i.e. MAIL, RCPT) and instances of HeadersAccessor and BodyAccessor which have awaitable methods and are themselves asynchronous iterators. The various methods await particular messages from an MTA and may return appropriate values from them. The asynchronous iterators yield repeating messages like kilter.protocol.Header and kilter.protocol.Body.

Examples

The following is a contrived example showing a filter that rejects messages sent by a particular user:

from kilter.service import Session
from kilter.protocol import Reject, Accept

# This corncob doesn't know when to stop; block him
BLOCK = b"the.black.knight@example.com"

async def reject_black_knight(session: Session) -> Reject|Accept:
	if (await session.envelope_from()) == BLOCK:
		return Reject()

	async with session.headers as headers:
		async for header in headers:
			if header.name == "From" and header.value == BLOCK:
				return Reject()

	return Accept()

The following two examples show two implementations of a filter that strips headers starting with "X-". They demonstrate the two methods of collecting headers, then later modifying them during the post phase.

from kilter.service import Session
from kilter.protocol import Accept

async def strip_x_headers(session: Session) -> Accept:
	remove = []

	# iterate over headers as they arrive and select ones for later removal
	async with session.headers as headers:
		async for header in headers:
			if header.name.startswith("X-"):
				remove.append(header)

	# remove the selected headers during the post phase
	for header in remove:
		await session.headers.delete(header)

	return Accept()
from kilter.service import Session
from kilter.protocol import Accept

async def strip_x_headers(session: Session) -> Accept:
	# collect the headers first
	await session.headers.collect()

	# iterate over collected headers during the post phase, removing the unwanted ones
	async with session.headers as headers:
		async for header in headers:
			if header.name.startswith("X-"):
				await session.headers.delete(header)

	return Accept()

Metadata

Release files for kilter.service 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kilter.service 0.3.1
File Size Uploaded Provenance Actions
kilter_service-0.3.1.tar.gz 22.1 kB Without provenance Details for kilter_service-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kilter.service 0.3.1
File Interpreter ABI Platform Actions
kilter_service-0.3.1-py3-none-any.whl Python 3 none any Details for kilter_service-0.3.1-py3-none-any.whl

Total release size: 45.3 kB

Release files / kilter_service-0.3.1.tar.gz

Download URL kilter_service-0.3.1.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
85a2b46f64587b0d7d4e7e6e77418cdd7e3de6092019c45b9ad08c091b327fbc
BLAKE2b-256 checksum
How to use checksums
75f8f43122605eae2138209074658fc3d9985e9f7a66e4476084c4102dbe94ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / kilter_service-0.3.1-py3-none-any.whl

Download URL kilter_service-0.3.1-py3-none-any.whl
Size 23.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bbd3a7c65b7915fd1cce6f64a2b1fcd45b9c65c9f7358d40a7920463f4d0a278
BLAKE2b-256 checksum
How to use checksums
a0618418db4728d20e596d1966a156ebb502e74ad3177e7613bea6e1686eac1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Security for kilter.service 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page