有时,我在Chrome Devtools中看到以下警告:You should consider switching your application to a secure origin, such as HTTPS.
这是怎么回事,例如HTTPS?除了HTTPS之外,是否可以为网站提供“安全来源”?
我使用https没问题(我正在使用的所有网站上都启用了https)。这个问题纯粹出于好奇。
最佳答案
简短答案:是的,localhost
是安全的来源。 Chrome浏览器还有一个命令行标志,用于将指定的HTTP端点视为安全:--unsafely-treat-insecure-origin-as-secure=http://a.test,http://b.test
。因此,不仅是“HTTPS”。
更长的答案:诸如blob:
,wss:
和chrome-extension:
之类的其他方案也可以视为安全上下文。 about:blank
是一个常见示例,可能会有所不同,因为浏览器必须记住它是如何到达那里的。指向HTTPS页面但嵌入在HTTP页面中的iframe
并不安全。
要确定浏览器如何看待所有这些,请检查window.isSecureContext
的值。规范在这里:https://w3c.github.io/webappsec-secure-contexts/
这个 Chrome 页面https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins提供了一些上下文并链接到上面的规范。
例如,data:
URL是不安全的,但是blob:
URL是安全的。
const src = "<script>document.write(isSecureContext)</scr"+"ipt>";
a.src = `data:text/html,${src}`;
b.src = URL.createObjectURL(
new Blob([src], {type:'text/html'}));
<p>data url is insecure
<iframe id=a width=100 height=25></iframe>
<p>blob url is secure
<iframe id=b width=100 height=25></iframe>
关于google-chrome - "Secure origin"不是HTTPS吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51958643/