Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project(Task) : Implement Rate Limiting for Issue Creation #1791

Merged
merged 8 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions blt/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@
# 'LOCATION': 'cache_table',
# }
# }


REST_AUTH = {
'SESSION_LOGIN': False
}
Expand Down
17 changes: 17 additions & 0 deletions website/migrations/0082_issue_reporter_ip_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.8 on 2024-02-17 19:32

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("website", "0081_userprofile_issue_downvoted"),
]

operations = [
migrations.AddField(
model_name="issue",
name="reporter_ip_address",
field=models.GenericIPAddressField(blank=True, null=True),
),
]
1 change: 1 addition & 0 deletions website/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class Issue(models.Model):
modified = models.DateTimeField(auto_now=True)
is_hidden = models.BooleanField(default=False)
rewarded = models.PositiveIntegerField(default=0) # money rewarded by the company
reporter_ip_address = models.GenericIPAddressField(null=True, blank=True)


def __unicode__(self):
Expand Down
21 changes: 21 additions & 0 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
from django.core.validators import URLValidator
from django.core.exceptions import ValidationError
from django.http import HttpRequest
from django.utils.timezone import now

def is_valid_https_url(url):
validate = URLValidator(schemes=['https']) # Only allow HTTPS URLs
Expand Down Expand Up @@ -626,6 +627,14 @@ def process_issue(self, user, obj, created, domain, tokenauth=False, score=3):
)

return HttpResponseRedirect("/")
def get_client_ip(request):
"""Extract the client's IP address from the request."""
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip

class IssueCreate(IssueBaseCreate, CreateView):
model = Issue
Expand Down Expand Up @@ -736,6 +745,18 @@ def post(self, request, *args, **kwargs):
return super().post(request, *args, **kwargs)

def form_valid(self, form):
reporter_ip = get_client_ip(self.request)
form.instance.reporter_ip_address = reporter_ip

#implement rate limit
limit = 50 if self.request.user.is_authenticated else 30
today = now().date()
recent_issues_count = Issue.objects.filter(reporter_ip_address=reporter_ip, created__date=today).count()

if recent_issues_count >= limit:
messages.error(self.request, "You have reached your issue creation limit for today.")
return HttpResponseRedirect("/report/")
form.instance.reporter_ip_address = reporter_ip

@atomic
def create_issue(self,form):
Expand Down
Loading