Disable activitypub requests in get_data

This commit is contained in:
Mouse Reeve 2024-08-27 14:18:30 -07:00
parent 877b818470
commit fbf551fb75
6 changed files with 23 additions and 8 deletions

View file

@ -191,7 +191,7 @@ class AbstractConnector(AbstractMinimalConnector):
def get_book_data(self, remote_id: str) -> JsonDict: # pylint: disable=no-self-use def get_book_data(self, remote_id: str) -> JsonDict: # pylint: disable=no-self-use
"""this allows connectors to override the default behavior""" """this allows connectors to override the default behavior"""
return get_data(remote_id) return get_data(remote_id, is_activitypub=False)
def create_edition_from_data( def create_edition_from_data(
self, self,
@ -310,8 +310,13 @@ def get_data(
url: str, url: str,
params: Optional[dict[str, str]] = None, params: Optional[dict[str, str]] = None,
timeout: int = settings.QUERY_TIMEOUT, timeout: int = settings.QUERY_TIMEOUT,
is_activitypub: bool = True,
) -> JsonDict: ) -> JsonDict:
"""wrapper for request.get""" """wrapper for request.get"""
# make sure this isn't a forbidden federated request
if is_activitypub:
models.SiteSettings.objects.get().raise_federation_disabled()
# check if the url is blocked # check if the url is blocked
raise_not_valid_url(url) raise_not_valid_url(url)

View file

@ -66,7 +66,7 @@ class Connector(AbstractConnector):
return f"{self.books_url}?action=by-uris&uris={value}" return f"{self.books_url}?action=by-uris&uris={value}"
def get_book_data(self, remote_id: str) -> JsonDict: def get_book_data(self, remote_id: str) -> JsonDict:
data = get_data(remote_id) data = get_data(remote_id, is_activitypub=False)
extracted = list(data.get("entities", {}).values()) extracted = list(data.get("entities", {}).values())
try: try:
data = extracted[0] data = extracted[0]
@ -128,7 +128,7 @@ class Connector(AbstractConnector):
"""get a list of editions for a work""" """get a list of editions for a work"""
# pylint: disable=line-too-long # pylint: disable=line-too-long
url = f"{self.books_url}?action=reverse-claims&property=wdt:P629&value={work_uri}&sort=true" url = f"{self.books_url}?action=reverse-claims&property=wdt:P629&value={work_uri}&sort=true"
return get_data(url) return get_data(url, is_activitypub=False)
def get_edition_from_work_data(self, data: JsonDict) -> JsonDict: def get_edition_from_work_data(self, data: JsonDict) -> JsonDict:
work_uri = data.get("uri") work_uri = data.get("uri")
@ -226,7 +226,7 @@ class Connector(AbstractConnector):
return "" return ""
url = f"{self.base_url}/api/data?action=wp-extract&lang=en&title={link}" url = f"{self.base_url}/api/data?action=wp-extract&lang=en&title={link}"
try: try:
data = get_data(url) data = get_data(url, is_activitypub=False)
except ConnectorException: except ConnectorException:
return "" return ""
return str(data.get("extract", "")) return str(data.get("extract", ""))

View file

@ -99,10 +99,10 @@ class Connector(AbstractConnector):
] ]
def get_book_data(self, remote_id: str) -> JsonDict: def get_book_data(self, remote_id: str) -> JsonDict:
data = get_data(remote_id) data = get_data(remote_id, is_activitypub=False)
if data.get("type", {}).get("key") == "/type/redirect": if data.get("type", {}).get("key") == "/type/redirect":
remote_id = self.base_url + data.get("location", "") remote_id = self.base_url + data.get("location", "")
return get_data(remote_id) return get_data(remote_id, is_activitypub=False)
return data return data
def get_remote_id_from_data(self, data: JsonDict) -> str: def get_remote_id_from_data(self, data: JsonDict) -> str:

View file

@ -266,7 +266,7 @@ def preview_image(instance, *args, **kwargs):
def check_for_updates_task(): def check_for_updates_task():
"""See if git remote knows about a new version""" """See if git remote knows about a new version"""
site = SiteSettings.objects.get() site = SiteSettings.objects.get()
release = get_data(RELEASE_API, timeout=3) release = get_data(RELEASE_API, timeout=3, is_activitypub=False)
available_version = release.get("tag_name", None) available_version = release.get("tag_name", None)
if available_version: if available_version:
site.available_version = available_version site.available_version = available_version

View file

@ -63,6 +63,9 @@ def is_bookwyrm_request(request):
def handle_remote_webfinger(query, unknown_only=False, refresh=False): def handle_remote_webfinger(query, unknown_only=False, refresh=False):
"""webfingerin' other servers""" """webfingerin' other servers"""
# SHOULD we do a remote webfinger? Is it allowed?
models.SiteSettings.objects.get().raise_federation_disabled()
user = None user = None
# usernames could be @user@domain or user@domain # usernames could be @user@domain or user@domain
@ -107,6 +110,9 @@ def handle_remote_webfinger(query, unknown_only=False, refresh=False):
def subscribe_remote_webfinger(query): def subscribe_remote_webfinger(query):
"""get subscribe template from other servers""" """get subscribe template from other servers"""
# SHOULD we do a remote webfinger? Is it allowed?
models.SiteSettings.objects.get().raise_federation_disabled()
template = None template = None
# usernames could be @user@domain or user@domain # usernames could be @user@domain or user@domain
if not query: if not query:

View file

@ -3,6 +3,7 @@
import re import re
from django.contrib.postgres.search import TrigramSimilarity, SearchRank, SearchQuery from django.contrib.postgres.search import TrigramSimilarity, SearchRank, SearchQuery
from django.core.exceptions import PermissionDenied
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models import F from django.db.models import F
from django.db.models.functions import Greatest from django.db.models.functions import Greatest
@ -129,7 +130,10 @@ def user_search(request):
# use webfinger for mastodon style account@domain.com username to load the user if # use webfinger for mastodon style account@domain.com username to load the user if
# they don't exist locally (handle_remote_webfinger will check the db) # they don't exist locally (handle_remote_webfinger will check the db)
if re.match(regex.FULL_USERNAME, query) and viewer.is_authenticated: if re.match(regex.FULL_USERNAME, query) and viewer.is_authenticated:
try:
handle_remote_webfinger(query) handle_remote_webfinger(query)
except PermissionDenied:
return TemplateResponse(request, "search/user.html", data)
results = ( results = (
models.User.viewer_aware_objects(viewer) models.User.viewer_aware_objects(viewer)