Monday, July 11, 2011

Google recaptcha with CSP, hosting javascript locally

I display google's recaptcha from google's recaptcha javascript hosted on my own server after a little manipulation (since I have CSP turned on, I have to bypass the in-body script and setInterval). But I still need to ping google every time for a new challenge.

1. So I saved my domain's equivalent of the RecaptchaState javascript medai/js/google/recState.js.

2. Refresh this file in views.py's by calling this function:
def recaptchaRefresh():
    # get the Recaptcha state.
    url = "https://www.google.com/recaptcha/api/challenge?k=%s" % settings.RECAPTCHA_PUBLIC_KEY
    resock = urllib.urlopen(url)
    data = resock.read()
    resock.close()

    # extract the recaptcha state part of the string
    docloc = data.find("document.write")

    recaptchaState = data[:docloc]

    f = open('media/js/google/recState.js', 'r+')
    f.write(recaptchaState)
    f.close()

3. Copy paste recaptcha.js and make the following changes for CSP compatibility:

3.1 CSP blocks setIntervals that takes string parameters, so change it into a function:
//Recaptcha.timer_id = setInterval("Recaptcha.reload('t');", (a.timeout - 300) * 1E3) 
// -->
Recaptcha.timer_id = setInterval( function() {Recaptcha.reload('t'); }, (a.timeout - 300) * 1E3)

3.2 CSP blocks in-body javascript, host it externally
//} else document.write('<div id="recaptcha_widget_div" style="display:none"></div>'), document.write('<script>Recaptcha.widget = Recaptcha.$("recaptcha_widget_div"); Recaptcha.challenge_callback();<\/script>');
// -->
} else document.write('<div id="recaptcha_widget_div" style="display:none"></div>'), document.write('<script src="http://haoqili.scripts.mit.edu/js/test3.js"><\/script>');

where http://haoqili.scripts.mit.edu/js/test3.js has "Recaptcha.widget = Recaptcha.$("recaptcha_widget_div"); Recaptcha.challenge_callback();"

4. and in your template.html include the javascript from step 2 and 3.

5. Change your settings.py's CSP policies to have "http[s]://www.google.com" allowed in many places. See example

10 comments:

  1. Spot on with this write-up, I honestly believe this web site needs far more attention. I'll probably be returning to read more, thanks for the information!
    website design

    ReplyDelete
  2. This type of hosting solution is known as VPS (virtual private server) hosting and is so called because it is a shared partitioned server which creates a virtual private space for your website.bulletproof hosting

    ReplyDelete
  3. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.  Read the reviews

    ReplyDelete
  4. I real glad to uncover this web internet site on bing, just what I was searching for. clicca qui

    ReplyDelete
  5. You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. visit website

    ReplyDelete
  6. just what most individuals' desire. However, most of which have no idea of the inner thoughts and multi-step means of purchasing a fresh home

    ReplyDelete
  7. If you don"t mind proceed with this extraordinary work and I anticipate a greater amount of your magnificent blog entries.  lemigliorivpn.com

    ReplyDelete
  8. This book gives you step wise extensive guide along with three golden lessons that helps your kids learn all the right things about survival lesmeilleursvpn

    ReplyDelete
  9. Just admiring your work and wondering how you managed this blog so well. It’s so remarkable that I can't afford to not go through this valuable information whenever I surf the internet!  Klik hier

    ReplyDelete
  10. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? Klik hier

    ReplyDelete