Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
¿Puede el tipo de variable definida no estar definido en javascript?

Aquí hay un código de ejemplo de Google sobre cómo inicializar de forma segura su servicio de recaptcha https://developers.google.com/recaptcha/docs/loading

 <script async src="https://www.google.com/recaptcha/api.js"></script> <script> if(typeof grecaptcha === 'undefined') { grecaptcha = {}; } grecaptcha.ready = function(cb){ //How is it possible for grecaptcha to be undefined here? if(typeof grecaptcha === 'undefined') { const c = '___grecaptcha_cfg'; window[c] = window[c] || {}; (window[c]['fns'] = window[c]['fns']||[]).push(cb); } else { cb(); } } grecaptcha.ready(function(){ grecaptcha.render("container", { sitekey: "ABC-123" }); }); </script>

Como puede ver, grecaptcha.ready es una función y nuevamente verifica if(typeof grecaptcha === 'undefined') . Pero, ¿cómo puede grecaptcha no estar undefined en el momento en que se llama a grecaptcha.ready ? Según tengo entendido, grecaptcha ya se ha inicializado como una propiedad del objeto global y su valor es {} , por lo tanto, su tipo debe ser "object"

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Tendría que ser un código en otro lugar, pero claro, es un objeto global, por lo que un actor malicioso podría sobrescribirlo, por ejemplo, podría tener algo como:

 var grecaptcha = { start() { var that = this; return new Promise((resolve, reject) => { setTimeout(() => { if (that.ready) { that.ready(that) } resolve(true); }, 5000); let remaining = 4; let interval = setInterval(() => { console.log(remaining--); if (remaining == 0) { clearInterval(interval); } }, 1000); }); } }; grecaptcha.start().then(() => console.log('But we still do stuff with it')); if(typeof grecaptcha === 'undefined') { grecaptcha = {}; } grecaptcha.ready = function(cb){ console.log(typeof grecaptcha); } setTimeout(() => grecaptcha = undefined, 1000)

about 4 years ago · Juan Pablo Isaza Relatório

0

Creo que el código está mal: https://jsfiddle.net/657b1gso/

Reportado: https://github.com/google/recaptcha/issues/485

y funciona con algunas pequeñas modificaciones: https://jsfiddle.net/p6ubwLme/

 <script async src="https://www.google.com/recaptcha/api.js"></script> <script> // How this code snippet works: // This logic overwrites the default behavior of `grecaptcha.ready()` to // ensure that it can be safely called at any time. When `grecaptcha.ready()` // is called before reCAPTCHA is loaded, the callback function that is passed // by `grecaptcha.ready()` is enqueued for execution after reCAPTCHA is // loaded. if(typeof grecaptcha === 'undefined') { grecaptcha = {}; } grecaptcha.asyncready = function(cb){ if(typeof grecaptcha.render === 'undefined') { // window.__grecaptcha_cfg is a global variable that stores reCAPTCHA's // configuration. By default, any functions listed in its 'fns' property // are automatically executed when reCAPTCHA loads. const c = '___grecaptcha_cfg'; window[c] = window[c] || {}; (window[c]['fns'] = window[c]['fns']||[]).push(cb); } else { cb(); } } // Usage grecaptcha.asyncready(function(){ grecaptcha.render("container", { sitekey: "ABC-123" }); }); </script> <div id="container"> </div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda