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

Fix missing CSRF token for database operations #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
22 changes: 16 additions & 6 deletions App01/database_operations.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import json

from django.http import HttpResponse
import logging


from App01.db import check_insert_privileges, init_database
from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie
from django.http import JsonResponse

logger = logging.getLogger(__name__)


@csrf_exempt
def test_connection(request):
if request.method == "POST":
# Validate CSRF token
ensure_csrf_cookie(request) # Ensure CSRF cookie is set

json_str = request.body
json_dict = json.loads(json_str)
ip = json_dict.get('ip', None)
Expand All @@ -20,11 +22,17 @@ def test_connection(request):
password = json_dict.get('password', None)

result = check_insert_privileges(ip, port, database, username, password)
return HttpResponse(json.dumps(result))
return JsonResponse(result)
return JsonResponse({'error': 'Invalid request method'}, status=405)


@csrf_exempt
def initialize_database(request):
if request.method == "POST":
# Validate CSRF token
ensure_csrf_cookie(request) # Ensure CSRF cookie is set

# Proceed with processing the request
json_str = request.body
json_dict = json.loads(json_str)
ip = json_dict.get('ip', None)
Expand All @@ -37,4 +45,6 @@ def initialize_database(request):
require_ssl = json_dict.get('require_ssl', None)

result = init_database(ip, port, database, root_username, root_password, username, password, require_ssl)
return HttpResponse(json.dumps(result))
return JsonResponse(result)

return JsonResponse({'error': 'Invalid request method'}, status=405)