【问题标题】:Nonce check is failing随机数检查失败
【发布时间】:2019-12-02 03:48:50
【问题描述】:

使用更新的代码进行编辑:仍然没有解决方案

谁能发现这段代码中的错误?

custom-page.php:

<form name="customForm">
                <?php wp_nonce_field('code_check', 'codecheck'); ?>
                Validation Code:<br>
                <input type="password" name="inputcode" id="inputcode" maxlength="6" inputmode="numeric">
                <input type="text" name="message" id="message" style="display:none; background-color: #FFCCCC;"><br>
                <input type="button" name="submitbutton" value="Submit" onClick="customfunction()">
                </form>

custom.js:

function customfunction() {
const userInput = document.addStamp.inputcode.value;
const token = document.addStamp.codecheck.value;
    fetch(`http://...../wp-json/api/v1/custom?code=${userInput}&token=${token}`).then(r => r.json()).then(data => {
......

API 文件.php:

public function custom($request)
    {
    $params = $request->get_params();
        $retrieved_nonce = $params[token];
        if($retrieved_nonce) {
            if (!wp_verify_nonce($retrieved_nonce, 'code_check' ) ) die( 'Failed security check' );
        }
        ....

在我将 nonce 验证码添加到 api 请求之前,一切正常。

现在当我点击“提交”按钮时,它没有提交,我进入控制台:

Uncaught (in promise) SyntaxError: Unexpected token F in JSON at position 0

所以它失败了,因为“F”是失败消息的第 0 点。

但是,如果我输出“$retrived_nonce”,我实际上会得到我的页面源代码中显示的 nonce 值,所以看起来它正在到达端点?

我已尝试注销并重新登录,但没有任何变化。

我的代码设置有误吗?

【问题讨论】:

  • 这真的很奇怪。因此,如下所示,我现在已将 nonce 值包含在端点 fetch 调用中。然后我可以输出该值,它与 nonce 值匹配。然而,如果我激活 !wp_verify_nonce 检查是否总是失败。

标签: wordpress api nonce


【解决方案1】:

你可以试试。

表格。

<form name="customForm" method="post">
<?php wp_nonce_field('code_check', 'code_check'); ?>
Validation Code:<br>
<input type="password" name="inputcode" id="inputcode" maxlength="6" inputmode="numeric">
<input type="text" name="message" id="message" style="display:none; background-color: #FFCCCC;"><br>
<input type="button" name="submitbutton" value="Submit" onClick="customfunction()">
</form>

在 customfunction 函数中还发送 wp_nonce_field 字段值,如。

function customfunction() {
const userInput = document.customForm.inputcode.value;
const code_check = document.customForm.code_check.value;
    fetch('http://...../wp-json/api/v1/custom?code='+userInput+'&code_check'+code_check).then(r => r.json()).then(data => {

现在验证 wp_nonce_field 字段值

public function custom($request)
        {
         $retrieved_nonce = $request['code_check'];
         if (!wp_verify_nonce($retrieved_nonce, 'code_check' ) ) die( 'Failed 
          security check' );

         /***
         you can also try
        if ( isset( $request['code_check'] ) || wp_verify_nonce( $request['code_check'], 'code_check' ) )  
         *******/
 }

【讨论】:

  • 感谢您的回复。恐怕结果一样。
  • 我尝试了相同的代码它正在工作。你是如何提交表格的?
  • 表单没有“真正”提交。当用户单击提交按钮时,输入代码字段中的条目由“function customfunction() {”使用,然后将其发送到我的端点进行处理/验证。函数自定义函数(){
  • 您没有在请求中发送 wp_verify_nonce 字段值。你只发送输入字段值。
  • 嗯,我添加了 const token = document.addStamp.code-check.value;到 js 函数并在 fetch 调用中传递令牌。然后在我的端点中添加: $retrieved_nonce = $params[token]; if (!wp_verify_nonce($retrieved_nonce, 'code_check') ) die('安全检查失败');结果还是一样....:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2013-03-17
  • 1970-01-01
  • 2015-08-04
相关资源
最近更新 更多