Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow certain URLs to be exempted from 'strict-dynamic' #95

Closed
mark-adams opened this issue Mar 16, 2018 · 4 comments
Closed

Allow certain URLs to be exempted from 'strict-dynamic' #95

mark-adams opened this issue Mar 16, 2018 · 4 comments

Comments

@mark-adams
Copy link

In a Django application, there are often certain views that are not controlled by the developer (such as the Django admin). For those of us using 'strict-dynamic' in our CSP policies, the only real option currently is to whitelist these URLs (which is very undesirable).

It is also common to have fallback CSP policies for when a user agent doesn't support strict-dynamic. An example policy would be 'strict-dynamic' 'unsafe-inline' 'self' 'http:' 'https'. With this policy, a CSP 3.x compatible browser would ignore unsafe-inline, self, http:, and https: and only obey strict-dynamic while older browsers would ignore strict-dynamic and follow the policy in the other directives.

It would be nice to define some sort of settings.CSP_BYPASS_STRICT_DYNAMIC value that would exclude views from having strict-dynamic applied to their CSP policy while leaving the other fallback directives alone. That way, views that the developer does not control (like Django admin) would have at least some benefit from CSP.

@g-k
Copy link
Contributor

g-k commented Mar 7, 2019

@mark-adams Thanks for raising this! If I'm understanding this correctly, you want to set a different CSP policy (including or excluding the 'strict-dynamic' script-src) for views you didn't write instead of excluding them from the CSP header entirely.

Possible options:

# urls.py
from django.contrib import admin
from django.urls import path

from csp.decorators import csp_replace

urlpatterns = [
    path('admin/', csp_replace(SCRIPT_SRC="'self'")(admin.site.urls),
]

Am I understanding the problem correctly? Would any of those options work?

@g-k
Copy link
Contributor

g-k commented Mar 11, 2019

Closing since this is really old and I think I addressed it in my last comment, but happy to revisit later.

@g-k g-k closed this as completed Mar 11, 2019
@anuj9196
Copy link

anuj9196 commented Sep 1, 2020

csp_replace in URL is not working.

@programanX
Copy link

I also stumbled on this.

Since admin.site.urls is a tupple, not a callable, you have to treat it like include.
So, install django-decorator-include and try below:

from csp.decorators import csp_replace
from decorator_include import decorator_include

urlpatterns = [
    path("admin/", decorator_include(csp_replace(SCRIPT_SRC="'self'"), admin.site.urls)),
    ...

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

No branches or pull requests

4 participants