diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 1b14149f2..2a81eaa3e 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -24,5 +24,5 @@ jobs: pip install pylint - name: Analysing the code with pylint run: | - pylint bookwyrm/ --ignore=migrations,tests --disable=E1101,E1135,E1136,R0903,R0901,R0902,W0707,W0511,W0406,R0401,R0801 + pylint bookwyrm/ --ignore=migrations,tests --disable=E1101,E1135,E1136,R0903,R0901,R0902,W0707,W0511,W0406,R0401,R0801,C0209 diff --git a/bookwyrm/forms.py b/bookwyrm/forms.py index b53d0b6a5..0be53ba0a 100644 --- a/bookwyrm/forms.py +++ b/bookwyrm/forms.py @@ -311,6 +311,12 @@ class EmailBlocklistForm(CustomForm): fields = ["domain"] +class IPBlocklistForm(CustomForm): + class Meta: + model = models.IPBlocklist + fields = ["address"] + + class ServerForm(CustomForm): class Meta: model = models.FederatedServer diff --git a/bookwyrm/middleware/__init__.py b/bookwyrm/middleware/__init__.py new file mode 100644 index 000000000..03843c5a3 --- /dev/null +++ b/bookwyrm/middleware/__init__.py @@ -0,0 +1,3 @@ +""" look at all this nice middleware! """ +from .timezone_middleware import TimezoneMiddleware +from .ip_middleware import IPBlocklistMiddleware diff --git a/bookwyrm/middleware/ip_middleware.py b/bookwyrm/middleware/ip_middleware.py new file mode 100644 index 000000000..8063dd1f6 --- /dev/null +++ b/bookwyrm/middleware/ip_middleware.py @@ -0,0 +1,16 @@ +""" Block IP addresses """ +from django.http import Http404 +from bookwyrm import models + + +class IPBlocklistMiddleware: + """check incoming traffic against an IP block-list""" + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + address = request.META.get("REMOTE_ADDR") + if models.IPBlocklist.objects.filter(address=address).exists(): + raise Http404() + return self.get_response(request) diff --git a/bookwyrm/timezone_middleware.py b/bookwyrm/middleware/timezone_middleware.py similarity index 100% rename from bookwyrm/timezone_middleware.py rename to bookwyrm/middleware/timezone_middleware.py diff --git a/bookwyrm/migrations/0097_auto_20210917_1858.py b/bookwyrm/migrations/0097_auto_20210917_1858.py new file mode 100644 index 000000000..28cf94e25 --- /dev/null +++ b/bookwyrm/migrations/0097_auto_20210917_1858.py @@ -0,0 +1,38 @@ +# Generated by Django 3.2.4 on 2021-09-17 18:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0096_merge_20210912_0044"), + ] + + operations = [ + migrations.CreateModel( + name="IPBlocklist", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("created_date", models.DateTimeField(auto_now_add=True)), + ("address", models.CharField(max_length=255, unique=True)), + ("is_active", models.BooleanField(default=True)), + ], + options={ + "ordering": ("-created_date",), + }, + ), + migrations.AddField( + model_name="emailblocklist", + name="is_active", + field=models.BooleanField(default=True), + ), + ] diff --git a/bookwyrm/models/__init__.py b/bookwyrm/models/__init__.py index e8e1b696e..4228371f5 100644 --- a/bookwyrm/models/__init__.py +++ b/bookwyrm/models/__init__.py @@ -26,8 +26,8 @@ from .import_job import ImportJob, ImportItem from .site import SiteSettings, SiteInvite from .site import PasswordReset, InviteRequest -from .site import EmailBlocklist from .announcement import Announcement +from .antispam import EmailBlocklist, IPBlocklist cls_members = inspect.getmembers(sys.modules[__name__], inspect.isclass) activity_models = { diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py new file mode 100644 index 000000000..7a85bbcf0 --- /dev/null +++ b/bookwyrm/models/antispam.py @@ -0,0 +1,35 @@ +""" Lets try NOT to sell viagra """ +from django.db import models + +from .user import User + + +class EmailBlocklist(models.Model): + """blocked email addresses""" + + created_date = models.DateTimeField(auto_now_add=True) + domain = models.CharField(max_length=255, unique=True) + is_active = models.BooleanField(default=True) + + class Meta: + """default sorting""" + + ordering = ("-created_date",) + + @property + def users(self): + """find the users associated with this address""" + return User.objects.filter(email__endswith=f"@{self.domain}") + + +class IPBlocklist(models.Model): + """blocked ip addresses""" + + created_date = models.DateTimeField(auto_now_add=True) + address = models.CharField(max_length=255, unique=True) + is_active = models.BooleanField(default=True) + + class Meta: + """default sorting""" + + ordering = ("-created_date",) diff --git a/bookwyrm/models/site.py b/bookwyrm/models/site.py index 09732c2fc..ee69a507f 100644 --- a/bookwyrm/models/site.py +++ b/bookwyrm/models/site.py @@ -124,23 +124,6 @@ class PasswordReset(models.Model): return "https://{}/password-reset/{}".format(DOMAIN, self.code) -class EmailBlocklist(models.Model): - """blocked email addresses""" - - created_date = models.DateTimeField(auto_now_add=True) - domain = models.CharField(max_length=255, unique=True) - - class Meta: - """default sorting""" - - ordering = ("-created_date",) - - @property - def users(self): - """find the users associated with this address""" - return User.objects.filter(email__endswith=f"@{self.domain}") - - # pylint: disable=unused-argument @receiver(models.signals.post_save, sender=SiteSettings) def preview_image(instance, *args, **kwargs): diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py index 452b8d940..cf33c129b 100644 --- a/bookwyrm/settings.py +++ b/bookwyrm/settings.py @@ -77,7 +77,8 @@ MIDDLEWARE = [ "django.middleware.common.CommonMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", - "bookwyrm.timezone_middleware.TimezoneMiddleware", + "bookwyrm.middleware.TimezoneMiddleware", + "bookwyrm.middleware.IPBlocklistMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", ] diff --git a/bookwyrm/static/css/bookwyrm.css b/bookwyrm/static/css/bookwyrm.css index 9065d7fba..e1012c2fd 100644 --- a/bookwyrm/static/css/bookwyrm.css +++ b/bookwyrm/static/css/bookwyrm.css @@ -378,6 +378,13 @@ input[type=file]::file-selector-button:hover { right: 1em; } +/** Tooltips + ******************************************************************************/ + +.tooltip { + width: 100%; +} + /** States ******************************************************************************/ diff --git a/bookwyrm/templates/components/tooltip.html b/bookwyrm/templates/components/tooltip.html index 06b13f344..b1a8f56c1 100644 --- a/bookwyrm/templates/components/tooltip.html +++ b/bookwyrm/templates/components/tooltip.html @@ -3,7 +3,7 @@ {% trans "Help" as button_text %} {% include 'snippets/toggle/open_button.html' with text=button_text class="ml-3 is-rounded is-small is-white p-0 pb-1" icon="question-circle is-size-6" controls_text=controls_text controls_uid=controls_uid %} -