websocket创建失败_Javascript创建WebSocket连接被拒绝 - 内容安全

Trying to open a WebSocket connection from a Browser to a server running on localhost:9000 here is my JS code:

$( document ).ready(function() {

var url = "ws://localhost:9000/myapp";

var connection = new WebSocket(url);

connection.onopen = function() {

console.log('WebSocket Open');

};

connection.onerror = function(error) {

console.log('WebSocket Error ', error);

};

connection.onmessage = function(event) {

console.log('WebSocket Msg ', event);

}

});

But the browser is refusing to accept the connection due to Content-security policy:

Content Security Policy: The page's settings blocked the loading of a

resource at ws://localhost:9000/myapp ("default-src

http://localhost:9000").

I thought that openning a websocket connection to "self" in this case "localhost" would be acceptable but both Chrome and FF are denying the connection. I thought of placing

but it didn't fix the problem.

These are the headers being returned by the Server:

HTTP/1.1 200 OK

Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin

X-Frame-Options: DENY

X-XSS-Protection: 1; mode=block

X-Content-Type-Options: nosniff

Content-Security-Policy: default-src 'self'

X-Permitted-Cross-Domain-Policies: master-only

Date: Sat, 24 Jun 2017 03:39:10 GMT

Content-Type: text/html; charset=utf-8

Content-Length: 2130

What could be causing the connection refusal ?

解决方案

It seems like that page must be getting served with a Content-Security-Policy response header that has default-src http://localhost:9000 in its value.

Given that you can never use a CSP directive somewhere to apply a more-liberal policy than one applied from somewhere else, if you have a strict default-src http://localhost:9000 policy in the CSP header, it’ll be applied instead of any more-liberal policy you might have specified using a meta element in a document.

The impact is that adding additional policies to the list of policies to enforce can only further restrict the capabilities of the protected resource.

So I think you may need to change value of the Content-Security-Policy header to have default-src http: ws: connect-src ws:. You can’t do it with just a meta element.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值