Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add CSP header for Colab output frames (#2390)
Summary: This is required to use the new `google.colab.kernel.proxyPort` Colab feature (see Google-internal <http://b/130310433>). Test Plan: The included tests verify that the mechanism is implemented as intended. To verify that the mechanism actually works in Colab, create a Colab notebook with the following cells: ```python import werkzeug @werkzeug.Request.application def app(request): frame_ancestors = request.args.get("frame_ancestors") response = werkzeug.Response("Hello, %s!\n" % (frame_ancestors,)) response.headers["Content-Type"] = "text/html" if frame_ancestors is not None: response.headers["Content-Security-Policy"] = ( "frame-ancestors %s" % frame_ancestors ) return response ``` ```python import threading import werkzeug.serving if "server" in locals(): server.shutdown() server = werkzeug.serving.ThreadedWSGIServer("localhost", 2345, app) threading.Thread(target=server.serve_forever).start() ``` ``` !curl -i localhost:2345?frame_ancestors=foo ``` ```javascript %%javascript google.colab.kernel.proxyPort(2345).then((base) => { const ancestors = "https://*.googleusercontent.com https://*.google.com"; const url = new URL( "?frame_ancestors=" + encodeURIComponent(ancestors), base, ); const iframe = document.createElement("iframe"); iframe.src = url.toString(); document.body.appendChild(iframe); }); ``` Run the notebook and verify that the final output frame renders properly: ![Screenshot of output frame with intended “Hello” message][1] Note that when changing the `iframe.src` to just `base`, the iframe instead renders a “sad page”, and that a console error indicates that the culprit is `X-Frame-Options: sameorigin`. [1]: https://user-images.githubusercontent.com/4317806/60227895-cf530280-9845-11e9-93f0-cc5159b88e31.png wchargin-branch: colab-csp
- Loading branch information