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-oauth-toolkit-spa
Advanced tools
A Django app that facilitates authentication using cookie-based refresh token
Django-oauth-toolkit-cookie-refresh is a Django app to that provides REST authentication endpoints which uses refresh token in httpOnly cookie. It relies on Django REST framework and Django Oauth Toolkit.
The django-oauth-toolkit by default sends back access token and refresh token both in response body. This presents a dilemma for web developers as to where to store/persist each token:
HttpOnly
flag are not accessible by Javascript and therefore not vulnerable to XSS, however they may be
the target of CSRF attack because of ambient authority, where cookies may be attached to requests automatically. Even
though a malicious website carrying out a CSRF has no way of reading the response of the request which is made on behalf
of a user, they may be able to make changes to user data resources if such endpoints exist. This makes HttpOnly
cookies unsuited for storing access token. There are several ways to mitigate CSRF, such as setting the SameSite
attribute of a cookie to "Lax" or "Strict", and using anti-CSRF token. You can read more about CSRF here.In addition to various XSS and CSRF mitigation techniques, this package deploys access token and refresh token for web apps in a specific way that broadly hardens application security against these attacks:
HttpOnly
cookie header that the client browser sees but inaccessible
by your own frontend application. This way, the refresh token is not subject to any XSS attack against your application.
While CSRF is possible, the attacker cannot use this mechanism to make modification to your resources even is a CSRF
attack is successfully carried out. It is important to note that in CSRF, the attacker cannot read the response even
when they successfully make the malicious request to your API endpoint; the worst they can do is to refresh the token
on user's behalf, and no damage can be done. The refresh token cookie would also typically have domain
and path
attributes specified, so that browsers should only attach them with request to your domain and specific url path
used for refreshing the tokens, therefore reducing attack surfaces further.Install using pip:
pip install django-oauth-toolkit-spa
Or, install from source:
Set up django-oauth-toolkit and django REST framework if you haven't already:
INSTALLED_APPS = (
'django.contrib.admin',
...,
'oauth2_provider',
'rest_framework',
)
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
)
}
Include the oauth_toolkit_spa URLconf in your project urls.py:
path('auth/', include('oauth_toolkit_spa.urls')),
django-oauth-toolkit's settings are largely extended and used, except few default values have been overwritten. These settings are used as default unless explicitly specified:
"ACCESS_TOKEN_EXPIRE_SECONDS": 300,
"REFRESH_TOKEN_EXPIRE_SECONDS": 36000,
"REFRESH_COOKIE_NAME": "refresh_token",
"REFRESH_COOKIE_PATH": "/auth"
You can modify these settings by specifying them in the settings for django-oauth-toolkit:
OAUTH2_PROVIDER = {
...,
"ACCESS_TOKEN_EXPIRE_SECONDS": 300,
"REFRESH_TOKEN_EXPIRE_SECONDS": 36000,
"REFRESH_COOKIE_NAME": "refresh_token",
"REFRESH_COOKIE_PATH": "/auth",
...
}
If you want to use a different path for authentication than the default path, you should provide the setting
in REFRESH_COOKIE_PATH
, using a string with leading slash /
; while provide the same path in URLconf but with a
trailing slash /
.
FAQs
A Django app that facilitates authentication using cookie-based refresh token
We found that django-oauth-toolkit-spa 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.