Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Setting up CSP for embedding notebook (eg: in iframe) #886

Closed
satendrakumar opened this issue Dec 21, 2015 · 18 comments · Fixed by #1063
Closed

[docs] Setting up CSP for embedding notebook (eg: in iframe) #886

satendrakumar opened this issue Dec 21, 2015 · 18 comments · Fixed by #1063

Comments

@satendrakumar
Copy link

Hello @qmaxquique

I am also facing same problem(issues#284) with iframe. I have tried as you have mentioned "c.NotebookApp.tornado_settings". It didn't work.
I have tried both way:

without SSL getting error

[W 16:43:55.857 NotebookApp] Content security violation: {"csp-report":{
    "blocked-uri":"",
    "document-uri":"http://192.168.2.115:9999/tree",
    "original-policy":"frame-ancestors http://192.168.2.115:9999; report-uri http://192.168.2.115:9999/api/security/csp-report",
    "referrer":"http://192.168.2.115:9191/",
    "violated-directive":"frame-ancestors http://192.168.2.115:9999"
}}
using SSL getting error:
[W 16:42:20.882 NotebookApp] Content security violation: {"csp-report":{
    "blocked-uri":"",
    "document-uri":"https://192.168.2.115:9999/tree",
    "original-policy":"frame-ancestors https://192.168.2.115:9999; report-uri https://192.168.2.115:9999/api/security/csp-report",
    "referrer":"http://192.168.2.115:9191/",
    "violated-directive":"frame-ancestors https://192.168.2.115:9999"}}
@Carreau
Copy link
Member

Carreau commented Dec 21, 2015

please indicate which exact setting you used, and on which domain you are trying to host ?

It seem like you are trying to host on port 9191 and only allowing 9999.

@satendrakumar
Copy link
Author

Thanks @Carreau
Actually i am try on my local box for testing after that I will deploy on AWS EC2 node.
I have added these setting into /home/sky/.jupyter/jupyter_notebook_config.py file:

c.NotebookApp.port = 9999

c.NotebookApp.ip = '192.168.2.115'

c.NotebookApp.tornado_settings = { 'headers': { 'Content-Security-Policy': "frame-ancestors 'self' https://192.168.2.115:9999/" } }


@Carreau
Copy link
Member

Carreau commented Dec 21, 2015

"frame-ancestors 'self' https://192.168.2.115:9999/"

That's redundant, if the notebook port and app are 192.168.2.115 and 9999 self is 192.168.2.115:9999, what you want to put there is the website you would be embedding into.

@satendrakumar
Copy link
Author

yes, suppose my website URL is ec2-xx-xx-xxx-xx.compute-1.amazonaws.com and port is 9999.
So what would be configuration setting ?

@Carreau
Copy link
Member

Carreau commented Dec 21, 2015

Depends, let's say I have a notebook server that runs on ec2, and that i want to embed a notebook on https://blog.jupyter.org, it would likely look like:

c.NotebookApp.tornado_settings = { 'headers': { 'Content-Security-Policy': "frame-ancestors 'self' https://blog.jupyter.org/" } }

But I'm not an expert on that.

There seem to be an example in old docs that seem to be missing on new one.

@Carreau Carreau changed the title Can't use Notebook inside an iframe [docs] Setting up CSP for embedding notebook (eg: in iframe) Dec 21, 2015
@Carreau
Copy link
Member

Carreau commented Dec 21, 2015

Renamed issue to remind us to write better documentation on that, or make it easier to find.

@satendrakumar
Copy link
Author

Thank you very much @Carreau. It is working fine now.

@satendrakumar
Copy link
Author

@Carreau When creating a new notebook it will open into new browser(or new tab). Is this possible it will open in same browser(same tab) ?

@Carreau
Copy link
Member

Carreau commented Dec 21, 2015

Hum, likely yes, you need to inject JS (through custom.js) and change the value of require('base/js/namespace')._target which by default is _blank to one of the supported value that suit you the most.

@willingc
Copy link
Member

@Carreau Thanks for tagging this for documentation review 👍

@satendrakumar
Copy link
Author

Hello @Carreau

I am trying something like:
Created file ~/.jupyter/static/custom/custom.js

require(['/tree/js/notebooklist'], function(notebookList){

NotebookList.prototype = function(notebook){
  notebook.open("", "_self")
}

})

added path into jupyter_notebook_config.py

c.NotebookApp.extra_static_paths = ["static/custom/custom.js"]

But it didn't work. Any idea what is wrong here ?

@Carreau
Copy link
Member

Carreau commented Dec 22, 2015

That's definitively not the right way to do it, but it works:
Better would be to write an extension for /tree/ and enable it in the config.

~ $ cat ~/.jupyter/custom/custom.js
define(['base/js/namespace'], function(Jupyter){
    Jupyter._target = '_self';
})

@satendrakumar
Copy link
Author

@Carreau : Yes definitively, I was wrong. Now it is working. Thank you so much for your help.
Now I am going to close this issue.

@Carreau
Copy link
Member

Carreau commented Dec 22, 2015

Reopening to fix docs.

@Carreau Carreau reopened this Dec 22, 2015
@willingc willingc added this to the 4.2 milestone Dec 23, 2015
@echozdog
Copy link

echozdog commented Jun 6, 2016

@Carreau I used this setting to get the notebook login page to show up on an iframe on socrates-login1/jupyter

c.JupyterHub.tornado_settings = {'headers': {'Content-Security-Policy': "frame-ancestors 'self' http://socrates-login1/"} }

And it works for the login page. But once the logged in I get the same error for the user profile pages.

Refused to display 'http://socrates-login1:7800/user/psplinter/tree' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self'".

Do I need to set this for each user?
Here is how I'm running juptyerhub

source /opt/cray/anaconda3/bin/activate /opt/cray/jupyter/python3_venv
module load /opt/modulefiles/python/python3
/opt/cray/jupyter/python3_venv/bin/jupyterhub --no-ssl -f /etc/jupyterhub/jupyterhub_config.py &

@rcquantuniversity
Copy link

I faced the same issue and was debugging since 2 days. Finally this post saved me!
Thanks guys for this article.
Cheers!

@KOSSOKO
Copy link

KOSSOKO commented Nov 15, 2018

@Carreau after creating the custom.js, how did you specify your jupyter to use this file ? I create the custom.js in the forlder ~/.jupyter/custom/custom.js, restart my docker image, but nothing happened. Still have my notebook in a new tab.

@astrojuanlu
Copy link

I guess the docs still need updating?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants