exceptions: RateLimited shouldn't inherit from PermissionDenied.

We will want to raise RateLimited in authenticate() in rate limiting
code - Django's authenticate() mechanism catches PermissionDenied, which
we don't want for RateLimited. We want RateLimited to propagate to our
code that called the authenticate() function.
This commit is contained in:
Mateusz Mandera
2019-08-01 18:48:41 +02:00
committed by Tim Abbott
parent a6a2d70320
commit 335b804510
2 changed files with 3 additions and 9 deletions

View File

@@ -2,7 +2,6 @@ from enum import Enum
from typing import Any, Dict, List, Type, TypeVar, Optional
from typing_extensions import NoReturn
from django.core.exceptions import PermissionDenied
from django.utils.translation import ugettext as _
@@ -179,7 +178,7 @@ class InvalidMarkdownIncludeStatement(JsonableError):
def msg_format() -> str:
return _("Invalid markdown include statement: {include_statement}")
class RateLimited(PermissionDenied):
class RateLimited(Exception):
def __init__(self, msg: str="") -> None:
super().__init__(msg)

View File

@@ -3,8 +3,7 @@ import logging
import time
import traceback
from typing import Any, AnyStr, Dict, \
Iterable, List, MutableMapping, Optional, \
Union
Iterable, List, MutableMapping, Optional
from django.conf import settings
from django.contrib.sessions.backends.base import UpdateError
@@ -350,12 +349,8 @@ class RateLimitMiddleware(MiddlewareMixin):
return response
# TODO: When we have Django stubs, we should be able to fix the
# type of exception back to just Exception; the problem is without
# stubs, mypy doesn't know that RateLimited's superclass
# PermissionDenied inherits from Exception.
def process_exception(self, request: HttpRequest,
exception: Union[Exception, RateLimited]) -> Optional[HttpResponse]:
exception: Exception) -> Optional[HttpResponse]:
if isinstance(exception, RateLimited):
entity_type = str(exception) # entity type is passed to RateLimited when raising
resp = json_error(