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-middleware-global-request
Advanced tools
Django middleware that keep request instance for every thread.
Django middleware that keep request instance for every thread.
pip install django-middleware-global-request
Add django application django_middleware_global_request
to INSTALLED_APPS in pro/settings.py
:
INSTALLED_APPS = [
...
'django_middleware_global_request',
...
]
Add GlobalRequestMiddleware
to MIDDLEWARE in pro/settings.py
:
MIDDLEWARE = [
...
'django_middleware_global_request.middleware.GlobalRequestMiddleware',
...
]
Use get_request
to get the global request instance from a function in pro/models.py
:
from django.db import models
from django.conf import settings
from django_middleware_global_request import get_request
class Book(models.Model):
name = models.CharField(max_length=64)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True)
def __str__(self):
return self.name
def save(self, *args, **kwargs):
if not self.author:
request = get_request()
if request:
self.author = request.user
return super().save(*args, **kwargs)
Use with GlobalRequest(xxx): pass
to set the global request to a new value in NON-USER-REQUEST context, e.g. in management command
context or in python manage.py shell
context. Out of the with scope
, the global request instance will be reset to the value when entering into the with scope
.
import djclick as click
from django.contrib.auth import get_user_model
from django_middleware_global_request_example.models import Book
from django_middleware_global_request import GlobalRequest
@click.command()
@click.option("-n", "--number", type=int, default=10)
def create(number):
admin = get_user_model().objects.all()[0]
with GlobalRequest(user=admin):
for i in range(number):
book = Book(name="book{idx}".format(idx=i+1))
book.save()
print(i, book.name, book.author.username)
Use GlobalRequestStorage
to set the global request instance for current thread context. The global request instance will all time exists until you changed it by another value.
example.py
from django.contrib.auth import get_user_model
from django_middleware_global_request_example.models import Book
from django_middleware_global_request import GlobalRequestStorage
b1 = Book(name="b1")
b1.save()
print(b1, b1.author)
admin = get_user_model().objects.get(username="admin")
GlobalRequestStorage().set_user(admin)
b2 = Book(name="b2")
b2.save()
print(b2, b2.author)
b3 = Book(name="b3")
b3.save()
print(b3, b3.author)
example result in python3 manage.py shell context
test@test django-middleware-global-request % python3.9 manage.py shell
Python 3.9.13 (main, Jun 8 2022, 15:40:49)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.3.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import example
b1 None
b2 admin
b3 admin
GlobalRequest
and GlobalRequestStorage
to set the global request instance value for NON-USER-REQUEST context.app_middleware_requires
to module __init__.py to work with django-app-requires
.request
instance was not accessible during streaming generation interactions.FAQs
Django middleware that keep request instance for every thread.
We found that django-middleware-global-request 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.