22
33import asyncio
44import logging
5+ from typing import Any
56
67import aiohttp
78from bs4 import BeautifulSoup
@@ -44,14 +45,12 @@ def calculate_protection_level(self) -> str:
4445 ]
4546 )
4647
47- if protections == 0 :
48- return "none"
49- elif protections == 1 :
50- return "weak"
51- elif protections == 2 :
52- return "moderate"
53- else :
54- return "strong"
48+ protection_levels = {
49+ 0 : "none" ,
50+ 1 : "weak" ,
51+ 2 : "moderate" ,
52+ }
53+ return protection_levels .get (protections , "strong" )
5554
5655
5756class CSRFScanner :
@@ -134,7 +133,7 @@ async def _scan_page(self, url: str, session: aiohttp.ClientSession) -> list[Vul
134133
135134 return vulnerabilities
136135
137- def _analyze_form (self , form : BeautifulSoup , page_url : str ) -> CSRFTestCase :
136+ def _analyze_form (self , form : Any , page_url : str ) -> CSRFTestCase :
138137 """Analyze a form for CSRF protection using Pydantic validation.
139138
140139 Args:
@@ -165,8 +164,12 @@ def _analyze_form(self, form: BeautifulSoup, page_url: str) -> CSRFTestCase:
165164
166165 inputs = form .find_all (["input" , "textarea" , "select" ])
167166 for input_field in inputs :
168- field_name = input_field .get ("name" , "" )
169- field_value = input_field .get ("value" , "" )
167+ field_name_raw = input_field .get ("name" , "" )
168+ field_value_raw = input_field .get ("value" , "" )
169+
170+ # Extract string values
171+ field_name = field_name_raw if isinstance (field_name_raw , str ) else ""
172+ field_value = field_value_raw if isinstance (field_value_raw , str ) else ""
170173
171174 if field_name :
172175 fields [field_name ] = field_value
@@ -183,9 +186,9 @@ def _analyze_form(self, form: BeautifulSoup, page_url: str) -> CSRFTestCase:
183186 return CSRFTestCase (
184187 url = page_url ,
185188 form_action = form_action if form_action else None ,
186- form_method = form_method
187- if form_method in ["GET" , "POST" , "PUT" , "DELETE" , "PATCH" ]
188- else "POST" ,
189+ form_method = (
190+ form_method if form_method in ["GET" , "POST" , "PUT" , "DELETE" , "PATCH" ] else "POST"
191+ ) ,
189192 has_csrf_token = has_csrf ,
190193 token_field_name = csrf_token_field ,
191194 form_fields = fields ,
@@ -195,8 +198,8 @@ def _analyze_form(self, form: BeautifulSoup, page_url: str) -> CSRFTestCase:
195198 def _check_csrf_protection (
196199 self ,
197200 test_case : CSRFTestCase ,
198- headers : dict ,
199- cookies : dict ,
201+ headers : dict [ str , str ] ,
202+ cookies : dict [ str , Any ] ,
200203 ) -> CSRFProtectionCheck :
201204 """Check for various CSRF protection mechanisms.
202205
@@ -212,7 +215,7 @@ def _check_csrf_protection(
212215
213216 # Check for SameSite cookie attribute
214217 for cookie in cookies .values ():
215- if hasattr (cookie , "get" ) and cookie .get ("samesite" ):
218+ if hasattr (cookie , "get" ) and callable ( cookie . get ) and cookie .get ("samesite" ):
216219 protection .has_samesite_cookie = True
217220 break
218221
0 commit comments