V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
bwangel
V2EX  ›  Python

关于 CSP 的一个问题

  •  
  •   bwangel ·
    bwangelme · 2016-03-23 15:11:06 +08:00 · 2938 次点击
    这是一个创建于 2960 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有个问题,半天解决不了,希望万能的 v 友们来帮助帮助俺!

    需求是这样的,在网站的一个页面中嵌入一个 iframe ,这个 iframe 指向另外一个网站(一个 jupyter 的 notebook )。在 safari 中访问没有问题,在 chrome 和 Firefox 中访问发现由于 CSP 的原因禁止。

    我找到了这个网站(content-security-policy)[http://content-security-policy.com/],然后发现child-src字段是 CSP Level2 的规范,被 Chrome 40+和 Firefox 45+支持,这个字段就是定义 iframe 允许的源。于是我在 jupyter notebook 的响应头中添加了child-src: '*',然后 Firefox 可以访问了,但是 Chrome 还不可以!

    我的 Chrome 版本是 49.0.

    Chrome 报的错误是:

    Refused to display 'http://127.0.0.1:8888/notebooks/2/2.ipynb' in a frame 
    because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self'".
    
    7 条回复    2016-03-23 15:56:27 +08:00
    bwangel
        1
    bwangel  
    OP
       2016-03-23 15:11:56 +08:00
    v2ex 没办法发图吗?想发个截图的。。。
    virusdefender
        2
    virusdefender  
       2016-03-23 15:22:04 +08:00
    frame-ancestors 是规定当前页面可以被哪些页面以 iframe,frame,object 等方式加载,所以你要看下 jupyter 是不是也有 CSP 策略。
    virusdefender
        3
    virusdefender  
       2016-03-23 15:22:57 +08:00
    而 child-src 是限制你的网站可以 iframe 的 url
    bwangel
        4
    bwangel  
    OP
       2016-03-23 15:41:49 +08:00
    我刚刚直接在 notebook 的设置里面,通过 tornado 的设置,直接添加了一个头

    ```
    frame-ancestors: '*';
    ```
    Chrome 还是不支持, notebook 应该已经设置了这个字段了,我去搜搜文档,看看有没有什么设置方法!
    bwangel
        5
    bwangel  
    OP
       2016-03-23 15:43:19 +08:00
    术语好像不大准确,应该叫做首部字段,不是头。。
    bwangel
        7
    bwangel  
    OP
       2016-03-23 15:56:27 +08:00
    坑爹啊,我照着 github 上它的文档设置了 frame-ancestors ,结果 chrome 和 Firefox 都不能访问了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   892 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:45 · PVG 06:45 · LAX 15:45 · JFK 18:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.