推荐学习书目
› 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 · Mar 23, 2016 · 3724 views
    This topic created in 3861 days ago, the information mentioned may be changed or developed.

    有个问题,半天解决不了,希望万能的 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 replies  •  2016-03-23 15:56:27 +08:00
    bwangel
        1
    bwangel  
    OP
       Mar 23, 2016
    v2ex 没办法发图吗?想发个截图的。。。
    virusdefender
        2
    virusdefender  
       Mar 23, 2016
    frame-ancestors 是规定当前页面可以被哪些页面以 iframe,frame,object 等方式加载,所以你要看下 jupyter 是不是也有 CSP 策略。
    virusdefender
        3
    virusdefender  
       Mar 23, 2016
    而 child-src 是限制你的网站可以 iframe 的 url
    bwangel
        4
    bwangel  
    OP
       Mar 23, 2016
    我刚刚直接在 notebook 的设置里面,通过 tornado 的设置,直接添加了一个头

    ```
    frame-ancestors: '*';
    ```
    Chrome 还是不支持, notebook 应该已经设置了这个字段了,我去搜搜文档,看看有没有什么设置方法!
    bwangel
        5
    bwangel  
    OP
       Mar 23, 2016
    术语好像不大准确,应该叫做首部字段,不是头。。
    bwangel
        7
    bwangel  
    OP
       Mar 23, 2016
    坑爹啊,我照着 github 上它的文档设置了 frame-ancestors ,结果 chrome 和 Firefox 都不能访问了!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2754 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:25 · PVG 19:25 · LAX 04:25 · JFK 07:25
    ♥ Do have faith in what you're doing.