Skip to content

feat(python/django): add rule to detect missing SecurityMiddleware - #3804

Open
balaakasam wants to merge 2 commits into
semgrep:developfrom
balaakasam:feat/django-missing-security-middleware
Open

feat(python/django): add rule to detect missing SecurityMiddleware#3804
balaakasam wants to merge 2 commits into
semgrep:developfrom
balaakasam:feat/django-missing-security-middleware

Conversation

@balaakasam

Copy link
Copy Markdown

Summary

This PR adds a new Semgrep rule to detect when Django's SecurityMiddleware is missing from the MIDDLEWARE setting in Django applications.

Problem

Django's SecurityMiddleware provides several critical security protections:

  • HTTPS redirection (SECURE_SSL_REDIRECT)
  • HTTP Strict Transport Security (HSTS) headers
  • Content type sniffing protection (X-Content-Type-Options)
  • Cross-site scripting filter (X-XSS-Protection)

When SecurityMiddleware is absent, none of these protections are active, leaving web applications — particularly ecommerce applications handling payment and personal data - exposed to common web attacks and non-compliant with PCI-DSS requirements.

This misconfiguration is common and does not currently have a rule in the Semgrep registry.

Changes

  • Added missing-security-middleware.yaml - rule targeting Python/Django settings files where MIDDLEWARE list does not include SecurityMiddleware
  • Added missing-security-middleware.py - test file with positive case (missing middleware, should trigger) and negative case
    (middleware present, should not trigger)

References

@CLAassistant

CLAassistant commented Apr 6, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@balaakasam

Copy link
Copy Markdown
Author

Hello team, just checking in on this PR. Happy to make any if needed adjustments to get this to closure. Please let me know
if there is anything I can improve. Thank you!

@balaakasam

Copy link
Copy Markdown
Author

Hi, just following up. CLA is now signed and all checks are passing. Happy to make any adjustments if needed, waiting to hear back. Thanks you.

@balaakasam

Copy link
Copy Markdown
Author

Hi team, following up to see if anything else is needed from my end to get this going. Please let me know. Thank you.

1 similar comment
@balaakasam

Copy link
Copy Markdown
Author

Hi team, following up to see if anything else is needed from my end to get this going. Please let me know. Thank you.

@balaakasam

Copy link
Copy Markdown
Author

Hi team, following up to see if anything else is needed from my end to get this going as this has been open since a very long time. Please let me know. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants