Skip to content

Commit b317860

Browse files
committed
Changed logging with client IP and maintained the securityHeaders
1 parent 0b9a51b commit b317860

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

jsweb/middleware.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33
from .static import serve_static
44
from .response import Forbidden
5-
import json
65

76
logger = logging.getLogger(__name__)
87

@@ -58,10 +57,10 @@ async def __call__(self, scope, receive, send):
5857
cookie_token = req.cookies.get("csrf_token")
5958
submitted_token = None
6059

61-
# 1. Check header first (Best practice for AJAX/APIs)
60+
6261
submitted_token = req.headers.get("x-csrf-token")
6362

64-
# 2. If no header token, check the body based on content type
63+
6564
if not submitted_token:
6665
content_type = req.headers.get("content-type", "")
6766

@@ -83,7 +82,7 @@ async def __call__(self, scope, receive, send):
8382
# If form parsing fails, we treat it as no token found
8483
pass
8584

86-
# 3. Perform the validation
85+
8786
# Both the cookie token and the submitted token MUST be present and match.
8887
if not cookie_token or not submitted_token or not secrets.compare_digest(submitted_token, cookie_token):
8988
# Log CSRF failure with context (but never log the actual tokens)

0 commit comments

Comments
 (0)