Blocking disposable email domains in Mastodon

⚠️
This post was automatically migrated from my old blogging software, and I have not reviewed it for problems yet. Please contact me if you notice any important issues.

Mastodon yesterday experienced a small wave of spam. One thing we’re doing in response at a2mi.social, as recommended by this summary of the incident, is improving how we block disposable email providers.

I wrote the following Bash script, inspired by this one. Run periodically via cron, it updates a2mi.social’s blocklist from the disposable-email-domains GitHub repo.

#!/usr/bin/env bash
set -euo pipefail
MASTODON_COMPOSE_DIR=/opt/docker/compose/mastodon
BLOCKLIST_DIR=/opt/disposable-email-domains
# BLOCKLIST_DIR is expected to contain a checkout of https://github.com/disposable-email-domains/disposable-email-domains.git
# Set up via `git clone https://github.com/disposable-email-domains/disposable-email-domains.git /opt/disposable-email-domains`
TMP_ALLOWLIST=/tmp/mail-allowlist.conf
TMP_BLOCKLIST=/tmp/mail-blocklist.conf
echo “Update blocklist repo ...”
cd ”$BLOCKLIST_DIR”
git pull
echo “Converting block/allowlists ...”
sed -e ‘:a’ -e ‘N’ -e ‘$!ba’ -e ‘s/\n/ /g’ ”$BLOCKLIST_DIR”/allowlist.conf | sed ‘s/ /\n/1000;P;D’ > ”$TMP_ALLOWLIST”
sed -e ‘:a’ -e ‘N’ -e ‘$!ba’ -e ‘s/\n/ /g’ ”$BLOCKLIST_DIR”/disposable_email_blocklist.conf | sed ‘s/ /\n/1000;P;D’ > ”$TMP_BLOCKLIST”
cd ”$MASTODON_COMPOSE_DIR”
echo “Add blocks ...”
while read -r LINE; do
# shellcheck disable=SC2086
docker compose exec web bin/tootctl email-domain-blocks add $LINE > /dev/null
done < ”$TMP_BLOCKLIST”
echo “Remove allowlist entries ...”
while read -r LINE; do
# shellcheck disable=SC2086
docker compose exec web bin/tootctl email-domain-blocks remove $LINE > /dev/null
done < ”$TMP_ALLOWLIST”
echo “Cleanup temporary files ...”
rm ”$TMP_ALLOWLIST”
rm ”$TMP_BLOCKLIST”
echo “Finished.”