索引页的设置PHP会话XSRF检查 [英] Setting PHP session on index page for XSRF check

查看:139
本文介绍了索引页的设置PHP会话XSRF检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在关于XSRF令牌运行到下面的问题。

客户端:AngularJS
服务器:PHP

当在index.php被击中,PHP生成一个XSRF令牌,并在会话中保存它。
一个cookie设置具有相同的值。

AngularJS读取cookie并存储的值。

在随后的帖子中,XSRF令牌形式发来的标题,这个想法是存储会话令牌比较发送的报头。

似乎一切都很好,没有任何问题。

但是:问题是,PHP无法读取的index.php注册的会话,因为在技术上没有出现过重新加载页面!如果我打了F5和重新加载一切,会话是很好的阅读。

我如何设置XSRF会话令牌上的index.php并使其可以从客户端的后续Ajax请求?我拉出在这一个... AP preciate反馈我的头发。

更新

更改会话标识符名称之后,一切突然的工作!

在index.php文件:

  //创建令牌,并设置会话
在session_start();
$令牌=散列('SHA256',uniqid(mt_rand(),真));
$ _SESSION ['X​​SRF'] = $令牌;

后来,也是在index.php文件:

  / *给令牌角客户* /
<脚本>
angular.module(应用程序)不变。(CSRF_TOKEN,'< = $ _ SESSION ['X​​SRF'];?>');
< / SCRIPT>

这是我使用的不是一个cookie注意,而不是我设置一个恒定的,然后提供给在角的方法。运行:

在角:

  angular.module(应用)。运行(['CSRF_TOKEN','$ HTTP',函数(CSRF_TOKEN,$ HTTP){   $ http.defaults.headers.common ['CSRF_TOKEN'] = CSRF_TOKEN;

到服务器的所有请求路由到一个共同的PHP文件。文件检查头被设置,并比较两个令牌:

  //只有POST请求进行检查(我不使用PUT / DELETE)
如果($ _ SERVER ['REQUEST_METHOD'] ==POST){
   在session_start();
   $ headerToken = $ _ SERVER ['HTTP_CSRF_TOKEN'];
   $ sessionToken = $ _SESSION ['X​​SRF'];
   如果($ headerToken!= $ sessionToken){
      标题(HTTP / 1.0 401未经授权');
      出口;
   }
}


解决方案

这是我在我的PHP / AngularJS项目正在做的:

的index.php

 在session_start();
如果(!使用isset($ _ SESSION ['X​​SRF-TOKEN'])){
    $ =唯一值的md5($ _ SERVER ['REMOTE_ADDR'] $ _ SERVER ['HTTP_USER_AGENT']); //添加更多/更少/任何独一无二的价值观,看评论
    $ _SESSION ['X​​SRF-TOKEN'] = SHA1(uniqid(microtime中()$唯一值,真实));
    的setcookie('XSRF-TOKEN',$ _SESSION ['X​​SRF-TOKEN']);
}

搜索结果:

这是AngularJS $ HTTP调用的任何脚本
(AngluarJS使用的cookie XSRF-TOKEN的价值,将在为X-XSRF-TOKEN自定义页眉每个请求发送的,所以我们需要比较这个值存储在会话中的值。)

 函数verifyXSRF(){    / *
    $头= apache_request_headers();
    $ headerToken =;
    的foreach($标题,如$头=> $值){
        如果($头==X-XSRF-TOKEN){
            $ headerToken = $价值;
            打破;
        }
    }
    * /    //更高效,看评论
    $ headerToken = $ _ SERVER ['HTTP_X_XSRF_TOKEN'];    如果($ headerToken = $ _SESSION ['X​​SRF-TOKEN']!)返回false;
    返回true;
}在session_start();
如果死(XSRF错误)(verifyXSRF()!);

反馈欢迎,因为我不知道究竟这是否足够保护XSRF

I have run in to the following problem regarding XSRF tokens.

Client: AngularJS Server: PHP

When the index.php is hit, PHP generates an XSRF token and saves it in a session. A cookie is set with same value.

AngularJS reads the cookie and stores the value.

On subsequent POSTS, the XSRF token is sent as a header, and the idea is to compare the stored session token to the sent header.

Everything seems fine, no problems whatsoever.

BUT: the issue is, that PHP cannot read the session registered in index.php, because technically there have been no page reloads! If I hit F5 and reloads everything , the session is read nicely.

How can I set the XSRF Session token on index.php and have it available for subsequent ajax requests from the client?? I'm pulling out my hair on this one... appreciate feedback.

UPDATE

After changing the session identifier name, everything suddenly worked!

In index.php:

// Create token and set session
session_start();
$token = hash('sha256', uniqid(mt_rand(), true));
$_SESSION['XSRF']=$token; 

Later, also in index.php:

/* Give token to Angular client */
<script>
angular.module("app").constant("CSRF_TOKEN", '<?=$_SESSION['XSRF'];?>'); 
</script>

Note that I'm not using a cookie, instead I set a constant which is then made available to the .run method in Angular:

in Angular:

angular.module('app').run(['CSRF_TOKEN','$http',function(CSRF_TOKEN,$http) {

   $http.defaults.headers.common['CSRF_TOKEN'] = CSRF_TOKEN;

All requests to the server are routed to one common php file. The file checks if the header is set, and compares the two tokens:

// Only POST requests are checked (I don't use PUT/DELETE)
if($_SERVER['REQUEST_METHOD']=="POST"){
   session_start();
   $headerToken = $_SERVER['HTTP_CSRF_TOKEN'];
   $sessionToken = $_SESSION['XSRF'];
   if($headerToken!=$sessionToken){
      header('HTTP/1.0 401 Unauthorized');
      exit;
   }
}

解决方案

This is what I'm doing in my PHP/AngularJS projects:

index.php

session_start();
if (!isset($_SESSION['XSRF-TOKEN'])) {
    $uniqueValues = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']); //add more/less/any "unique" values, see comments
    $_SESSION['XSRF-TOKEN'] = sha1(uniqid(microtime() . $uniqueValues, true));
    setcookie('XSRF-TOKEN', $_SESSION['XSRF-TOKEN']);
}

any script called by AngularJS $http:

(AngluarJS uses the value of the cookie XSRF-TOKEN and will send it in every request as X-XSRF-TOKEN custom header, so we need to compare this value to the value stored in the session.)

function verifyXSRF() {

    /*
    $headers = apache_request_headers();
    $headerToken = "";
    foreach ($headers as $header => $value) {
        if ($header == "X-XSRF-TOKEN") {
            $headerToken = $value;
            break;          
        }
    }
    */

    //more efficient, see comments
    $headerToken = $_SERVER['HTTP_X_XSRF_TOKEN'];

    if ($headerToken != $_SESSION['XSRF-TOKEN']) return false;
    return true;
}

session_start();
if (!verifyXSRF()) die("XSRF error");

Feedback welcome as I don't know exactly if this is enough XSRF protection.

这篇关于索引页的设置PHP会话XSRF检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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