From 60dc595b4aff04e007eba92add42731c2067d9ea Mon Sep 17 00:00:00 2001 From: "Renato \"Lond\" Cerqueira" Date: Sat, 2 Jan 2021 10:25:31 +0100 Subject: [PATCH] Improve error for instances with authorized fetch enabled When authorized fetch mode is enabled on mastodon https://docs.joinmastodon.org/admin/config/#authorized_fetch, mastodon will signatures for endpoints which are used for activitypub or an authenticated user for internal endpoints. Since custom emojis is internal, it cannot be accessed without a logged-in user. This improves the error to make it more clear. --- emojos.py | 12 ++++++++++-- templates/forbidden.html | 5 +++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 templates/forbidden.html diff --git a/emojos.py b/emojos.py index dafc6da..d946993 100644 --- a/emojos.py +++ b/emojos.py @@ -22,6 +22,8 @@ from flask import Flask, redirect, render_template, request, url_for app = Flask(__name__) +class ForbiddenEndpointError(Exception): + pass @app.route('/') def emojo(domain): @@ -36,14 +38,20 @@ def emojo(domain): try: url = urllib.parse.urlunsplit(('https', domain, '/api/v1/custom_emojis', '', '')) + response = requests.get(url) + if response.status_code == 401: + raise ForbiddenEndpointError + if show_all: - emojo = sorted(requests.get(url).json(), key=operator.itemgetter('shortcode')) + emojo = sorted(response.json(), key=operator.itemgetter('shortcode')) else: - emojo = sorted(filter(lambda x: x.get('visible_in_picker', True), requests.get(url).json()), + emojo = sorted(filter(lambda x: x.get('visible_in_picker', True), response.json()), key=operator.itemgetter('shortcode')) return render_template('emojo.html', domain=domain, emojo=emojo, show_animated=show_animated) except requests.exceptions.RequestException as e: return render_template('oh_no.html', domain=domain) + except ForbiddenEndpointError: + return render_template('forbidden.html', domain=domain) @app.route('/favicon.ico') diff --git a/templates/forbidden.html b/templates/forbidden.html new file mode 100644 index 0000000..e0433b6 --- /dev/null +++ b/templates/forbidden.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} +{% block title %}Custom emoji list for {{ domain }}{% endblock title %} +{% block body %} +

{{ domain }} doesn't allow access to the v1/custom_emojis endpoint. :(

+{% endblock body %}