我正在编写opencart项目,并使用本条款编写自定义apis。
它使用这段代码对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攻击?似乎它只是将访问控制允许源标头设置为请求来自的任何域。
发布于 2021-03-30 12:56:26
这并不能防止CSRF攻击,因为你允许所有的来源!这是同一篇文章
Access-Control-Allow-Origin: *您应该创建如下所示的可接受性列表,这将确保只为CORS授予列表中的那些内容。
Scheme、Domain和Port是比较重要的信息。在使用缺省值(如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');
}https://stackoverflow.com/questions/66871184
复制相似问题