简体   繁体   English

wordpress 随机数是如何工作的?

[英]How do wordpress nonces work?

I am trying to understand how wordpress nonces work in terms of security.我试图了解 wordpress nonces 在安全性方面的工作原理。 nonce is for a number used once, but according to wordpress, it can be valid up to 24 hours, which makes no sense, it could be used 9999 times during this period (from same client). nonce是一个使用一次的数字,但根据 wordpress,它可以在 24 小时内有效,这没有任何意义,在此期间它可以使用 9999 次(来自同一客户端)。

I thought that a wordpress nonce is really a number used once and that a nonce is valid only for one-time usage, but that's not the case.我认为 wordpress nonce 实际上是一个使用一次的数字,并且 nonce 仅对一次性使用有效,但事实并非如此。 I guess for a better security, a one-time usage number would be better, eg you have a commenting system and someone clicks on "reply" two times.我想为了更好的安全性,一次性使用号码会更好,例如你有一个评论系统,有人点击“回复”两次。 Instead of inserting the comment two times, it is being inserted one time, because of the one-time valid nonce (same one) given in the two requests.不是插入两次评论,而是插入一次,因为在两个请求中给出了一次有效的随机数(相同的随机数)。

Am I getting something wrong?我有什么问题吗? What is the purpose of those wordpress nonces?这些 wordpress 随机数的目的是什么?

You are right WordPress nonce is not an actual nonce.你是对的 WordPress 随机数不是一个实际的随机数。 It can be used multiple times during a period of time while nonce is valid (12-24 hours) check nonce tick function .在nonce有效期间(12-24小时) 检查nonce打勾功能,可以在一段时间内多次使用。 The main reason why you need to use nonce is to prevent CSRF (Cross Site Request Forgery) and malicious scripts.您需要使用 nonce 的主要原因是为了防止CSRF (跨站请求伪造)和恶意脚本。

But in all other security cases, you shouldn't rely on WP nonce.但在所有其他安全案例中,您不应该依赖 WP nonce。 Use current_user_can() to actually check what current user can and cannot do on your website.使用current_user_can()实际检查当前用户在您的网站上可以做什么和不可以做什么。 Check docs 检查文档

You are getting it kind of wrong.你弄错了。 Although it is true that nonce is valid up to 24 hours, after which it will expire and a new one will be generated, a nonce can be only used once.虽然 nonce 的有效期最长为 24 小时,但过期后会生成一个新的,但一个 nonce 只能使用一次。

Hence the name nonce - n umber only used once .因此, nonce - number 这个名字只使用了一次

Here is a website which will be able to help you more about understading the whole thing behind nonces - https://codex.wordpress.org/WordPress_Nonces这是一个网站,可以帮助您更多地了解 nonces 背后的整个事情 - https://codex.wordpress.org/WordPress_Nonces

Nonce can protect several types of attacks including CSRF, but do not provide protection against replay attacks because they are not checked for one time use. Nonce 可以保护包括 CSRF 在内的多种类型的攻击,但不能提供针对重放攻击的保护,因为它们不是一次性使用的。 That is why for authentication, authorization or any access control purpose we should not rely on nonce.这就是为什么对于身份验证、授权或任何访问控制目的,我们不应该依赖 nonce。

We can minimize nonce lifetime to any lower hour by adding filter to nonce_life hook我们可以通过向nonce_life钩子添加过滤器来将 nonce 生命周期最小化到任何较低的小时

 add_filter( 'nonce_life', function () { return 4 * HOUR_IN_SECONDS; } );

The example above will give you nonces that are valid for 2-4 hours.上面的示例将为您提供 2-4 小时有效的随机数。

As Wordpress nonce are not providing one time use, for better protection we can add role permission checking after the nonce verification由于 Wordpress nonce 不提供一次性使用,为了更好的保护,我们可以在 nonce 验证后添加角色权限检查

 if( current_user_can( 'edit_posts' ) ){
     // Write you action code here after verifying the actual user permission
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM