设置 Access-Control-Allow-Origin 接受所有域存在哪些安全风险?

2023-04-18前端开发问题
156

本文介绍了设置 Access-Control-Allow-Origin 接受所有域存在哪些安全风险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我最近不得不将 Access-Control-Allow-Origin 设置为 * 以便能够进行跨子域 AJAX 调用.我觉得这可能是一个安全问题.如果我保留该设置,我会面临哪些风险?

I recently had to set Access-Control-Allow-Origin to * in order to be able to make cross-subdomain AJAX calls. I feel like this might be a security problem. What risks am I exposing myself to if I keep the setting?

推荐答案

Access-Control-Allow-Origin: * 可以完全安全地添加到任何资源中,除非该资源包含受标准凭据以外的东西保护的私有数据.标准凭据是 cookie、HTTP 基本身份验证和 TLS 客户端证书.

Access-Control-Allow-Origin: * is totally safe to add to any resource, unless that resource contains private data protected by something other than standard credentials. Standard credentials are cookies, HTTP basic auth, and TLS client certificates.

想象一下https://example.com/users-private-data,这可能会根据用户的登录状态暴露私人数据.此状态使用会话 cookie.将 Access-Control-Allow-Origin: * 添加到此资源是安全,因为此标头仅允许在没有 cookie 的请求和 cookie 的情况下访问响应需要获取私有数据.因此,不会泄露任何私人数据.

Imagine https://example.com/users-private-data, which may expose private data depending on the user's logged in state. This state uses a session cookie. It's safe to add Access-Control-Allow-Origin: * to this resource, as this header only allows access to the response if the request is made without cookies, and cookies are required to get the private data. As a result, no private data is leaked.

想象一下 https://intranet.example.com/company-private-data,它会公开公司的私人数据,但只有在公司的 wifi 网络上才能访问.将 Access-Control-Allow-Origin: * 添加到此资源是不安全,因为它使用标准凭据以外的其他方式进行保护.否则,错误的脚本可能会将您用作通往 Intranet 的隧道.

Imagine https://intranet.example.com/company-private-data, which exposes private company data, but this can only be accessed if you're on the company's wifi network. It's not safe to add Access-Control-Allow-Origin: * to this resource, as it's protected using something other than standard credentials. Otherwise, a bad script could use you as a tunnel to the intranet.

想象一下,如果用户在隐身窗口中访问资源,他们会看到什么.如果您对看到此内容的所有人(包括浏览器收到的源代码)感到满意,则可以安全地添加 Access-Control-Allow-Origin: *.

Imagine what a user would see if they accessed the resource in an incognito window. If you're happy with everyone seeing this content (including the source code the browser received), it's safe to add Access-Control-Allow-Origin: *.

这篇关于设置 Access-Control-Allow-Origin 接受所有域存在哪些安全风险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax
Ajax call in rails 3.2.3 with will_paginate gem(使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax)...
2024-04-20 前端开发问题
6

使用视图附加或嵌入节点添加表单
Attach or embed node add form with a view(使用视图附加或嵌入节点添加表单)...
2024-04-19 前端开发问题
9

Drupal:如何使用 AJAX POST 从 REST 服务提交 webfrom
Drupal: How to submit the webfrom from REST services using AJAX POST(Drupal:如何使用 AJAX POST 从 REST 服务提交 webfrom)...
2024-04-19 前端开发问题
5