这个CORS处理程序安全吗? [英] Is this CORS handler safe?

查看:211
本文介绍了这个CORS处理程序安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个简单的方法来处理我的简单服务器代理中的CORS。

I wrote this trivial method to handle CORS in a simple server proxy of mine.

private void handleCors(HttpServletRequest req, HttpServletResponse resp) {
  final String origin = req.getHeader("Origin");
  if (Strings.isNullOrEmpty(origin)) {
    return;
  }
  if (!origin.startsWith("http://localhost:")) {
    return;
  }
  resp.setHeader("Access-Control-Allow-Origin", origin);
  resp.setHeader("Access-Control-Allow-Credentials", "true");
  resp.setHeader("Access-Control-Expose-Headers", "Authorization");
  resp.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
}

实际应用程序不需要它,它仅在手动测试时使用(带有 ionic serve )。我想,这是安全的,因为除了原点是localhost之外什么也不做,但比抱歉更安全。

It's not needed for the real application, it's only used when testing manually (with ionic serve). I guess, it is safe because of doing nothing except when the origin is localhost, but better safe than sorry.

此外,findbugs抱怨响应拆分漏洞。我应该只使用 URLEncoder.html #coding 或者还有更多内容吗?

Moreover, findbugs complains about response splitting vulnerability. Should I simply use URLEncoder.html#encode or is there more to it?

一般删除空格或者在包含空格的情况下不添加CORS头?

Would in general removing spaces or adding no CORS headers in case of contained spaces do?

推荐答案

CORS比JSONP等早期技术更安全,更灵活。

CORS is safer and more flexible than earlier techniques such as JSONP.

对于 GET 请求,WebAPI开箱即用。但是,一旦你开始使用它来进行 POST,PUT或DELETE 操作,然后CORS就会启动并放弃命中服务器的请求。 CORS会停止任何跨域请求,因此如果您的api在 www.myapi.com 运行,并且来自 www.mywebsite.com的请求进来,请求将被删除。这是一项安全功能,可确保来自未知域的请求无法访问服务器。

WebAPI works great straight out of the box for GET requests. However, once you start using it for POST, PUT or DELETE operations, then CORS kicks in and drops requests from hitting the server. CORS stops any cross domain requests so if your api is running at www.myapi.com and a request from www.mywebsite.com comes in, the request will be dropped. This is a security feature to ensure that requests from unknown domains cannot hit the server.

如果您使用Web客户端执行ajax调用,那还有一件事你需要添加到你的ajax调用,以确保所有浏览器上的CORS单词。

If you are using a web client to execute ajax calls, then there is one more thing you need to add to your ajax call to ensure that CORS words on all browsers.

$.support.cors = true
crossDomain: true



资源链接:



如何在WebAPI,旧学校实施跨域请求(CORS)?


但是在一行中,如果我们想说CORS处理程序不安全。
@zapl 已经提供了相关信息。

But in a single line, if we want to say then CORS handler is not safe. Already @zapl has given info about this.

现在我试图给你一些带有一些场景的攻击类型。希望它能为您提供明确的信息。

Now I am trying to give you some attack type with some scenerios. Hope it will give you clear information.



  1. CORS的不正确实现会产生一些安全问题,最常见的是在
    服务器头中使用通用允许符号(*)。

  2. 客户端不应该信任收到的内容完全和eval或
    渲染内容而不进行清理,这可能导致错误的
    信任。

  3. 允许CORS的应用程序可能容易受到CSRF $ b $的攻击b攻击。

  4. Preflight响应的长时间缓存可能导致因滥用预检客户端缓存而导致攻击

  5. 访问控制基于Origin标头的决策可能会导致
    漏洞,因为这可能会被攻击者欺骗。

  1. Several security issues arise from the improper implementation of CORS, most commonly using a universal allow notation (*) in the server headers.
  2. Clients should not trust the received content completely and eval or render content without sanitization which could result in misplaced trust.
  3. The application that allows CORS may become vulnerable to CSRF attacks.
  4. Prolonged caching of Preflight responses could lead to attacks arising out of abuse of the Preflight Client Cache.
  5. Access control decisions based on the Origin header could result in vulnerabilities as this can be spoofed by an attacker.



CORS安全 - 通用允许




  • 将Access-Control-Allow-Origin标题设置为*

  • 有效转动把内容变成了公共资源,允许从任何域访问


  • 场景:


    • 攻击者可以通过诱使用户访问,从已将此标头设置为*的Intranet网站窃取数据一个攻击者控制的网站在互联网上是

    • An attacker can steal data from an intranet site that has set this header to * by enticing a user to visit an attacker controlled site
      on the Internet.

    当受害者导航时,攻击者可以通过受害者的浏览器对其他远程应用程序进行攻击到攻击者控制的网站。

    An attacker can perform attacks on other remote apps via a victim’s browser when the victim navigates to an attacker controlled site.






    < h2> CORS安全 - 错位信托


    CORS Security – Misplaced Trust


    1. 两个域之间的数据交换基于信任

    2. 如果其中一个涉及数据交换的服务器是
      妥协,然后CORS模型面临风险




    方案:


    • 攻击者可能会破坏网站A并托管恶意内容站点B信任站点A通过CORS
      请求发送给站点B的数据,导致XSS和其他攻击。

    • 攻击者可以破坏站点B并使用暴露的CORS功能站点A攻击站点A中的用户。



    带CORS的CSRF



    CSRF with CORS


    1. 服务器可以处理客户端请求以更改服务器端数据,而
      验证Origin标头已设置

    2. 攻击者可以使用XHR的.withCredentials =true属性到
      重播受害者登录的应用程序的任何cookie
      in




    场景:


    • 攻击者设置原点标头或使用受信任的站点A向站点B发送非幂等请求。

    • 在查看受信任站点A时登录到站点B的受害者会导致站点B创建没有他的知识的用户帐户

      通过CSRF攻击。



    CORS - 预检应答的缓存



    CORS – Caching of Preflight responses


    1. Access-Control-Max-Age标头设置为较高的值,允许
      浏览器缓存Preflight响应。

    2. 将预检响应缓存更长时间会造成
      安全风险。

    3. 如果在服务器上更改了COR访问控制策略,则
      浏览器仍将遵循Preflight
      结果缓存中的旧策略。



    CORS - 基于Origin的访问控制



    CORS – Access Control based on Origin


    1. Origin标头表示请求来自特定的
      域名,但不保证。

    2. 如果基于此标题访问
      ,则欺骗Origin标题允许访问该页面




    场景:


    • 攻击者将Origin标头设置为view sensi受限制的信息

    • 攻击者使用cURL设置自定义原始标题:



         curl --header 'origin:http://someserver.com' http://myserver.com:90/demo/origin_spoof.php
    

    这是一个例子。您可以浏览以下链接:

    Here is an example is given. You can go through this link :


    1. https://www.owasp.org/index.php/Test_Cross_Origin_Resource_Sharing_(OTG-CLIENT-007)

    2. HTML5 CORS的一些安全影响或如何将浏览器用作
      代理

    1. https://www.owasp.org/index.php/Test_Cross_Origin_Resource_Sharing_(OTG-CLIENT-007)
    2. Some Security Impacts of HTML5 CORS or How to use a Browser as a Proxy

    这篇关于这个CORS处理程序安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆