使用基于时间的旋转哈希或字符串来确保安全性

phi*_*bar 5 php security hash iframe

在CMS应用程序中,我偶尔需要打开另一个域的iframe.目前我将该iframe的网址设置为非常模糊的内容.喜欢http://domain.com/iframe/jhghjg34787386/.这有效但理论上iframe源URL将保存在用户的历史记录中,并且可以从外部世界访问.

所以,我想知道在请求端处理一个不断变化的哈希或字符串的基于时间的方法,并在iframe源端进行检查.但是我希望它是基于时间的.

我可以这样做得到我的哈希:

<?php 

   $seed = '123456789'; // a password that both the parent and source have
   $string = md5(time().$seed); 
?>
Run Code Online (Sandbox Code Playgroud)

但是这两台服务器必须完全同步.任何使时间约束更模糊的方法?

我也对其他方法持开放态度.有没有办法验证iframe的父窗口是否属于某个域?

Bea*_*eat 7

您可以向哈希添加密钥并使用查询发送时间戳,例如:

$key = "YOUR_SECRET_KEY";
$time = time();
$hash = hash_hmac('sha256', $time, $key);
$url = "https://example.com/iframe?hash=$hash&time=$time";
Run Code Online (Sandbox Code Playgroud)

另一方面,您应首先检查时间戳是否在限制范围内(例如,不超过五分钟),然后重新检查密钥和提交的时间戳.如果获得相同的哈希,则请求有效.

笔记: