From 1903812b1daa611a4475228aa10a7a4eae5e8d39 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 10 Apr 2021 11:38:57 -0700 Subject: [PATCH] Class method for checking if urls are blocked --- bookwyrm/connectors/abstract_connector.py | 6 ++++++ bookwyrm/models/federated_server.py | 8 ++++++++ bookwyrm/views/inbox.py | 18 +++--------------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bookwyrm/connectors/abstract_connector.py b/bookwyrm/connectors/abstract_connector.py index 2483cc62..2fe5d825 100644 --- a/bookwyrm/connectors/abstract_connector.py +++ b/bookwyrm/connectors/abstract_connector.py @@ -219,6 +219,12 @@ def dict_from_mappings(data, mappings): def get_data(url, params=None): """ wrapper for request.get """ + # check if the url is blocked + if models.FederatedServer.is_blocked(url): + raise ConnectorException( + "Attempting to load data from blocked url: {:s}".format(url) + ) + try: resp = requests.get( url, diff --git a/bookwyrm/models/federated_server.py b/bookwyrm/models/federated_server.py index 7283cdd8..d2b82355 100644 --- a/bookwyrm/models/federated_server.py +++ b/bookwyrm/models/federated_server.py @@ -1,4 +1,5 @@ """ connections to external ActivityPub servers """ +from urllib.parse import urlparse from django.db import models from .base_model import BookWyrmModel @@ -38,3 +39,10 @@ class FederatedServer(BookWyrmModel): # TODO: only reactivate users as appropriate self.user_set.update(is_active=True) + + @classmethod + def is_blocked(cls, url): + """ look up if a domain is blocked """ + url = urlparse(url) + domain = url.netloc + return cls.objects.filter(server_name=domain, status="blocked").exists() diff --git a/bookwyrm/views/inbox.py b/bookwyrm/views/inbox.py index 7997bd5c..99f9c556 100644 --- a/bookwyrm/views/inbox.py +++ b/bookwyrm/views/inbox.py @@ -1,7 +1,7 @@ """ incoming activities """ import json import re -from urllib.parse import urldefrag, urlparse +from urllib.parse import urldefrag from django.http import HttpResponse, HttpResponseNotFound from django.http import HttpResponseBadRequest, HttpResponseForbidden @@ -71,11 +71,7 @@ def is_blocked_user_agent(request): if not user_agent: return False url = re.search(r"https?://{:s}/?".format(regex.domain), user_agent).group() - domain = urlparse(url).netloc - if not domain: - # idk, we'll try again later with the actor - return False - return is_blocked(domain) + return models.FederatedServer.is_blocked(url) def is_blocked_activity(activity_json): @@ -84,15 +80,7 @@ def is_blocked_activity(activity_json): if not actor: # well I guess it's not even a valid activity so who knows return False - url = urlparse(actor) - return is_blocked(url.netloc) - - -def is_blocked(domain): - """ is this domain blocked? """ - return models.FederatedServer.objects.filter( - server_name=domain, status="blocked" - ).exists() + return models.FederatedServer.is_blocked(actor) @app.task