question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use CSP middleware to block online sources in DEBUG mode

See original GitHub issue

Summary

With KA Lite, we debugged some issues fetching online sources with JavaScript.

During this process, I had a theory that external content (fetched from our update/content server) was blocked due to inadequate security definitions. This wasn’t true, however a CSP middleware came out of it.

I made it so it only triggers in DEBUG=True mode. The advantage is that we can ensure that ALL resources fetched from online sources etc. are blocked while we are developing. Or explicitly allowed. That way, we can ensure that we don’t accidentally fetch an online source during testing or development that won’t actually be available in an offline context.

class CSPMiddleware:
    """
    Implements the Content-Security-Policy response header, which
    conforming user-agents can use to restrict the permitted sources
    of various content.
    See http://www.w3.org/TR/CSP/
    """

    def process_response(self, request, response):

        # Check for debug view
        status_code = response.status_code
        if not settings.DEBUG or status_code in (403, 404, 500):
            return response

        header = 'Content-Security-Policy'
        if getattr(settings, 'CSP_REPORT_ONLY', False):
            header += '-Report-Only'

        response[header] = "default-src 'self' 'unsafe-eval' 'unsafe-inline' data: *.learningequality.org; img-src data: *; script-src 'self' *.learningequality.org 'unsafe-eval' 'unsafe-inline'"
        return response

How to reproduce

  1. Use an external JS library that has an online-dependent behavior
  2. Don’t notice it during development or testing
  3. Be baffled when stuff breaks offline.

Real-life consequences (anything community should be aware of, for instance how it affects your deployment)

I think the threat is real, as we don’t necessary profile the networking behavior of all client-side browser scripts.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
jamalexcommented, Feb 8, 2017

we could also block eval and inline if we want to be even more strict

Would be interesting to see whether this is transitive into sandboxed iframes. We probably wouldn’t want to prevent them from doing inline JS or whatever they want (within the confines of the sandbox specifications), but we similarly would want to prevent them from trying to load online resources, as described in the initial proposal above.

1reaction
benjaomingcommented, Mar 27, 2020

A supplemental approach can be Zulip’s test suite that prohibits remote connections during tests:

https://zulip.readthedocs.io/en/latest/testing/testing.html#internet-access-inside-test-suites

Read more comments on GitHub >

github_iconTop Results From Across the Web

Loading of a resource blocked by Content Security Policy
Content Security Policy (CSP) is a mechanism to help prevent Cross-Site Scripting (XSS) and is best handled at server side; please note it ......
Read more >
Getting Started with Content Security Policy using Django
This package allows us to define the Content Security Policy in our Django settings and send it via a middleware that we added...
Read more >
Implementing Content Security Policy (CSP) in ASP.NET Core
A primary goal of CSP is to mitigate and report XSS attacks. XSS attacks exploit the browser's trust in the content received from...
Read more >
Implementing CSP and Trusted Types debugging in Chrome ...
Using this process, we learned that the source location was the most important piece of information for debugging CSP issues.
Read more >
Content Security Policy (CSP) for ASP.NET MVC
A much better policy would be to block everything by default and then only allow certain resources that you actually use as shown...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found