Excessive 404 Middleware
- class smarter.lib.django.middleware.excessive_404.SmarterBlockExcessive404Middleware(get_response, *args, **kwargs)[source]
Bases:
SmarterMiddlewareMixinMiddleware that throttles unauthenticated clients producing excessive 404s.
This mitigates:
automated scanners
brute-force URL probing
broken crawlers
vulnerability enumeration attempts
- async async_process_response(request, response)[source]
Async entry point for ASGI deployments: count anonymous 404s per client ip, and block excessive ones.
Unlike
process_response(), it does not check theENABLE_MIDDLEWARE_EXCESSIVE_404switch.- Parameters:
request (
HttpRequest) – The request.response (
HttpResponseBase) – The response from the view.
- Return type:
- Returns:
The response, or a 403 response when the client has exceeded the throttle limit.
- property formatted_class_name: str[source]
Return the formatted class name for logging.
It names the instance, not a request.
- increment_throttle(throttle_key)[source]
Add one to a client’s 404 counter in the cache, and reset its
THROTTLE_TIMEOUT.While the cache can’t be written, e.g. during a Redis outage, the 404 isn’t counted.
- Parameters:
throttle_key (
str) – The cache key of the client’s counter, fromget_throttle_key().- Return type:
- is_ignored_path(path)[source]
Determine whether 404s on this path should never count toward the throttle.
- log_404_event(request, client_ip, blocked_count)[source]
Log a 404 probe at DEBUG, sampled every
LOG_SAMPLE_RATEcounts to reduce spam during bot scans.- Parameters:
request (
HttpRequest) – The request.client_ip (
str) – The client’s ip address.blocked_count (
int) – The client’s 404 count before this one.
- Return type:
- process_response(request, response)[source]
Sync entry point: count anonymous 404s per client ip, and block excessive ones.
It does nothing unless the
ENABLE_MIDDLEWARE_EXCESSIVE_404waffle switch is active.- Parameters:
request (
HttpRequest) – The request.response (
HttpResponseBase) – The response from the view.
- Return type:
- Returns:
The response, or a 403 response when the client has exceeded the throttle limit.