2020-08-12 00:36:09 +00:00
|
|
|
import logging
|
2022-03-23 02:50:12 +00:00
|
|
|
from typing import Union, List, Optional, Tuple, Dict, Any
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.models.users import ApiKeys
|
2022-01-15 00:23:50 +00:00
|
|
|
from app.classes.shared.authentication import authentication
|
2021-09-08 22:01:10 +00:00
|
|
|
from app.classes.shared.main_controller import Controller
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.shared.helpers import helper
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
try:
|
|
|
|
import tornado.web
|
|
|
|
import bleach
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
helper.auto_installer_fix(e)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
class BaseHandler(tornado.web.RequestHandler):
|
|
|
|
|
2021-04-03 18:18:43 +00:00
|
|
|
nobleach = {bool, type(None)}
|
2021-04-04 17:48:02 +00:00
|
|
|
redactables = ("pass", "api")
|
2021-04-03 18:18:43 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
# noinspection PyAttributeOutsideInit
|
2022-03-23 02:50:12 +00:00
|
|
|
def initialize(
|
|
|
|
self, controller: Controller = None, tasks_manager=None, translator=None
|
|
|
|
):
|
2021-03-22 04:02:18 +00:00
|
|
|
self.controller = controller
|
|
|
|
self.tasks_manager = tasks_manager
|
2021-03-26 13:57:50 +00:00
|
|
|
self.translator = translator
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
def get_remote_ip(self):
|
2022-03-23 02:50:12 +00:00
|
|
|
remote_ip = (
|
|
|
|
self.request.headers.get("X-Real-IP")
|
|
|
|
or self.request.headers.get("X-Forwarded-For")
|
|
|
|
or self.request.remote_ip
|
|
|
|
)
|
2020-08-12 00:36:09 +00:00
|
|
|
return remote_ip
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
current_user: Optional[Tuple[Optional[ApiKeys], Dict[str, Any], Dict[str, Any]]]
|
2022-03-23 02:50:12 +00:00
|
|
|
|
|
|
|
def get_current_user(
|
|
|
|
self,
|
|
|
|
) -> Optional[Tuple[Optional[ApiKeys], Dict[str, Any], Dict[str, Any]]]:
|
2022-01-15 00:23:50 +00:00
|
|
|
return authentication.check(self.get_cookie("token"))
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2021-04-04 17:48:02 +00:00
|
|
|
def autobleach(self, name, text):
|
|
|
|
for r in self.redactables:
|
|
|
|
if r in name:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Auto-bleaching {name}: [**REDACTED**]")
|
2021-04-04 18:22:52 +00:00
|
|
|
break
|
2021-04-04 17:48:02 +00:00
|
|
|
else:
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Auto-bleaching {name}: {text}")
|
2021-04-03 18:18:43 +00:00
|
|
|
if type(text) in self.nobleach:
|
2021-04-04 17:48:02 +00:00
|
|
|
logger.debug("Auto-bleaching - bypass type")
|
2021-03-22 04:02:18 +00:00
|
|
|
return text
|
|
|
|
else:
|
2021-04-03 18:18:43 +00:00
|
|
|
return bleach.clean(text)
|
2021-03-22 04:02:18 +00:00
|
|
|
|
|
|
|
def get_argument(
|
2022-03-23 02:50:12 +00:00
|
|
|
self,
|
|
|
|
name: str,
|
|
|
|
default: Union[
|
|
|
|
None, str, tornado.web._ArgDefaultMarker
|
|
|
|
] = tornado.web._ARG_DEFAULT,
|
|
|
|
strip: bool = True,
|
|
|
|
) -> Optional[str]:
|
2021-03-22 04:02:18 +00:00
|
|
|
arg = self._get_argument(name, default, self.request.arguments, strip)
|
2021-04-04 17:48:02 +00:00
|
|
|
return self.autobleach(name, arg)
|
2021-03-22 04:02:18 +00:00
|
|
|
|
|
|
|
def get_arguments(self, name: str, strip: bool = True) -> List[str]:
|
2022-03-08 03:12:56 +00:00
|
|
|
if not isinstance(strip, bool):
|
|
|
|
raise AssertionError
|
2021-03-22 04:02:18 +00:00
|
|
|
args = self._get_arguments(name, self.request.arguments, strip)
|
|
|
|
args_ret = []
|
|
|
|
for arg in args:
|
2021-04-04 17:48:02 +00:00
|
|
|
args_ret += self.autobleach(name, arg)
|
2021-03-22 04:02:18 +00:00
|
|
|
return args_ret
|