惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 【当耐特】
The Cloudflare Blog
B
Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 叶小钗
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
A
About on SuperTechFans
雷峰网
雷峰网
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
浏览器同源策略 | 时间的朋友
2022-10-06 · via 时间的朋友

浏览器同源策略

Published: · LastMod: October 06, 2022 · 642 words

浏览器同源策略(Same-origin policy) 🔗

同源政策的目的,是为了保证用户信息的安全,防止恶意的网站窃取数据。

同源包括三个部分

  • protocol 协议
  • port 端口 (IE对于不同的port会视为同源)
  • host 主机名

image.png

https://domain-a.com:80/hannah-lin 为例

1
2
3
4
5
http://domain-a.com  不同源.scheme 不同
https://domain-a.com/mike  同源
https://news.domain-a.com  不同源.domain 不同
https://domain-a.com:81  不同源.port 不同
https://domain-b.com  不同源.domain 不同

非同源的行为限制

  • Cookie、LocalStorage 和 IndexDB 无法读取。
  • DOM 无法获得。
  • AJAX 请求不能发送。

两个网页一级域名相同,只是二级域名不同,浏览器允许通过设置相同的document.domain共享 Cookie。

1
document.domain = 'example.com';

这种方法只适用于 Cookie 和 iframe 窗口,LocalStorage 和 IndexDB 无法通过这种方法,规避同源政策

服务器在设置cookie时也可以设置一级域名

1
Set-Cookie: key=value; domain=.exaple.com;

二级域名和三级域名不需要设置,都可以读取这个Cookie

window.postMessage 🔗

html5中对于夸文档通信,新增了一个postMessage方法

父窗口 -> 子窗口

1
2
var popup = window.open('http://bbb.com', 'title');
popup.postMessage('Hello World!', 'http://bbb.com');

子窗口 -> 父窗口

1
window.opener.postMessage('Nice to see you', 'http://aaa.com');

各自页面中也都可以通过监听message方法获取数据

1
2
3
window.addEventListener('message', function(e) {
  console.log(e.data);
},false);

JSONP 🔗

通过动态添加script元素, 服务器收到请求后,将数据放在一个指定名字的回调函数里传回来。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function addScriptTag(src) {
  var script = document.createElement('script');
  script.setAttribute("type","text/javascript");
  script.src = src;
  document.body.appendChild(script);
}

window.onload = function () {
  addScriptTag('http://example.com/ip?callback=foo');
}

function foo(data) {
  console.log('Your public IP address is: ' + data.ip);
};

Websocket 🔗

WebSocket是一种通信协议,使用ws://(非加密)和wss://(加密)作为协议前缀。

WebSocket通过请求头中的Origin字段判断是否允许请求