Search code examples
cssdjangogoogle-app-engine

Django , Content Security Policy directive


I am trying to import font-awesome to my app using the following:

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

This returns the following error in the JS console:

Refused to load the stylesheet 'http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline'".

The Django app is a djangae (Django + GoogleAppEngine) example app which I am using as a starting point for what I want to make. https://github.com/davide-ceretti/googleappengine-djangae-blog.

The import happens in base.html. How can I fix this? I assume it's a setting, but I can't track it down.

p.s. I get the same error for another import:

<link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>

Solution

  • Add this to your settings.py:

    # Keep our policy as strict as possible
    CSP_DEFAULT_SRC = ("'none'",)
    CSP_STYLE_SRC = ("'self'", 'fonts.googleapis.com')
    CSP_SCRIPT_SRC = ("'self'",)
    CSP_FONT_SRC = ("'self'", 'fonts.gstatic.com')
    CSP_IMG_SRC = ("'self'",)
    

    And have a look at http://www.w3.org/TR/CSP/