【发布时间】:2016-11-15 02:43:29
【问题描述】:
我有一个应用程序,它使用 javascript 获取字符串并将其发布到服务器上的接收器 php 文件以进行进一步处理。接收者的工作是解析字符串,传递信息,并向 javascript 报告信息的进展情况。最近我尝试为整个 shebang 添加基于密码的安全性,但现在 receiver.php 传回一个空响应。
我发现,如果我在接收器中的任何位置调用 password_verify(无论我用它做什么......我什至可以不使用它就直接调用它)脚本中的以下回显不会运行 -这些回声中的任何 responseText 都是空的——我不知道为什么。不过,如果我只是从命令行运行 php 脚本,一切都会正常工作。
在继续之前,我要指出我对 Web 开发和基于密码的安全性还很陌生,所以不要太刻薄地歪曲我。不过,好消息 - 与我在网络上可以找到的所有其他问题不同,我得到了正确的哈希值和正确的验证响应。
使用 php 5.6.23
JS文件“test.html”的缩小版:
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript">
function reviewAndSubmit() {
//bigOlString is usually built as a result of TONS of script on this page, use this as a test
var bigOlString = "password=1234 name=test otherParams=barglebargle"
var postString = new XMLHttpRequest();
//build function to receive post response
postString.onreadystatechange = function() {
if (postString.readyState == 4) {
window.alert("responseText: " + postString.responseText)
}
}
postString.open("POST", "receivertest.php", true);
postString.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
postString.send(bigOlString);
}
</script>
</head>
<body>
<button class="button" id="pushMe" onClick="reviewAndSubmit()">Why does PHP hate me so bad?</button>
和receivertest.php:
<?php
//hashed version of '1234' and debug name 'test'
$debugPass = '$2y$10$b.08/4NfawKOwrBYJqguc.AWsI3mQiGGaz1eYvfc9Uid1auQKKABm';
$debugName = 'test';
//get incoming string
$inString = file_get_contents('php://input');
//get challenge content
$challengePass = substr($inString, strpos($inString, "password=") + 9, strpos($inString, "name=") - 10); //10 because of the space
$name = substr($inString, strpos($inString, "name=") +5, (strpos($inString, "otherParams=") - strpos($inString, "name=") - 6)); //ugly!
//begin authentication
$auth = False;
echo $name;
echo $challengePass;
password_verify($challengePass, $debugPass); //yes, I'm not doing anything with this. Doesn't matter.
echo "this line won't echo";
?>
如果您在receivertest.php 中注释掉'password_verify' 行,一切都会完美回响。如果你不这样做,那就不走运了——test.html 中的“警报”窗口只会吐出“responseText test123”。但是,如果您在控制台(php receivertest.php)中运行receivertest.php,一切都会正确回显。
为什么我的 responseText 是空的?
编辑:我编辑了 php 脚本以更好地说明问题。是的,我知道我没有使用 password_verify 进行任何操作。没关系。 'this line won't echo' 行不会像其他行一样在 test.html 中回显。 我的问题是:为什么不呢?
【问题讨论】:
-
你运行的是什么版本的 PHP?
password_verify出现在 PHP 5.5 中。如果您没有运行 PHP 5.5,那么您运行的是危险的旧版本、不受支持且因此不安全的 PHP 版本。 -
你似乎也没有做任何事情 with
password_verify。即使验证失败,您也在回显true。 -
if ($name == $debugName) { echo true;}总是不管password_verify()会发生什么 -
ceejoyz 和 RiggsFolly,请阅读帖子。我知道在这种情况下我没有对 password_verify 做任何事情。我也不在乎。我关心的是 responseText 是空的。如果这段代码总是回显“true”,我会很高兴,但它只在控制台中发生 - 它在 responsetext 中没有回显。
标签: javascript php html passwords