Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Django Channels JWT Middleware is a secure way to handle authentication for Django Channels WebSocket connections without directly exposing JWT tokens. Instead of sending the token itself in the query parameter, this middleware uses UUID-based authentication and cache-based user retrieval for enhanced security.
Enhanced Security: JWT tokens can potentially be intercepted if sent as query parameters. This middleware avoids sending tokens directly, minimizing the risk of token leakage.
UUID-based Authentication: This middleware generates UUIDs as tokens for WebSocket connections. These UUIDs are short-lived and act as temporary access keys. When a user connects, they provide the UUID, which is used to retrieve the authenticated user.
Cache-based User Retrieval: Upon connection, the middleware validates the UUID, retrieves the corresponding user ID from the cache, and fetches the user asynchronously. This ensures that the WebSocket connection is only established for authenticated users.
Sending tokens as query parameters can expose security vulnerabilities:
Install the package using pip:
pip install django-channels-jwt
Wrap your URLRouter
from django_channels_jwt.middlware import JwtAuthMiddlewareStack
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": JwtAuthMiddlewareStack(
URLRouter(
websocket_urlpatterns,
)
),
})
Include the provided URL for ticket generation in your project's urls.py
:
from django.urls import path, include
url_patterns = [
# ... your other URL patterns
path("api/auth/", include('django_channels_jwt.urls')
]
or if you want to set customized route
from django.urls import path, include
from django_channels_jwt.views import AsgiValidateTokenView
url_patterns = [
# ... your other URL patterns
path("auth_for_ws_connection/", AsgiValidateTokenView.as_view())
]
AsgiValidateTokenView
to generate a ticket (UUID) for WebSocket connections.ws://localhost:8001/ws/chat/?uuid=
FAQs
Secure JWT Auth Middleware for Django Channels
We found that django-channels-jwt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.