浏览器的 Secure Context 安全上下文限制
source link: https://easeapi.com/blog/172-web-secure-context.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
浏览器的 Secure Context 安全上下文限制
原创 2023-04-10
主流浏览器都启用了Secure Context
,它禁止浏览器访问被认为是不安全的内容,防止不合法访问造成的信息泄漏和安全问题。
Secure Context
主流浏览器都启用了Secure Context
,即安全的上下文环境。Secure Context
规范定义了最低限度的认证和保密标准,一些 API 被限制只能在Secure Context
中运行。比如访问摄像头、获取用户密码凭证、Service Worker
等。Secure Context 规范约定:
- 访问本地资源时,地址为
http://127.0.0.1
、http://localhost
、http://*.localhost
和 file:// 的资源是安全的; - 访问非本地资源时,路径为 https:// 或 wss:// 协议的资源是安全的。
当浏览器访问不符合安全要求的资源时,可能会提示:The request client is not a secure context。
判断浏览器是否在安全上下文中
if (window.isSecureContext) {
//页面在安全上下文中
}
以Service Worker
的使用为例,Service Worker
扩充了浏览器的处理能力,允许浏览器充当一部分后台服务的角色,处理不需要和用户直接交互的任务。Service Worker
必须运行在安全的上下文环境中,只能通过localhost
、HTTPS
访问,否则navigator.serviceWorker
获取到值为空。
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/worker.js', {
scope: '/'
}).then((registration) => {
registration.showNotification('registration successful');
});
}
在测试环境关闭Secure Context
的限制
有时候为了方便测试,会直接把服务部署在HTTP环境中,Secure Context
的限制会让测试工作变得复杂,这个时候可以强制浏览器关闭Secure Context
限制。
Chrome
打开Chrome浏览器,在地址栏输入:
chrome://flags/#unsafely-treat-insecure-origin-as-secure
将 Insecure origins treated as secure
选择设置为 Enabled,并在输入框中输入不安全源(HTTP)的地址和端口号,多个地址以英文逗号分隔。Relaunch 后生效。
Microsoft Edge
打开Edge浏览器,在地址栏输入:
edge://flags/#unsafely-treat-insecure-origin-as-secure
同样将Insecure origins treated as secure
选择设置为“已启用”,并在输入框中输入不安全源(HTTP)的地址和端口号,多个地址以英文逗号分隔。
Firefox
在地址栏输入about:config
进入高级选项。Firefox浏览器将权限细分了,以启用Service Worker为例,找到dom.serviceWorkers.testing.enabled
并设置为true,就可以直接在HTTP环境使用Service Worker。
Safari
Safari浏览器限制更严格,据目前掌握的信息,它不支持取消Secure Context
的限制。
相关文章:
iOS 抓取网络数据包
iOS Crash log符号化
分享AppStore审核的一些经验教训
Let’s Encrypt acme.sh 泛域名证书
UTF-8 BOM EF BB BF 问题
您的电子邮箱地址不会被公开,必填项已用*标注。
留言内容 *
显示名称 *
电子邮箱地址 *
个人网站地址
在此浏览器中保存我的显示名称、邮箱地址和网站地址,以便下次留言时使用。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK