【问题标题】:how does this protect against csrf attacks?这如何防止 csrf 攻击?
【发布时间】:2021-06-26 11:37:26
【问题描述】:

我正在制作我的 opencart 项目并使用 This Article 编写自定义 api。

它使用此代码块对 csrf 攻击进行安全检查:

    if (isset($this->request->server['HTTP_ORIGIN'])) {
      $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
      $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
      $this->response->addHeader('Access-Control-Max-Age: 1000');
      $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    }

我的问题是如何根据文章防止 csrf 攻击?似乎它只是将 Access-Control-Allow-Origin 标头设置为请求来自的任何域

【问题讨论】:

    标签: php security cors csrf


    【解决方案1】:

    这根本不能防止 CSRF 攻击,因为您允许所有来源!和写法一样

    Access-Control-Allow-Origin: *
    

    您应该创建一个如下所示的接受列表,以确保只有列表中的那些被授予 CORS。

    SchemeDomainPort 是要比较的重要信息。端口可以​​省略,如果要使用默认值,例如 http=80 和 https=443。

    if(in_array($this->request->server['HTTP_ORIGIN'], [
        'http://xxx-domain.org',
        'https://example.org',
        'http://localhost:8888',
    ])) {
        $this->response->addHeader("Access-Control-Allow-Origin: {$this->request->server['HTTP_ORIGIN']}");
        $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
        $this->response->addHeader('Access-Control-Max-Age: 1000');
        $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-28
      • 2018-01-08
      • 2011-03-12
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多