pythonpython-3.xpyramidpylonspyramid-debug-toolbar

Pyramid debug toolbar serving static content over HTTP instead of HTTPS


On our test servers, we're using the Pyramid debug toolbar, however, it generates http:// links to static content (like its CSS and JavaScript files), while the rest of the content is served over HTTPS. This causes mixed content warnings, and it breaks all functionality. Is there a way to force it to generate HTTPS links?

I know it's possible to enable mixed content in Chrome, and this works, but it's not a feasible solution for the entire QA team.


Solution

  • There might be better/simpler ways to achieve this, but one thing you can do to achieve this add the _scheme='https' parameter to each call to request.static_url().

    For that you can of course edit pyramid/url.py, but you can also do this in your projects' __init__.py:

    from pyramid.url import URLMethodsMixin
    
    URLMethodsMixin.static_url_org = URLMethodsMixin.static_url  # backup of original
    
    def https_static_url(self, *args, **kw):
        kw['_scheme'] = 'https'  # add parameter forcing https
        return URLMethodsMixin.static_url_org(self, *args, **kw)  # call backup
    
    URLMethodsMixin.static_url = https_static_url  # replace original with backup
    

    Parameters for static_url works like route_url. From the documentation:

    Note that if _scheme is passed as https, and _port is not passed, the _port value is assumed to have been passed as 443. Likewise, if _scheme is passed as http and _port is not passed, the _port value is assumed to have been passed as 80. To avoid this behavior, always explicitly pass _port whenever you pass _scheme. Setting '_scheme' automatically forces port 443