简体   繁体   English

reCAPTCHA v3网络密集型Web应用程序

[英]reCAPTCHA v3 network intensive web applications

I'm using Google's reCAPTCHA v3 in an Angular 2 application to protect from automatic form submission. 我在Angular 2应用程序中使用Google的reCAPTCHA v3来防止自动表单提交。 My application makes many network calls in the background as users' interact with the UI. 当用户与UI交互时,我的应用程序在后台进行许多网络调用。

From index.html , I make an intentionally blocking call to load the library (preventing the Angular world from entering before recaptcha/api.js is loaded): index.html ,我进行了一个故意阻塞调用来加载库(防止Angular世界在加载recaptcha/api.js之前进入):

<script src="https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key"></script>

From the constructor of an Angular Service I use the DOCUMENT DI token to reference the grecaptcha object: 从Angular Service的构造函数中,我使用DOCUMENT DI标记来引用grecaptcha对象:

  constructor(@Inject(DOCUMENT) private document: any) {
    this.grecaptcha = this.document.grecaptcha;
  }

Once the application's loaded (using lifecycle hooks ), the aforementioned Angular service calls grecaptcha.execute to obtain the unique token (as per the Frontend Integration guide): 一旦应用程序被加载(使用生命周期钩子 ),前面提到的Angular服务调用grecaptcha.execute来获取唯一token (根据前端集成指南):

  public executeCaptcha() {
    this.grecaptcha.ready(() => {
      this.grecaptcha
        .execute(MyService.CAPTCHA_KEY, {
          action: 'execute'
        })
        .then((token: string) => this.token = token);
    });
  }

The token is a parameter of the callback, and is stored as a member of the Angular service ( this.token = token) ). token是回调的参数,并存储为Angular服务的成员( this.token = token) )。

At this point the application has not made any API calls to my backend , nor has the user been verified as a human. 此时,应用程序没有对我的后端进行任何API调用,也没有将用户验证为人。

The token must be sent to my backend server, which in-turn must verify the user's response by making an API Request . 必须将token发送到我的后端服务器,后端服务器必须通过发出API请求验证用户的响应

The API Response can then be returned to the browser (Angular app): 然后可以将API响应返回给浏览器(Angular app):

{
  "success": true|false,
  "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
  "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
  "error-codes": [...]        // optional
}

Questions 问题

  • Should the token be sent with every HTTP request from my Angular app, and verified each time? 是否应该在我的Angular应用程序的每个HTTP请求中发送token ,并且每次都进行验证?
    • ... or can the user be verified once (at the start) and their score remembered in the Angular app? ...或者用户可以一次(在开始时)进行验证,并在Angular应用程序中记住他们的分数
  • I read somewhere that Google uses mouse movements, and various client-side signals to calculate the score. 我在某处读到谷歌使用鼠标移动和各种客户端信号来计算分数。
    • ...does this mean I should (re)verify periodically to get an improved score? ......这是否意味着我应该(重新)定期验证以获得更高的分数?

If you want to be secure you have to send token (each time new one) every time user is posting data via forms. 如果您想要安全,每次用户通过表单发布数据时,您都必须发送令牌(每次新签名)。 Otherwise if user find out that you are checking him just once he could click post the first time himself then he could run selenium or other scripting program, since his session would already be verified. 否则,如果用户发现你正在检查他,只要他第一次点击自己,那么他就可以运行selenium或其他脚本程序,因为他的会话已经被验证了。

Notice that you need to ask google each time for new token. 请注意,您每次都需要向Google询问新令牌。 Firstly because they allow you only to use one token single time, secondly token has small time-to-live time after which it it expired. 首先,因为它们只允许您使用一个令牌一次,其次令牌的生存时间很短,之后它就会过期。 And those two boundaries are for purpose to protect against vulnerability I described above. 这两个边界的目的是防止我上面描述的漏洞。

Of course there is always a trade-off between security and performance. 当然,安全性和性能之间总是存在权衡。 I would not recommend caching verification data, but maybe for some kind of lightweight search, you could consider it, but I would strongly advise against such practice when posting data, updating or deleting. 我不建议缓存验证数据,但也许对于某种轻量级搜索,你可以考虑它,但我强烈建议在发布数据,更新或删除时反对这种做法。

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

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