|
| 1 | +import json |
| 2 | + |
| 3 | +from codemodder.codemods.test import BaseSASTCodemodTest |
| 4 | +from core_codemods.semgrep.semgrep_no_csrf_exempt import SemgrepNoCsrfExempt |
| 5 | + |
| 6 | + |
| 7 | +class TestSemgrepNoCsrfExempt(BaseSASTCodemodTest): |
| 8 | + codemod = SemgrepNoCsrfExempt |
| 9 | + tool = "semgrep" |
| 10 | + |
| 11 | + def test_name(self): |
| 12 | + assert self.codemod.name == "no-csrf-exempt" |
| 13 | + |
| 14 | + def test_decorators(self, tmpdir): |
| 15 | + input_code = """\ |
| 16 | + from django.http import JsonResponse |
| 17 | + from django.views.decorators.csrf import csrf_exempt |
| 18 | + from django.dispatch import receiver |
| 19 | + from django.core.signals import request_finished |
| 20 | + |
| 21 | + @csrf_exempt |
| 22 | + def ssrf_code_checker(request): |
| 23 | + if request.user.is_authenticated: |
| 24 | + if request.method == 'POST': |
| 25 | + return JsonResponse({'message': 'Testbench failed'}, status=200) |
| 26 | + return JsonResponse({'message': 'UnAuthenticated User'}, status=401) |
| 27 | + |
| 28 | + |
| 29 | + @receiver(request_finished) |
| 30 | + @csrf_exempt |
| 31 | + def foo(): |
| 32 | + pass |
| 33 | + """ |
| 34 | + expected_output = """\ |
| 35 | + from django.http import JsonResponse |
| 36 | + from django.views.decorators.csrf import csrf_exempt |
| 37 | + from django.dispatch import receiver |
| 38 | + from django.core.signals import request_finished |
| 39 | + |
| 40 | + def ssrf_code_checker(request): |
| 41 | + if request.user.is_authenticated: |
| 42 | + if request.method == 'POST': |
| 43 | + return JsonResponse({'message': 'Testbench failed'}, status=200) |
| 44 | + return JsonResponse({'message': 'UnAuthenticated User'}, status=401) |
| 45 | + |
| 46 | + |
| 47 | + @receiver(request_finished) |
| 48 | + def foo(): |
| 49 | + pass |
| 50 | + """ |
| 51 | + |
| 52 | + results = { |
| 53 | + "runs": [ |
| 54 | + { |
| 55 | + "results": [ |
| 56 | + { |
| 57 | + "fingerprints": {"matchBasedId/v1": "a3ca2"}, |
| 58 | + "locations": [ |
| 59 | + { |
| 60 | + "physicalLocation": { |
| 61 | + "artifactLocation": { |
| 62 | + "uri": "code.py", |
| 63 | + "uriBaseId": "%SRCROOT%", |
| 64 | + }, |
| 65 | + "region": { |
| 66 | + "endColumn": 73, |
| 67 | + "endLine": 11, |
| 68 | + "snippet": { |
| 69 | + "text": "@csrf_exempt\ndef ssrf_code_checker(request):\n if request.user.is_authenticated:\n if request.method == 'POST':\n return JsonResponse({'message': 'Testbench failed'}, status=200)\n return JsonResponse({'message': 'UnAuthenticated User'}, status=401)" |
| 70 | + }, |
| 71 | + "startColumn": 1, |
| 72 | + "startLine": 6, |
| 73 | + }, |
| 74 | + } |
| 75 | + } |
| 76 | + ], |
| 77 | + "message": { |
| 78 | + "text": "Detected usage of @csrf_exempt, which indicates that there is no CSRF token set for this route. This could lead to an attacker manipulating the user's account and exfiltration of private data. Instead, create a function without this decorator." |
| 79 | + }, |
| 80 | + "ruleId": "python.django.security.audit.csrf-exempt.no-csrf-exempt", |
| 81 | + }, |
| 82 | + { |
| 83 | + "fingerprints": {"matchBasedId/v1": "1cc62"}, |
| 84 | + "locations": [ |
| 85 | + { |
| 86 | + "physicalLocation": { |
| 87 | + "artifactLocation": { |
| 88 | + "uri": "code.py", |
| 89 | + "uriBaseId": "%SRCROOT%", |
| 90 | + }, |
| 91 | + "region": { |
| 92 | + "endColumn": 9, |
| 93 | + "endLine": 17, |
| 94 | + "snippet": { |
| 95 | + "text": "@receiver(request_finished)\n@csrf_exempt\ndef foo():\n pass" |
| 96 | + }, |
| 97 | + "startColumn": 1, |
| 98 | + "startLine": 14, |
| 99 | + }, |
| 100 | + } |
| 101 | + } |
| 102 | + ], |
| 103 | + "message": { |
| 104 | + "text": "Detected usage of @csrf_exempt, which indicates that there is no CSRF token set for this route. This could lead to an attacker manipulating the user's account and exfiltration of private data. Instead, create a function without this decorator." |
| 105 | + }, |
| 106 | + "ruleId": "python.django.security.audit.csrf-exempt.no-csrf-exempt", |
| 107 | + }, |
| 108 | + ], |
| 109 | + } |
| 110 | + ] |
| 111 | + } |
| 112 | + self.run_and_assert( |
| 113 | + tmpdir, |
| 114 | + input_code, |
| 115 | + expected_output, |
| 116 | + results=json.dumps(results), |
| 117 | + num_changes=2, |
| 118 | + ) |
0 commit comments