Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 28dc89e6 authored by Nicolas Gelot's avatar Nicolas Gelot
Browse files

Patch botdetection with API media rule

parent a3834922
Loading
Loading
Loading
Loading
+14 −1
Original line number Original line Diff line number Diff line
@@ -84,6 +84,12 @@ API_WONDOW = 3600
API_MAX = 4
API_MAX = 4
"""Maximum requests from one IP in the :py:obj:`API_WONDOW`"""
"""Maximum requests from one IP in the :py:obj:`API_WONDOW`"""


API_MEDIA_WONDOW = 60
"""Time (sec) before sliding window for API media requests (format != html) expires."""

API_MEDIA_MAX = 10
"""Maximum requests from one IP in the :py:obj:`API_MEDIA_WONDOW`"""

SUSPICIOUS_IP_WINDOW = 3600 * 24 * 30
SUSPICIOUS_IP_WINDOW = 3600 * 24 * 30
"""Time (sec) before sliding window for one suspicious IP expires."""
"""Time (sec) before sliding window for one suspicious IP expires."""


@@ -104,7 +110,14 @@ def filter_request(
        logger.debug("network %s is link-local -> not monitored by ip_limit method", network.compressed)
        logger.debug("network %s is link-local -> not monitored by ip_limit method", network.compressed)
        return None
        return None


    if request.args.get('format', 'html') != 'html':
    if request.args.get("format", "html") == "json" and request.args.get("categories", "general") in [
        "images",
        "videos",
    ]:
        c = incr_sliding_window(redis_client, 'ip_limit.API_MEDIA_WONDOW:' + network.compressed, API_MEDIA_WONDOW)
        if c > API_MEDIA_MAX:
            return too_many_requests(network, "too many request in API_MEDIA_WINDOW")
    elif request.args.get('format', 'html') != 'html':
        c = incr_sliding_window(redis_client, 'ip_limit.API_WONDOW:' + network.compressed, API_WONDOW)
        c = incr_sliding_window(redis_client, 'ip_limit.API_WONDOW:' + network.compressed, API_WONDOW)
        if c > API_MAX:
        if c > API_MAX:
            return too_many_requests(network, "too many request in API_WINDOW")
            return too_many_requests(network, "too many request in API_WINDOW")