Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions django_ratelimit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import socket
import time
import zlib
import json

from django.conf import settings
from django.core.cache import caches
Expand Down Expand Up @@ -207,6 +208,8 @@ def get_usage(request, group=None, fn=None, key=None, rate=None, method=ALL,
value = key(group, request)
elif key in _SIMPLE_KEYS:
value = _SIMPLE_KEYS[key](request)
elif 'params' in key:
value = json.dumps({**request.GET.dict(), **request.POST.dict()})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is advisable as built-in, advertised behavior of the library.

  • dict keys can be randomly ordered in Python, they are not consistently generated AFAIK
  • even so, how would you define ?a=1&b=1 vs ?b=1&a=1
  • easy to tamper with, since the rate limiting is based on user input
  • csrf tokens in form posts

Of course, you may have a great use case for wanting to do this 👍 But you can define your own callable: https://django-ratelimit.readthedocs.io/en/stable/keys.html#callable-values

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea is that based on parameters there is the ratelimit, from the doc is not clear how to achieve what I am doing with this PR.

I see your point about dict order and the code can be improved to sort the parameters always.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Mte90 I would solve it like this:

Write your own callable for your project (as described in docs)

Share your experience by adding an example in the docs that shows someone how to achieve what you needed.

elif ':' in key:
accessor, k = key.split(':', 1)
if accessor not in _ACCESSOR_KEYS:
Expand Down