diff --git a/app.py b/app.py index c4cef7d..f45d905 100644 --- a/app.py +++ b/app.py @@ -1,865 +1,866 @@ import babel import collections import copy import decorator import flask import jinja2 import json import mwapi import mwoauth import os import random import re import requests_oauthlib import string import toolforge import yaml import werkzeug.datastructures from flask_utils import OrderedFlask, TagOrderedMultiDict, TagImmutableOrderedMultiDict from formatters import I18nFormatter from matching import match_template_to_lexeme_data, match_lexeme_forms_to_template, match_template_entity_to_lexeme_entity from parse_tpsv import parse_lexemes from templates import templates from translations import translations app = OrderedFlask(__name__) app.session_interface.serializer.register(TagOrderedMultiDict, index=0) app.session_interface.serializer.register(TagImmutableOrderedMultiDict, index=0) user_agent = toolforge.set_user_agent('lexeme-forms', email='mail@lucaswerkmeister.de') __dir__ = os.path.dirname(__file__) try: with open(os.path.join(__dir__, 'config.yaml')) as config_file: app.config.update(yaml.safe_load(config_file)) consumer_token = mwoauth.ConsumerToken(app.config['oauth']['consumer_key'], app.config['oauth']['consumer_secret']) except FileNotFoundError: print('config.yaml file not found, assuming local development setup') app.secret_key = 'fake' @app.before_request def fixSessionToken(): """Fix the session token after its path was changed. Old versions of this tool on Toolforge used '/' for the session cookie path, which was insecure, sending our session cookie to other tools as well. However, changing it to the tool name does not invalidate the old cookie, so the first time a client visits the tool again after this change was made, when we try to update the cookie in our response, we’re actually setting a new one with a different path, and on the next request we’ll receive two session cookies, for the old and new path. That is the earliest time when we can detect the situation, and deal with it by instructing the client to delete the '/' version and then reload. (We could try to decode the old session and salvage parts of it, but this tool only uses the session for the CSRF token and OAuth tokens, and salvaging either of those is probably a bad idea.) """ if app.config.get('APPLICATION_ROOT', '/') == '/': return cookies_header = flask.request.headers.get('Cookie') if not cookies_header: return first_session = cookies_header.find('session=') if first_session < 0: return second_session = cookies_header[first_session+1:].find('session=') if second_session < 0: return response = flask.redirect(current_url()) response.set_cookie('session', '', expires=0, path='/') return response @decorator.decorator def enableCORS(func, *args, **kwargs): rv = func(*args, **kwargs) response = flask.make_response(rv) response.headers['Access-Control-Allow-Origin'] = '*' return response @app.after_request def denyFrame(response): """Disallow embedding the tool’s pages in other websites. If other websites can embed this tool’s pages, e. g. in