Hello all,
I'm brand new to web development, and have chosen to do my first project with Play 2.6, so apologies for any glaring mistakes I may be making, and thank you for your help and patience.
I'm attempting to load some content from external sources, specifically I'm trying to use some image icons to appear in various places with my text on my app from
fontawesome.com (it loads through
<script src="https://use.fontawesome.com/3aed4d9ed5.js"></script> located in my header) and also a font from
fonts.googleapi.com as such:
<link href="https://fonts.googleapis.com/css?family=Ubuntu:400,500" rel="stylesheet">
I discovered that both were being blocked by the content security policy, and would not load with the rest of my site. So, after reading up on Play's documentation regarding
configuring security headers that I needed to add them to the configuration policy. So, I added this to the application.conf file:
play.filters.headers.contentSecurityPolicy += "; font-src 'self' .fonts.googleapis.com.; script-src 'self' .fontawesome.com."
(as a note, I have also tried not starting it with a semicolon as well)
However, when I now try to load my application, I get this error:
Cannot concatenate object or list with a non-object-or-list, Quoted("default-src 'self'") and SimpleConfigList(["; font-src 'self' ...
So I'm not sure why, but it won't concatenate the lists. So, instead, I just try to rebuild it entirely, adding this instead to my application.conf file:
play.filters.headers.contentSecurityPolicy = "default-src 'self'; font-src 'self' .fonts.googleapis.com.; script-src 'self' .fontawesome.com."
This allows the page to load at least, but fails to load anything, this lights up my console:
Content Security Policy: Couldn’t parse invalid host .fonts.googleapis.com. (unknown)
Content Security Policy: Couldn’t parse invalid host .fontawesome.com. (unknown)
Content Security Policy: The page’s settings blocked the loading of a resource at https://use.fontawesome.com/3aed4d9ed5.js (“script-src http://localhost:9000”). (unknown)
Content Security Policy: The page’s settings blocked the loading of a resource at https://fonts.googleapis.com/css?family=Ubuntu:400,500 (“default-src http://localhost:9000”). (unknown)
Content Security Policy: The page’s settings blocked the loading of a resource at self (“script-src http://localhost:9000”). Source: onclick attribute on BUTTON element. localhost:9000So it seems it's still blocking everything. Including even a button attribute I define on the page which just takes the app from the front page to its own login page.
However, I DID find something that works, kind of. I use allowed-action headers. So first, I need to enable them in my application.conf file:
play.filters.headers.allowActionSpecificHeaders = true
And then I need to add this specifically to
every action to make it work:
def index() = Action {
implicit request: Request[AnyContent] => Ok(views.html.index()).withHeaders(SecurityHeadersFilter
.CONTENT_SECURITY_POLICY_HEADER -> " .fontawesome.com .fonts.googleapis.com")
}And, after all that, everything does load fine. Though, it still says this in my console:
Content Security Policy: Couldn’t process unknown directive ‘.
fontawesome.com’ (unknown)
This works, but I'm grossly violating the DRY principle here by adding this to every action. I would think creating the list as I first attempted would work better, as it would be nice to have just one place to add (or remove) something should I need to load more externals in the future. I'm wondering if I'm just getting my syntax wrong. Any suggestions? Thanks in advance!