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

add csrf tokens #60

Open
vjs22334 opened this issue Apr 11, 2019 · 5 comments
Open

add csrf tokens #60

vjs22334 opened this issue Apr 11, 2019 · 5 comments
Assignees
Labels
high priority High Priority Issue

Comments

@vjs22334
Copy link
Contributor

we need to include csrf tokens to all forms

@vjs22334
Copy link
Contributor Author

@pkkp86nitd have you finished this?

@ash1969
Copy link
Contributor

ash1969 commented Jul 30, 2020

@pkkp86nitd Was this done?

@pkkp86nitd
Copy link
Contributor

Tokens were already added there in all the forms .
We need to find out why CsrfMiddleware was giving error ( #'django.middleware.csrf.CsrfViewMiddleware', this is commented at line 52 in settings.py ).

@arin17bishwa arin17bishwa reopened this Dec 22, 2022
@arin17bishwa arin17bishwa added the high priority High Priority Issue label Dec 22, 2022
@arin17bishwa arin17bishwa assigned moinak878 and unassigned pkkp86nitd Dec 22, 2022
@harshitsurana
Copy link
Contributor

@moinak878: (Possible scenarios where getting an error)
If we uncomment django.middleware.csrf.CsrfViewMiddleware in settings.py and register a new user, error (Forbidden (CSRF token missing or incorrect.): /profile/register/username_check) is thrown while making an AJAX call.
Need to find how we can pass csrf token with data in the AJAX call.

@moinak878
Copy link
Member

@harshitsurana I found the following two methods to be working :-

  1. Remove django.middleware.csrf.CsrfViewMiddleware from settings.py and add @csrf_protect before the view

@csrf_protect
def user_register(request):
    if request.user.is_authenticated :
        id=request.user.id
        return HttpResponseRedirect(reverse('user_profile:view_profile', args=(id,)))
    form = SignUpForm(request.POST or None)
    if request.method == 'POST': .....
 
  1. Let django.middleware.csrf.CsrfViewMiddleware be present in settings.py and add the header : {'X-CSRFToken': csrftoken} , in the AJAX call
$("#id_username").keyup(function (event) {
      var res = $(this).serialize()
      $.ajax({
        method: "POST",
        url: "/profile/register/username_check",
        headers : {'X-CSRFToken': csrftoken},
        data: res,
        success: function (data) {.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority High Priority Issue
Projects
None yet
Development

No branches or pull requests

6 participants