Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content Security Policy Violation on File Preview

This is quite odd, and I've checked around and attempted many things to no avail.

I have a simple file upload, it displays the image prior to uploading it. This works fine in my local development environment, and it also works fine on the staging server.

On the production server, it doesn't work. This is what is showing in my console:

refused to load the image 'blob:https://example.com/a7ced718-483b-4bc2-9db0-f012c8c6af5a' because it violates the following Content Security Policy directive: "img-src 'self' data:

However, I have no CSP defined, and it works fine on an exact replica server utilized for staging to test for bugs.

Furthermore, I've tried to add a CSP in the meta tag at the head of the document, but it does not solve the problem either. This is the CSP I attempted to implement:

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; img-src https://*; child-src 'none';">

Can anyone shed some light into why this may not be working?

like image 659
Ohgodwhy Avatar asked Mar 31 '17 20:03

Ohgodwhy


People also ask

How do you fix a CSP violation?

For the CSP violation error, here are a few things you could try, to fix: -Check if you have any plugins on the browser. -Try clearing the cache of the browser you are using. -Try logging in from a different browser.

How do I know if CSP is enabled?

Once the page source is shown, find out whether a CSP is present in a meta tag. Conduct a find (Ctrl-F on Windows, Cmd-F on Mac) and search for the term “Content-Security-Policy”. If “Content-Security-Policy” is found, the CSP will be the code that comes after that term.

How do I fix the Content-Security-Policy of your site blocks the use of eval in JavaScript?

The Content Security Policy (CSP) prevents the evaluation of arbitrary strings as JavaScript to make it more difficult for an attacker to inject unauthorized code on your site. To solve this issue, avoid using eval() , new Function() , setTimeout([string], ...) and setInterval([string], ...) for evaluating strings.


1 Answers

Check the response headers the server sends (using browser devtools or curl or whatever).

The production server must be sending a Content-Security-Policy response header.

If so, the reason your meta element has no effect is, the browser uses the most-restrictive CSP policy, wherever it’s specified—and the Content-Security-Policy header your production server is sending is more restrictive than the one you’re specifying with the meta element.

See the details on multiple polices at https://w3c.github.io/webappsec-csp/#multiple-policies and the details on the meta element at https://w3c.github.io/webappsec-csp/#meta-element —

A policy specified via a meta element will be enforced along with any other policies active for the protected resource, regardless of where they’re specified. The general impact of enforcing multiple policies is described in §8.1 The effect of multiple policies.

8.1. The effect of multiple policies

The impact is that adding additional policies to the list of policies to enforce can only further restrict the capabilities of the protected resource.

So it seems that the Content-Security-Policy header your production server is sending has img-src 'self' data: but in order for your image URL to be allowed and to avoid the error you’re seeing, the Content-Security-Policy header your production server is sending needs to at least also include blob: in its source list: img-src 'self' data: blob:.

like image 195
sideshowbarker Avatar answered Sep 20 '22 12:09

sideshowbarker